Skip to content

Commit d1082bc

Browse files
authored
Merge pull request #5 from lpil/lp/tuple
Tuple module
2 parents f7b71df + 14b8e9a commit d1082bc

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_build

src/tuple.alp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{-| Some helpers for working with 2-tuples.
2+
3+
For larger chunks of data, it is best to switch to using records. So
4+
instead of representing a 3D point as `(3,4,5)` and wondering why there
5+
are no helper functions, represent it as `{ x = 3, y = 4, z = 5 }` and
6+
use all the built-in syntax for records.
7+
-}
8+
9+
module tuple
10+
11+
export first/1, second/1, map_first/2, map_second/2, tuple/2
12+
13+
14+
{-| Extract the first value from a tuple.
15+
16+
first (3, 4) == 3
17+
first ("john", "doe") == "john"
18+
-}
19+
val first 'a 'b : fn ('a, 'b) -> 'a
20+
let first (x, _) =
21+
x
22+
23+
test "first example 1" =
24+
assert.equal (first (3, 4)) 3
25+
26+
test "first example 2" =
27+
assert.equal (first ("john", "doe")) "john"
28+
29+
30+
{-| Extract the second value from a tuple.
31+
32+
second (3, 4) == 4
33+
second ("john", "doe") == "doe"
34+
-}
35+
val second 'a 'b : fn ('a, 'b) -> 'b
36+
let second (_, y) =
37+
y
38+
39+
test "second example 1" =
40+
assert.equal (second (3, 4)) 4
41+
42+
test "second example 2" =
43+
assert.equal (second ("john", "doe")) "doe"
44+
45+
46+
{-| Transform the first value in a tuple.
47+
48+
map_first (fn x -> x + 1) (0, 0) == (1, 0)
49+
-}
50+
val map_first 'a 'b 'aa : fn (fn 'a -> 'aa) ('a, 'b) -> ('aa, 'b)
51+
let map_first func (x, y) =
52+
(func x, y)
53+
54+
test "map_first" =
55+
let result = map_first (fn x -> x + 1) (0, 0) in
56+
assert.equal result (1, 0)
57+
58+
59+
{-| Transform the second value in a tuple.
60+
61+
map_second (fn x -> x + 1) (0, 0) == (0, 1)
62+
-}
63+
val map_second 'a 'b 'bb : fn (fn 'b -> 'bb) ('a, 'b) -> ('a, 'bb)
64+
let map_second func (x, y) =
65+
(x, func y)
66+
67+
test "map_second" =
68+
let result = map_second (fn x -> x + 1) (0, 0) in
69+
assert.equal result (0, 1)
70+
71+
72+
{-| Construct a tuple from 2 values.
73+
74+
tuple 1 2 == (1, 2)
75+
-}
76+
val tuple 'a 'b : fn 'a 'b -> ('a, 'b)
77+
let tuple x y =
78+
(x, y)
79+
80+
test "tuple" =
81+
let result = tuple 1 2 in
82+
assert.equal result (1, 2)

0 commit comments

Comments
 (0)