]> gitweb.factorcode.org Git - factor.git/blob - extra/variants/variants-docs.factor
scryfall: better moxfield words
[factor.git] / extra / variants / variants-docs.factor
1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays classes classes.singleton classes.tuple help.markup
4 help.syntax kernel multiline slots quotations ;
5 IN: variants
6
7 HELP: VARIANT:
8 { $syntax "
9 VARIANT: class-name
10     singleton
11     singleton
12     tuple: { slot slot slot ... }
13     .
14     .
15     .
16     ; " }
17 { $description "Defines " { $snippet "class-name" } " as a union of the following " { $link singleton-class } " and " { $link tuple-class } " definitions. Each " { $snippet "singleton" } " word is defined as a " { $snippet "singleton-class" } ", and each " { $snippet "tuple" } " word is defined as a " { $snippet "tuple-class" } " with the given set of " { $snippet "slot" } "s, using the same syntax for slot specifiers as " { $link POSTPONE: TUPLE: } ". Typed tuple slots can recursively reference the variant " { $snippet "class-name" } " being defined. For " { $snippet "tuple" } " types, a " { $link boa } " constructor word " { $snippet "<tuple>" } " is defined as well." }
18 { $examples { $code "
19 USING: kernel variants ;
20 IN: scratchpad
21
22 VARIANT: list
23     nil
24     cons: { { first object } { rest list } }
25     ;
26 " } } ;
27
28 HELP: VARIANT-MEMBER:
29 { $description "Defines a new member of a variant class without restricting such definitions to a single statement or source file. The variant class should be listed first, and the class member should follow." }
30 { $examples { $code "
31 USING: kernel variants ;
32 IN: scratchpad
33
34 VARIANT: list ;
35
36 VARIANT-MEMBER: list nil ;
37 VARIANT-MEMBER: list cons: { { first object } { rest list } } ;
38 " } } ;
39
40 HELP: match
41 { $values { "branches" array } }
42 { $description "Dispatches on the type of the value on the top of the stack. If the type is a " { $link singleton-class } ", the corresponding quotation is called with the underlying stack unchanged. If the type is a " { $link tuple-class } ", the tuple slots are pushed onto the stack by order of arguments." }
43 { $examples { $example "
44 USING: kernel math prettyprint variants ;
45 IN: scratchpad
46
47 VARIANT: list
48     nil
49     cons: { { first object } { rest list } }
50     ;
51
52 : list-length ( list -- length )
53     {
54         { nil [ 0 ] }
55         { cons [ nip list-length 1 + ] }
56     } match ;
57
58 1 2 3 4 nil <cons> <cons> <cons> <cons> list-length .
59 " "4" } } ;
60
61 HELP: unboa
62 { $values { "class" class } }
63 { $description "Decomposes a tuple of type " { $snippet "class" } " into its component slot values by order of arguments. The inverse of " { $link boa } "." } ;
64
65 HELP: variant-class
66 { $class-description "This class comprises class names that have been defined with " { $link POSTPONE: VARIANT: } ". When a " { $snippet "variant-class" } " is used as the type of a specialized " { $link tuple } " slot, the variant's first member type is used as the default " { $link initial-value } "." } ;
67
68 { POSTPONE: VARIANT: variant-class match } related-words
69
70 ARTICLE: "variants" "Algebraic data types"
71 "The " { $vocab-link "variants" } " vocabulary provides syntax and combinators for defining and manipulating algebraic data types."
72 { $subsections
73     POSTPONE: VARIANT:
74     POSTPONE: VARIANT-MEMBER:
75     variant-class
76     match
77 } ;
78
79 ABOUT: "variants"