-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprelude.oops
More file actions
100 lines (97 loc) · 1.55 KB
/
prelude.oops
File metadata and controls
100 lines (97 loc) · 1.55 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
trait Stream
head
tail
is_empty
def for_each(f)
if self.is_empty() then
[]
else
f(self.head())
self.tail().for_each(f)
end
end
def map(f)
if self.is_empty() then
[]
else
f(self.head()) :: self.tail().map(f)
end
end
def filter(f)
if self.is_empty() then
[]
else
let x = self.head() in
if f(x) then
x :: self.tail().filter(f)
else
self.tail().filter(f)
end
end
end
end
def fold(f, i)
if self.is_empty() then
i
else
self.tail().fold(f, f(i, self.head()))
end
end
def collect()
if self.is_empty() then
[]
else
self.head() :: self.tail().collect()
end
end
end
impl Stream for List
def is_empty()
self.length() == 0
end
end
struct Complex
re = 0
im = 0
end
impl for Complex
def +(other)
Complex{re: self.re + other.re, im: self.im + other.im}
end
def -(other)
Complex{re: self.re - other.re, im: self.im - other.im}
end
def *(other)
Complex{re: self.re * other.re - self.im * other.im, im: self.re * other.im + self.im * other.re}
end
def /(other)
self * other.conj() * Complex{re: 1 / (other.abs() * other.abs())}
end
def conj()
Complex{re: self.re, im: -self.im}
end
def abs()
sqrt(self.re * self.re + self.im * self.im)
end
def to_string()
"{0} + {1}i".format([self.re, self.im])
end
end
struct Range
start
stop
end
impl Stream for Range
def head()
self.start
end
def tail()
Range{start: self.start + 1, stop: self.stop}
end
def is_empty()
self.start > self.stop
end
def to_string()
"{0}..{1}".format([self.start, self.stop])
end
end