aboutsummaryrefslogtreecommitdiff
path: root/gen/list.erl
blob: ae1ef2469e3ec7c51d42b8f2bfe5a46ec5a9f4e4 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-module(list).
-include_lib("eunit/include/eunit.hrl").

-export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, map/2, new/0, foldl/3, foldr/3]).

length(A) ->
    erlang:length(A).

-ifdef(TEST).
length_test() ->
    _ = fun(Capture1) -> expect:equal(Capture1, 0) end(length([])),
    _ = fun(Capture1) -> expect:equal(Capture1, 1) end(length([1])),
    _ = fun(Capture1) -> expect:equal(Capture1, 2) end(length([1, 1])),
    fun(Capture1) -> expect:equal(Capture1, 3) end(length([1, 1, 1])).
-endif.

reverse(A) ->
    erlang:reverse(A).

-ifdef(TEST).
reverse_test() ->
    _ = fun(Capture1) -> expect:equal(Capture1, 0) end(length([])),
    fun(Capture1) -> expect:equal(Capture1, 5) end(length([1, 2, 3, 4, 5])).
-endif.

is_empty(List) ->
    List =:= [].

-ifdef(TEST).
is_empty_test() ->
    _ = expect:true(is_empty([])),
    expect:false(is_empty([1])).
-endif.

has_member(List, Elem) ->
    case List of
        [] ->
            false;

        [Head | Rest] ->
            Head =:= Elem orelse has_member(Rest, Elem)
    end.

-ifdef(TEST).
has_member_test() ->
    _ = expect:true(has_member([0, 4, 5, 1], 1)),
    _ = expect:false(has_member([0, 4, 5, 7], 1)),
    expect:false(has_member([], 1)).
-endif.

head(List) ->
    case List of
        [] ->
            {error, empty};

        [X | Xs] ->
            {ok, X}
    end.

-ifdef(TEST).
head_test() ->
    _ = fun(Capture1) ->
        expect:equal(Capture1, {ok, 0})
    end(head([0, 4, 5, 7])),
    fun(Capture1) -> expect:equal(Capture1, {error, empty}) end(head([])).
-endif.

tail(List) ->
    case List of
        [] ->
            {error, empty};

        [X | Xs] ->
            {ok, Xs}
    end.

-ifdef(TEST).
tail_test() ->
    _ = fun(Capture1) ->
        expect:equal(Capture1, {ok, [4, 5, 7]})
    end(tail([0, 4, 5, 7])),
    _ = fun(Capture1) -> expect:equal(Capture1, {ok, []}) end(tail([0])),
    fun(Capture1) -> expect:equal(Capture1, {error, empty}) end(tail([])).
-endif.

do_map(List, Fun, Acc) ->
    case List of
        [] ->
            reverse(Acc);

        [X | Xs] ->
            do_map(Xs, Fun, [Fun(X) | Acc])
    end.

map(List, Fun) ->
    do_map(List, Fun, []).

-ifdef(TEST).
map_test() ->
    _ = fun(Capture1) ->
        expect:equal(Capture1, [])
    end(fun(Capture1) -> map(Capture1, fun(X) -> X * 2 end) end([])),
    fun(Capture1) ->
        expect:equal(Capture1, [0, 8, 10, 14, 6])
    end(fun(Capture1) ->
            map(Capture1, fun(X) -> X * 2 end)
        end([0, 4, 5, 7, 3])).
-endif.

new() ->
    [].

-ifdef(TEST).
new_test() ->
    fun(Capture1) -> expect:equal(Capture1, []) end(new()).
-endif.

foldl(List, Acc, Fun) ->
    case List of
        [] ->
            Acc;

        [X | Rest] ->
            foldl(Rest, Fun(X, Acc), Fun)
    end.

-ifdef(TEST).
foldl_test() ->
    fun(Capture1) ->
        expect:equal(Capture1, [3, 2, 1])
    end(fun(Capture1) ->
            foldl(Capture1, [], fun(X, Acc) -> [X | Acc] end)
        end([1, 2, 3])).
-endif.

foldr(List, Acc, Fun) ->
    case List of
        [] ->
            Acc;

        [X | Rest] ->
            Fun(X, foldr(Rest, Acc, Fun))
    end.

-ifdef(TEST).
foldr_test() ->
    fun(Capture1) ->
        expect:equal(Capture1, [1, 2, 3])
    end(fun(Capture1) ->
            foldr(Capture1, [], fun(X, Acc) -> [X | Acc] end)
        end([1, 2, 3])).
-endif.