smalltalk-tng
view r2/test.tng @ 323:454c18798969
merger
| author | Tony Garnock-Jones <tonygarnockjones@gmail.com> |
|---|---|
| date | Tue Feb 07 11:34:20 2012 -0500 (3 months ago) |
| parents | d6d37f34aa95 |
| children |
line source
1 define map [
2 f: [Nil : Nil
3 [Hd: h Tl: t] : [Hd: f h Tl: map f t]]
4 ];;
6 (arg Hd)
7 (arg Tl)
9 "a pattern:" [(a b): _]
10 "-->" (arg a b)
12 define fold-left [
13 kons: [knil: [Nil : knil
14 [Hd: h Tl: t] : fold-left kons (kons h knil) t]]
15 ];;
17 map [x: x + 1] ([1, 2, 3] AsList);;
19 letrec (map = [f: [Nil : Nil
20 [Hd: h Tl: t]) : [Hd: f h Tl: map f t]],
21 toList = [[] : Nil
22 [h ; t]: [Hd: h Tl: (toList t)]]); "yikes"
23 map [x: x] (toList [1 2 3])
24 ;;
26 map [x: x + 1] (toList [1 2 3]);;
28 [x: (Update: [] Set: x To: 123)] 'hi';;
30 define-behaviour Cst;;
31 define-method ({Cst cst} Convert) (
32 Case: {cst} Of: (
33 [[Adj: [l, r]]: [(l Convert) (r Convert)]],
34 [[Tuple: cs]: (let cs* = map [x: x Convert] cs in {Tuple cs*})],
35 [[Quote: v]: v],
36 [_: cst]
37 )
38 );;
40 "To lift something:" {lifted} <- val
41 "To drop something:" val <- {lifted}
43 define-method ({Tuple x} Length) (
44 x Length;;
45 );;
47 define-method ({Tuple x} AsList) (
48 (0 .. x Length) Map: [i: x ? i]
49 );;
51 "A semi- or pseudo-monadic version:"
52 define-method ((results = {ParseResults _}) Next) (
53 [has-value, next] <- (atomic Read: (results Next*)),
54 (If: has-value
55 Then: next
56 Else: (next-value <- (next []),
57 atomic Into: (results Next*) Write: (True, next-value),
58 next-value))
59 );;
61 "A monadic version:"
62 "(This is stupid code anyway because it's a lazy language,
63 it already delays and evaluates once!)"
64 "We don't put the 'atomic' in here to stay compositional as long as possible"
65 define-method ((results = {ParseResults _}) Next) (
66 [has-value, next] <- (Read: (results Next*)),
67 (If: has-value
68 Then: return next
69 Else: (next-value <- return (next []),
70 Into: (results Next*) Write: (True, next-value),
71 return next-value))
72 );;
74 "We don't put the 'atomic' in here to stay compositional as long as possible"
75 define-method (var Bump) (
76 Into: var Write: (1 + (Read: var))
77 );;
79 let valid-orders = map [x: [x, x]] ['sale_date', 'cost', 'address'] in
80 let [_, order] = Find: [[l, r]: l == order] In: valid-orders
81 IfAbsent: [_, 'sale_date']
82 in ...
83 ;;
