Skip to content

Commit c6b81a3

Browse files
authored
Prelude extension (#268)
- Adds `min` and `max` functions - Adds `strListCat` for efficient string concatenation and `replicate` for string repetition - Exposes fst and snd as methods on pairs - Adds map method to Option type
1 parent b0515e8 commit c6b81a3

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

lib/Base/Option.fram

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ pub method unwrap {~__line__, ~__file__, ?msg} self =
3131
| None => runtimeError (msg.unwrapOr "Called `unwrap` on `None`")
3232
| Some x => x
3333
end
34+
35+
{## Maps value stored in `Some`. ##}
36+
pub method map self f =
37+
match self with
38+
| None => None
39+
| Some x => Some (f x)
40+
end

lib/Prelude.fram

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ pub let id x = x
2323
pub let flip f x y = f y x
2424

2525
pub let fst (x, _) = x
26+
pub method fst = fst
2627
pub let snd (_, y) = y
28+
pub method snd = snd
2729

2830
pub let not b = if b then False else True
2931

@@ -48,3 +50,25 @@ pub module Int64
4850
pub let one = 1L
4951
pub let ofInt (n : Int) = n.toInt64
5052
end
53+
54+
## Returns the greater of two elements `x` and `y`.
55+
pub let max {T, method lt : T -> T ->> Bool} (x : T) (y : T) =
56+
if y < x then x else y
57+
58+
## Returns the smaller of two elements `x` and `y`.
59+
pub let min {T, method lt : T -> T ->> Bool} (x : T) (y : T) =
60+
if y < x then y else x
61+
62+
## Concatenates list of strings in linear time.
63+
pub let strListCat = extern dbl_strListCat : List String -> String
64+
65+
## Creates a string with `n` repetitions.
66+
pub let rec replicate (s : String) (n : Int) =
67+
if n <= 0 then
68+
""
69+
else if n % 2 == 0 then
70+
(let s' = replicate s (n / 2) in
71+
s' + s')
72+
else
73+
(let s' = replicate s (n / 2) in
74+
s' + (s' + s))

test/stdlib/stdlib0001_Option.fram

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ let _ =
44
assert ((Some 42).unwrap == 42);
55
let ~onError () = 0 in
66
assert ((None).unwrapErr == 0);
7-
assert ((Some 42).unwrapErr == 42)
7+
assert ((Some 42).unwrapErr == 42);
8+
assert (Some 42 >.map (fn x => 1 + x) >.unwrapErr == 43)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# min
2+
let _ =
3+
assert (min 10 20 == 10);
4+
assert (min 20 10 == 10);
5+
assert (min 10 10 == 10);
6+
assert (min "abc" "def" == "abc")
7+
8+
# max
9+
let _ =
10+
assert (max 10 20 == 20);
11+
assert (max 20 10 == 20);
12+
assert (max 20 20 == 20);
13+
assert (max "abc" "def" == "def")
14+
15+
# strListCat
16+
let _ =
17+
assert (strListCat ["a", "b", "c"] == "abc");
18+
assert (strListCat ["do", "re", "mi"] == "doremi");
19+
assert (strListCat [] == "")
20+
21+
# replicate
22+
let _ =
23+
assert (replicate "ab" 0 == "");
24+
assert (replicate "ab" 1 == "ab");
25+
assert (replicate "ab" 2 == "abab");
26+
assert (replicate "ab" 3 == "ababab");
27+
assert (replicate "ab" 5 == "ababababab");
28+
assert (replicate "" 20 == "")

0 commit comments

Comments
 (0)