I wanted to map over a nested array in factor so I created a helper function:

: nested-map ( a quote -- a' ) swap [ over map ] map nip ;

which i then called with

{ { 1 2 } { 3 4 } } [ 1 + ] nested-map

But when I call it I get the error from the title.

If I just paste the body of the function it works as intended:

{ { 1 2 } { 3 4 } } [ 1 + ] swap [ over map ] map nip

So I guess I have two questions: is there a better way to achieve the original goal, but also how am I supposed to create higher order functions without getting this error?

  • Tushta@programming.devOP
    link
    fedilink
    arrow-up
    2
    ·
    5 months ago

    Thanks to johnb on the Factor’s discord channel I got answers to these two questions:

    In terms of nicer way to do a nested map nicely, fried quotations are the answer:

    '[ _ map ] map

    and in order to call a quotation inside a word definition, one needs to use call( stack effect ) form:

    : nested-map ( a quote -- a' ) '[ _ '[ _ call( x -- x' ) ] map ] map ;