git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update docs, add more examples
commit4bb6219adf3fc8df6fa84b26db4c8ca5111360d1 authorPhil Freeman <paf31@cantab.net> dateMon 10 Feb 2014, 20:25 (13 years ago) parent997c4ee
4 files changed+30−4
docs/source/intro.rst+2−2 docs/source/patterns.rst+12 docs/source/types.rst+1−1 examples/passing/MultipleConstructorArgs.purs+15−1
docs/source/intro.rst+2−2
@@ -112,11 +112,11 @@ Line by line, this reads as follows:
112112The generated Javascript looks like this::
113113
114114 var Person = function (value) {
115− return { ctor: 'Person', value: value };
115+ return { ctor: 'Person', values: [value] };
116116 };
117117
118118 function showPerson(_1) {
119− return _1.value.name + ", aged " + numberToString(_1.value.age);
119+ return _1.values[0].name + ", aged " + numberToString(_1.values[0].age);
120120 };
121121
122122Related Projects
docs/source/patterns.rst+12
@@ -25,6 +25,7 @@ The following pattern types are supported:
2525- Variable pattern
2626- Array patterns
2727- Cons patterns
28+- Constructor patterns
2829- Record patterns
2930- Named patterns
3031- Guards
@@ -85,6 +86,17 @@ The head and tail of a non-empty array can be matched by using a cons pattern::
8586 addPairs (x : y : xs) = x * y + addPairs xs
8687 addPairs _ = 0
8788
89+
90+Constructor patterns
91+--------------------
92+
93+Constructor patterns match a data constructor and its arguments::
94+
95+ data Foo = Foo String | Bar Number Boolean
96+
97+ foo (Foo s) = true
98+ foo (Bar _ b) = b
99+
88100Record Patterns
89101---------------
90102
docs/source/types.rst+1−1
@@ -31,7 +31,7 @@ PureScript records correspond to Javascript objects. They may have zero or more
3131Tagged Unions
3232-------------
3333
34−Tagged unions consist of one or more constructors, each of which takes zero or one arguments.
34+Tagged unions consist of one or more constructors, each of which takes zero or more arguments.
3535
3636Tagged unions can only be created using their constructors, and deconstructed through pattern matching (see later).
3737
examples/passing/MultipleConstructorArgs.purs+15−1
@@ -1,14 +1,28 @@
11module MultipleConstructorArgs where
22
3+import Arrays
4+
35data P a b = P a b
46
57runP :: forall a b r. (a -> b -> r) -> P a b -> r
68runP f (P a b) = f a b
79
10+idP = runP P
11+
12+testCase = \p -> case p of
13+ P (x:xs) (y:ys) -> x : y : testCase (P xs ys)
14+ P _ _ -> []
15+
16+test1 = testCase (P [1, 2, 3] [4, 5, 6])
17+
818module Main where
919
1020import Prelude
1121import MultipleConstructorArgs
1222import Global
23+import Eff
24+import Arrays
1325
14−main = Trace.trace (runP (\s n -> s ++ show n) (P "Test" 1))
26+main = do
27+ Trace.trace (runP (\s n -> s ++ show n) (P "Test" 1))
28+ Trace.print test1