Also, your each2* can be implemented without local variables or closures:
: each2* ( seq quot: ( prev next -- next' ) -- )
    [ unclip-slice swap ] dip each drop ; inline

Jon

On Fri, Jan 20, 2017 at 8:23 PM, Jon Harper <jon.harpe...@gmail.com> wrote:

> When I run your code, I don't get what you said:
> :: each2* ( seq quot: ( prev next -- next' ) -- )
>     seq unclip-slice :> elt! quot '[ elt @ elt! ] each ; inline
>
> { 1 2 3 } [ [ swap . . nl ] keep 10 + ] each2*
> 2
> 1
>
> 3
> 11
>
> It's actually pretty close to what you wanted, you just swapped the prev
> and next element in the each2* you wrote. The following has the correct
> behavior.
>  :: each2* ( seq quot: ( prev next -- next' ) -- )
>     seq unclip-slice :> elt! quot '[ elt swap @ elt! ] each ; inline
>
> { 1 2 3 } [ [ swap . . nl ] keep 10 + ] each2*
> 1
> 2
>
> 12
> 3
>
> Also, you did create a closure. This is exactly what you want, and the
> assignments to elt do work, the value is updated in the subsequent calls.
> (In fact, factor implements local mutable variable by boxing the value in a
> 1 element array. The array is curried into the quotation).
>
> Hope that helps,
> Jon
>
>
>
> Jon
>
> On Wed, Jan 18, 2017 at 7:20 PM, 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