The
infix vocabulary implements support for infix notation in Factor source code.
[infix
INFIX::
The usual infix math operators are supported:
• | + |
• | - |
• | * |
• | / |
• | **, which is the infix operator for ^. |
• | %, which is the infix operator for mod. |
The standard precedence rules apply: Grouping with parentheses before
*,
/and
% before
+ and
-.
USE: infix
[infix 5-40/10*2 infix] .
-3
You can call Factor words in infix expressions just as you would in C. There are some restrictions on which words are legal to use though:
• | The word must return exactly one value. |
• | The word name must consist of the letters a-z, A-Z, _ or 0-9, and the first character can't be a number. |
USING: infix locals math.functions ;
:: binary_entropy ( p -- h )
[infix -(p*log(p) + (1-p)*log(1-p)) / log(2) infix] ;
[infix binary_entropy( sqrt(0.25) ) infix] .
1.0
You can access
sequences inside infix expressions with the familiar
seq[index] notation.
USING: arrays locals infix ;
[let { 1 2 3 4 } :> myarr [infix myarr[4/2]*3 infix] ] .
9
You can create sub-
sequences inside infix expressions using
seq[from:to] notation.
USING: arrays locals infix ;
[let "foobar" :> s [infix s[0:3] infix] ] .
"foo"
Additionally, you can step through
sequences with
seq[from:to:step] notation.
USING: arrays locals infix ;
[let "reverse" :> s [infix s[::-1] infix] ] .
"esrever"
USING: arrays locals infix ;
[let "0123456789" :> s [infix s[::2] infix] ] .
"02468"