slack1
2018-8-21 16:03:32

How might one make the bounded variable n from the sequence one is iterating over in a for/fold, such as [n (in-naturals)], available to the final #:results clause without creating a separate accumulator variable?


samth
2018-8-21 16:04:53

@pnwamk see @slack1’s question


pnwamk
2018-8-21 16:04:59

@pnwamk has joined the channel


pnwamk
2018-8-21 16:14:05

@slack1 do you have a brief example? I’m not positive I know what you mean by having “the sequence” available in the #:results clause.


slack1
2018-8-21 16:17:24
(for/fold ([stuff 0] #:result n)
          ([n (in-naturals)] #:break (< 10 n))
          (add1 stuff))

slack1
2018-8-21 16:18:01

I know this is a contrived example


slack1
2018-8-21 16:18:08

But my actual example is kind of convoluted and long


pnwamk
2018-8-21 16:18:18

It’s still helpful — so you mean the last value observed in the sequence?


slack1
2018-8-21 16:18:24

Yes


pnwamk
2018-8-21 16:19:31

Okay — I don’t think there’s a convenient way to do that today. But if that’s a valuable addition I would assume it would not be a complicated PR to add such a feature….


pnwamk
2018-8-21 16:20:09

in particular, if the accumulator and sequencing ids must be unique then it seems like it would be trivial to add. If they can conflict… I’d have to think about that and look at the implementation some more


slack1
2018-8-21 16:20:50

I see, in the meantime I’ll just add a [index 0]


slack1
2018-8-21 16:21:35

thanks


pnwamk
2018-8-21 16:22:31

hah, so the identifiers can indeed conflict and shadow each other. I guess to add such a feature we’d have to figure out what the following should return (0 or 9):


pnwamk
2018-8-21 16:22:58
#lang racket


(for/fold ([n 0]
           #:result n)
          ([n (in-range 10)])
  n)

slack1
2018-8-21 16:23:14

Ah…


pnwamk
2018-8-21 16:23:28

actually it returns 9 today, so it would have to keep returning 9… yah I’d say this is worth a github issue


pnwamk
2018-8-21 16:23:41

maybe if we can figure out what the corner cases are it wouldn’t be a difficult feature to add


slack1
2018-8-21 16:24:23

Or is there a more fitting construct for what I’m trying to do


slack1
2018-8-21 16:24:38

perhaps I’m just abusing for/fold


pnwamk
2018-8-21 16:25:12

I’m not sure what the “more fitting” construct would be


pnwamk
2018-8-21 16:25:25

this doesn’t seem like a crazy idea (the values are already computed and there after all)


pnwamk
2018-8-21 16:26:00

I’m confused as to why today that example I wrote returns 9 though…


pnwamk
2018-8-21 16:27:12

now I’m concerned this is something I should have thought of when we added #:result


pnwamk
2018-8-21 16:27:23

why does this return "foo"???


pnwamk
2018-8-21 16:27:26
(for/fold ([n 0]
           #:result n)
          ([n (in-range 10)])
  "foo")

pnwamk
2018-8-21 16:27:53

oh wait, that’s the value assigned to n, nevermind!


pnwamk
2018-8-21 16:28:18

oh duh, of course the original returns 9


pnwamk
2018-8-21 16:28:28

sorry my brain’s all over the place this morning it turns out xD


slack1
2018-8-21 16:28:56

I do think it’s sanitary to not have the same id’s in such close proximity


slack1
2018-8-21 16:29:03

but if it’s not enforced I can see how it’s problematic


pnwamk
2018-8-21 16:29:34

It looks like if we add the feature, the accumulators would need to make sure and shadow any iteration identifiers:


pnwamk
2018-8-21 16:29:37
(for/fold ([n 0]
           #:result n)
          ([n (in-range 10)])
  (add1 n))

pnwamk
2018-8-21 16:29:45

since that returns 10 (and not 9)


pnwamk
2018-8-21 16:30:01

okay yah I think this would be a straightforward addition — I’ll make an issue


slack1
2018-8-21 16:30:24

woots



notjack
2018-8-21 19:30:53

@pnwamk any chance we can just disallow duplicate identifiers across accumulators and sequence elements? or put a note in the docs saying “folks, just don’t do this”


joelmccracken
2018-8-22 00:06:17

is there a language that is side-effect and mutation free?


joelmccracken
2018-8-22 00:07:15

a “pure” lang in which the core logic for an app could be built, and the side effects done by other layers