]> gitweb.factorcode.org Git - factor.git/blob - extra/infix/infix-docs.factor
d99116424fd6b2f41ed839843294eb956edadc74
[factor.git] / extra / infix / infix-docs.factor
1 ! Copyright (C) 2009 Philipp Brüschweiler
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: help.syntax help.markup math prettyprint locals sequences ;
4 IN: infix
5
6 HELP: [infix
7 { $syntax "[infix ... infix]" }
8 { $description "Parses the infix code inside the brackets, converts it to stack code and executes it." }
9 { $examples
10     { $example
11         "USING: infix prettyprint ;"
12         "IN: scratchpad"
13         "[infix 8+2*3 infix] ."
14         "14"
15     } $nl
16     { $link POSTPONE: [infix } " isn't that useful by itself, as it can only access literal numbers and no variables. It is designed to be used together with locals; for example with " { $link POSTPONE: :: } " :"
17     { $example
18         "USING: infix locals math.functions prettyprint ;"
19         "IN: scratchpad"
20         ":: quadratic-equation ( a b c -- z- z+ )"
21         "    [infix (-b-sqrt(b*b-4*a*c)) / (2*a) infix]"
22         "    [infix (-b+sqrt(b*b-4*a*c)) / (2*a) infix] ;"
23         "1 0 -1 quadratic-equation . ."
24         "1.0\n-1.0"
25     }
26 } ;
27
28 HELP: [infix|
29 { $syntax "[infix| binding1 [ value1... ]\n        binding2 [ value2... ]\n        ... |\n    infix-expression infix]" }
30 { $description "Introduces a set of lexical bindings and evaluates the body as a snippet of infix code. The values are evaluated in parallel, and may not refer to other bindings within the same " { $link POSTPONE: [infix| } " form, as it is based on " { $link POSTPONE: [let } "." }
31 { $examples
32     { $example
33         "USING: infix prettyprint ;"
34         "IN: scratchpad"
35         "[infix| pi [ 3.14 ] r [ 12 ] | r*r*pi infix] ."
36         "452.16"
37     }
38 } ;
39
40 { POSTPONE: [infix POSTPONE: [infix| } related-words
41
42 ARTICLE: "infix" "Infix notation"
43 "The " { $vocab-link "infix" } " vocabulary implements support for infix notation in Factor source code."
44 { $subsections
45     POSTPONE: [infix
46     POSTPONE: [infix|
47 }
48 $nl
49 "The usual infix math operators are supported:"
50 { $list
51     { $link + }
52     { $link - }
53     { $link * }
54     { $link / }
55     { { $snippet "%" } ", which is the infix operator for " { $link mod } "." }
56 }
57 "The standard precedence rules apply: Grouping with parentheses before " { $snippet "*" } ", " { $snippet "/" } "and " { $snippet "%" } " before " { $snippet "+" } " and " { $snippet "-" } "."
58 { $example
59     "USE: infix"
60     "[infix 5-40/10*2 infix] ."
61     "-3"
62 }
63 $nl
64 "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:"
65 { $list
66     "The word must return exactly one value."
67     "The word name must consist of the letters a-z, A-Z, _ or 0-9, and the first character can't be a number."
68 }
69 { $example
70     "USING: infix locals math.functions ;"
71     ":: binary_entropy ( p -- h )"
72     "    [infix -(p*log(p) + (1-p)*log(1-p)) / log(2) infix] ;"
73     "[infix binary_entropy( sqrt(0.25) ) infix] ."
74     "1.0"
75 }
76 $nl
77 "You can access " { $vocab-link "sequences" } " inside infix expressions with the familiar " { $snippet "arr[index]" } " notation."
78 { $example
79     "USING: arrays infix ;"
80     "[infix| myarr [ { 1 2 3 4 } ] | myarr[4/2]*3 infix] ."
81     "9"
82 }
83 "Please note: in Factor " { $emphasis "fixnums are sequences too." } " If you are not careful with sequence accesses you may introduce subtle bugs:"
84 { $example
85     "USING: arrays infix locals ;"
86     ":: add-2nd-element ( x y -- res )"
87     "    [infix x[1] + y[1] infix] ;"
88     "{ 1 2 3 } 5 add-2nd-element ."
89     "3"
90 }
91 ;
92
93 ABOUT: "infix"