Examples of fried quotations
Factor handbook » The language » Fried quotations

Prev:@
Next:Fried quotation philosophy


The easiest way to understand fried quotations is to look at some examples.

If a quotation does not contain any fry specifiers, then '[ behaves just like [:
{ 10 20 30 } '[ . ] each

Occurrences of _ on the left map directly to curry. That is, the following three lines are equivalent:
{ 10 20 30 } 5 '[ _ + ] map { 10 20 30 } 5 [ + ] curry map { 10 20 30 } [ 5 + ] map

Occurrences of _ in the middle of a quotation map to more complex quotation composition patterns. The following three lines are equivalent:
{ 10 20 30 } 5 '[ 3 _ / ] map { 10 20 30 } 5 [ 3 ] swap [ / ] curry compose map { 10 20 30 } [ 3 5 / ] map

Occurrences of @ are simply syntax sugar for _ call. The following four lines are equivalent:
{ 10 20 30 } [ sq ] '[ @ . ] each { 10 20 30 } [ sq ] [ call . ] curry each { 10 20 30 } [ sq ] [ . ] compose each { 10 20 30 } [ sq . ] each

The _ and @ specifiers may be freely mixed, and the result is considerably more concise and readable than the version using curry and compose directly:
{ 8 13 14 27 } [ even? ] 5 '[ @ dup _ ? ] map { 8 13 14 27 } [ even? ] 5 [ dup ] swap [ ? ] curry compose compose map { 8 13 14 27 } [ even? dup 5 ? ] map

The following is a no-op:
'[ @ ]

Here are some built-in combinators rewritten in terms of fried quotations:
literalize: literalize '[ _ ] ;
curry: curry '[ _ @ ] ;
compose: compose '[ @ @ ] ;