Skip to content

Commit 27fec21

Browse files
committed
stdlib additions
1 parent 6af3f43 commit 27fec21

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

gen/list.erl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-compile(no_auto_import).
33
-include_lib("eunit/include/eunit.hrl").
44

5-
-export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, filter/2, map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, foldl/3, foldr/3]).
5+
-export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, filter/2, map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, foldl/3, foldr/3, find/2]).
66

77
length(A) ->
88
erlang:length(A).
@@ -264,3 +264,32 @@ foldr(List, Acc, Fun) ->
264264
foldr_test() ->
265265
expect:equal(foldr([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end), [1, 2, 3]).
266266
-endif.
267+
268+
find(Haystack, F) ->
269+
case Haystack of
270+
[] ->
271+
{error, not_found};
272+
273+
[X | Rest] ->
274+
case F(X) of
275+
{ok, X1} ->
276+
{ok, X1};
277+
278+
_ ->
279+
find(Rest, F)
280+
end
281+
end.
282+
283+
-ifdef(TEST).
284+
find_test() ->
285+
F = fun(X) -> case X of
286+
2 ->
287+
{ok, 4};
288+
289+
_ ->
290+
{error, not_found}
291+
end end,
292+
expect:equal(find([1, 2, 3], F), {ok, 4}),
293+
expect:equal(find([1, 3, 2], F), {ok, 4}),
294+
expect:equal(find([1, 3], F), {error, not_found}).
295+
-endif.

gen/tuple.erl

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
-compile(no_auto_import).
33
-include_lib("eunit/include/eunit.hrl").
44

5-
-export([first/1, second/1]).
5+
-export([new/2, first/1, second/1, swap/1, fetch/2]).
6+
7+
new(A, B) ->
8+
{A, B}.
9+
10+
-ifdef(TEST).
11+
new_test() ->
12+
expect:equal(new(1, 2), {1, 2}),
13+
expect:equal(new(2, <<"3">>), {2, <<"3">>}).
14+
-endif.
615

716
first(Tup) ->
817
{A, _} = Tup,
@@ -21,3 +30,29 @@ second(Tup) ->
2130
second_test() ->
2231
expect:equal(second({1, 2}), 2).
2332
-endif.
33+
34+
swap(Tup) ->
35+
{A, B} = Tup,
36+
{B, A}.
37+
38+
-ifdef(TEST).
39+
swap_test() ->
40+
expect:equal(swap({1, <<"2">>}), {<<"2">>, 1}).
41+
-endif.
42+
43+
fetch(Haystack, Needle) ->
44+
list:find(Haystack, fun(Tuple) -> case first(Tuple) =:= Needle of
45+
true ->
46+
{ok, second(Tuple)};
47+
48+
false ->
49+
{error, []}
50+
end end).
51+
52+
-ifdef(TEST).
53+
fetch_test() ->
54+
Proplist = [{0, <<"1">>}, {1, <<"2">>}],
55+
expect:equal(fetch(Proplist, 0), {ok, <<"1">>}),
56+
expect:equal(fetch(Proplist, 1), {ok, <<"2">>}),
57+
expect:is_error(fetch(Proplist, 2)).
58+
-endif.

src/list.gleam

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import expect
33
pub enum Empty =
44
| Empty
55

6+
pub enum NotFound =
7+
| NotFound
8+
69
// Using the Erlang C BIF implementation.
710
//
811
pub external fn length(List(a)) -> Int = "erlang" "length"
@@ -279,3 +282,35 @@ test foldr {
279282
|> foldr(_, [], fn(x, acc) { [x | acc] })
280283
|> expect:equal(_, [1, 2, 3])
281284
}
285+
286+
pub fn find(haystack, f) {
287+
case haystack {
288+
| [] -> Error(NotFound)
289+
| [x | rest] ->
290+
case f(x) {
291+
| Ok(x) -> Ok(x)
292+
| _ -> find(rest, f)
293+
}
294+
}
295+
}
296+
297+
test find {
298+
let f = fn(x) {
299+
case x {
300+
| 2 -> Ok(4)
301+
| _ -> Error(NotFound)
302+
}
303+
}
304+
305+
[1, 2, 3]
306+
|> find(_, f)
307+
|> expect:equal(_, Ok(4))
308+
309+
[1, 3, 2]
310+
|> find(_, f)
311+
|> expect:equal(_, Ok(4))
312+
313+
[1, 3]
314+
|> find(_, f)
315+
|> expect:equal(_, Error(NotFound))
316+
}

src/tuple.gleam

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
import expect
2+
import result
3+
import list
4+
5+
pub fn new(a, b) {
6+
{a, b}
7+
}
8+
9+
test new {
10+
new(1, 2)
11+
|> expect:equal(_, {1, 2})
12+
13+
new(2, "3")
14+
|> expect:equal(_, {2, "3"})
15+
}
216

317
pub fn first(tup) {
418
let {a, _} = tup
@@ -21,3 +35,39 @@ test second {
2135
|> second
2236
|> expect:equal(_, 2)
2337
}
38+
39+
pub fn swap(tup) {
40+
let {a, b} = tup
41+
{b, a}
42+
}
43+
44+
test swap {
45+
{1, "2"}
46+
|> swap
47+
|> expect:equal(_, {"2", 1})
48+
}
49+
50+
pub fn fetch(haystack, needle) {
51+
list:find(haystack, fn(tuple) {
52+
case first(tuple) == needle {
53+
| True -> Ok(second(tuple))
54+
| False -> Error([])
55+
}
56+
})
57+
}
58+
59+
test fetch {
60+
let proplist = [{0, "1"}, {1, "2"}]
61+
62+
proplist
63+
|> fetch(_, 0)
64+
|> expect:equal(_, Ok("1"))
65+
66+
proplist
67+
|> fetch(_, 1)
68+
|> expect:equal(_, Ok("2"))
69+
70+
proplist
71+
|> fetch(_, 2)
72+
|> expect:is_error
73+
}

0 commit comments

Comments
 (0)