-
Notifications
You must be signed in to change notification settings - Fork 6
/
examples.cr
57 lines (45 loc) · 1.04 KB
/
examples.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require "./src/crz"
include CRZ
# # Basic algebraic data type
adt IntList,
Empty,
Cons(Int32, IntList)
empty = IntList::Empty.new
listWithJust1 = IntList::Cons.new 1, empty
listWith0And1 = IntList::Cons.new 0, (IntList::Cons.new 1, IntList::Empty.new)
## or
listWith0And1 = IntList::Cons.new 0, listWithJust1
pp empty
pp listWithJust1
pp listWith0And1
head = IntList.match listWithJust1, IntList, {
[Cons, x, xs] => x,
[Empty] => nil
}
pp head
adt List(A),
Empty,
Cons(A, List(A))
empty = List::Empty(Int32).new
cons = List::Cons.new 1, empty
head = List.match cons, List(Int32), { # Just List won't work here, it has to be concrete type List(Int32)
[Cons, x, _] => x,
[_] => nil
}
pp head
option = Option::Some.new(1)
.map {|x| x+1}
.map {|x| x.to_s}
.map {|s| "asdf" + s}
puts option.to_s
def sum(x, y)
x + y
end
a = lift_apply sum, Option::Some.new(1), Option::Some.new(2)
puts a.to_s
c = mdo({
x <= Option::Some.new(1),
y <= Option::Some.new(2),
Option::Some.new(x + y)
})
puts c.to_s # ==> Some(3)