Sorry for not responding earlier, but I think your ``each2*`` is just:

``[ ] swap map-reduce``



On Wed, Jan 18, 2017 at 10:20 AM, Alexander Ilin <ajs...@yandex.ru> wrote:

> Hello!
>
>   I've got a task where I need to process a sequence of elements pairwise,
> while collecting the (possibly mutated) elements into a new sequence.
>
>   The neighboring elements may affect each other, hence the need for
> mutation.
>
>   I searched through the help system looking for words similar to
> each-pair or each2, to no avail.
>
>   Here's what I came up with:
>
> ```
> :: each2* ( seq quot: ( prev next -- next' ) -- )
>     seq unclip-slice :> elt! quot '[ elt @ elt! ] each ; inline
> ```
>
>   What I want to happen is this:
>
> ```
> { 1 2 3 } [ [ swap . . nl ] keep 10 + ] each2*
> 1
> 2
>
> 12
> 3
> ```
>
>   So that the quotation passed to `each2*` would walk through all
> consecutive pairs of the elements in `seq`, but would also be able to
> mutate the `next` element for its next iteration.
>
>   Now, I have no experience with functional languages (in fact, that's why
> I'm studying Factor, so feel free to correct me), but my suspicion is that
> I inadvertently created a closure over the `elt` variable, because instead
> of the expected output I get the following:
>
> ```
> { 1 2 3 } [ [ swap . . nl ] keep 10 + ] each2*
> 1
> 2
>
> 1
> 3
> ```
>
>   By using the walker I see that the initial value of `elt` is curried to
> the quotation, so the subsequent `elt!` has no effect.
>
>   Please, tell me: is this the intended interaction between the locals and
> the fry, or is it an optimization gone wrong?
>
>   And how do I get the behavior that I want from the `each2*`? Is there a
> totally different approach that I missed? My intention is to use `each2*`
> inside the `make` call to construct the result I need.
>
>   Thank you!
>
> ---=====---
>  Александр
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to