Skip to content

Commit da230aa

Browse files
committed
Records, Blocks.
1 parent 7358bf7 commit da230aa

6 files changed

+120
-11
lines changed

Anonymous_functions.fold

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@
99

1010
(-> system :fetcher :conf :credentials :username)
1111

12-
=> system #fetcher #conf #credentials #username
12+
-> system #fetcher #conf #credentials #username
1313

14-
-> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
14+
-> map (data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
1515
-> map (process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
1616

17-
-> map (\data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
17+
-> map (\data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
1818
-> map \(process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
1919

2020
-> map (λ data flag → process data verbose: (not flag)) [1, 2, 3, 4, 5]
2121
-> map λ(process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
2222

23-
-> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
23+
-> map (data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
2424
-> map {process ~ verbose: (not ~)} [1, 2, 3, 4, 5]
2525

26-
-> map (\x -> x * x) [1 .. 5]
26+
-> map (|data flag| -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
27+
-> map {process ~ verbose: (not ~)} [1, 2, 3, 4, 5]
28+
29+
-> map (|x| -> x * x) [1 .. 5]
2730
= [1, 4, 9, 16, 25]
2831

2932
-> map \(~1 * ~1) [1 .. 5]
3033
= [1, 4, 9, 16, 25]
3134

32-
3335
-- Consider using ~0 as a reference to the anonymous function, allowing this way recursion.
3436

3537
-- Mathematica

Blocks.fold

+7-5
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,25 @@ end
226226
-- // --
227227

228228

229-
(function factorial [n]
229+
(def factorial [n]
230230
(log/info "Will calculate factorial of {n}")
231231
(if (= n 0)
232232
1
233233
(* n (factorial (- n 1)))))
234234

235-
function factorial n do
235+
236+
237+
def factorial n do
236238
Log.info "Will calculate factorial of {n}"
237239
(n == 0) ? 1 : n * factorial (n - 1)
238240
end
239241

240-
let factorial n =
242+
def factorial n =
241243
Log.info "Will calculate factorial of {n}"
242244
(n == 0) ? 1 : n * factorial (n - 1)
243245
end
244246

245-
function factorial n:
247+
def factorial n:
246248
Log.info "Will calculate factorial of {n}"
247249
(n == 0) ? 1 : n * factorial (n - 1)
248250
end
@@ -252,7 +254,7 @@ factorial n = do
252254
(n == 0) ? 1 : n * factorial (n - 1)
253255
end
254256

255-
function factorial n {
257+
def factorial n = {
256258
Log.info "Will calculate factorial of {n}"
257259
(n == 0) ? 1 : n * factorial (n - 1)
258260
}

Modules.fold

+46
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,49 @@ module User =
102102
compare x y =
103103

104104

105+
106+
-- // --
107+
108+
109+
110+
module JSONCodec n do
111+
Log.info "Will calculate factorial of {n}"
112+
(n == 0) ? 1 : n * factorial (n - 1)
113+
end
114+
115+
interface Codec a =
116+
encode :: a -> Result Data
117+
decode :: Data -> Result a
118+
end
119+
120+
module Codec Json =
121+
encode = String x => "\"{x}\""
122+
| Object x => surround x with ('{', '}')
123+
| List xs => surround (join (map encode xs) with: ',')
124+
with: '"'
125+
end
126+
127+
module JSONCodec n =
128+
Log.info "Will calculate factorial of {n}"
129+
(n == 0) ? 1 : n * factorial (n - 1)
130+
end
131+
132+
module JSONCodec n:
133+
Log.info "Will calculate factorial of {n}"
134+
(n == 0) ? 1 : n * factorial (n - 1)
135+
end
136+
137+
JSONCodec n = module
138+
Log.info "Will calculate factorial of {n}"
139+
(n == 0) ? 1 : n * factorial (n - 1)
140+
end
141+
142+
module JSONCodec n = {
143+
Log.info "Will calculate factorial of {n}"
144+
(n == 0) ? 1 : n * factorial (n - 1)
145+
}
146+
147+
JSONCodec n = module {
148+
Log.info "Will calculate factorial of {n}"
149+
(n == 0) ? 1 : n * factorial (n - 1)
150+
}

Records.fold

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
type Pair a = { first::a, second::a }
3+
4+
Pair :: first: a -> second: a -> Pair a
5+
6+
7+
type Person = MkPerson String Int
8+
9+
showPerson : Person -> String
10+
showPerson p = let MkPerson name age = p in
11+
name ++ " is " ++ show age ++ " years
12+
old"
13+
14+
15+
alan = { name: "Alan Turing", age: 32 }
16+
17+
alan = { name: "Alan Turing" age: 32 }

Records_examples.fold

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
type Pair a = { first: a,
3+
second: a }
4+
5+
-- A generic function
6+
function swap (pair::Pair a) -> Pair a = {
7+
Pair { first, second } = pair
8+
Pair { first: second, second: first }
9+
10+
Pair first: pair.second, second: pair.first
11+
}
12+
13+
fn swap<T>(pair: Pair<T>) -> Pair<T> {
14+
let Pair { first, second } = pair;
15+
16+
Pair { first: second, second: first }
17+
}
18+
19+
// Reimplementing a 2-element tuple as a tuple struct
20+
struct Tuple2<T, U>(T, U);
21+
22+
fn main() {
23+
// Explicitly specialize `Pair`
24+
let pair_of_chars: Pair<char> = Pair { first: 'a', second: 'b' };
25+
26+
// Implicitly specialize `Pair`
27+
let pair_of_ints = Pair { first: 1i32, second: 2 };
28+
29+
// Explicitly specialize `Tuple2`
30+
let _tuple: Tuple2<char, i32> = Tuple2('R', 2);
31+
32+
// Explicitly specialize `swap`
33+
let _swapped_pair_of_chars = swap::<char>(pair_of_chars);
34+
35+
// Implicitly specialize `swap`
36+
let _swapped_pair_of_ints = swap(pair_of_ints);
37+
}

Types.fold

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
type Result a b = Ok a | Error b
3+
4+
5+
type

0 commit comments

Comments
 (0)