public
Description: Haskell's Data.List functions for Erlang. That was the idea anyway. Good ideas from other languages will be shamelessly stolen too.
Homepage: http://12monkeys.co.uk
Clone URL: git://github.com/hassy/hslists.git
More functions and more tests.
hassy (author)
Thu Apr 10 10:18:05 -0700 2008
commit  401bad831c782a4c34cebc360e3ad626703408e3
tree    f03b3df1abb8a0b6508ac8288c6b6fcd60df1cf3
parent  a0fef4a6a834c3a037d46fe8729a4b18a9163b5f
...
4
5
6
7
 
 
8
9
10
11
12
13
14
15
 
 
 
16
17
18
19
20
21
22
 
 
 
 
23
24
25
...
63
64
65
66
67
68
69
70
...
79
80
81
82
 
83
84
85
...
98
99
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
4
5
6
 
7
8
9
 
 
 
 
 
 
 
10
11
12
13
14
 
 
15
16
17
18
19
20
21
22
23
24
...
62
63
64
 
 
65
66
67
...
76
77
78
 
79
80
81
82
...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
0
@@ -4,22 +4,21 @@
0
 
0
 -module(hslists).
0
 -import(lists, [all/2, append/1, duplicate/2, flatten/1, foldl/3, foreach/2,
0
- map/2, nth/2, reverse/1, seq/2, sum/1, zipwith/3]).
0
+ map/2, nth/2, nthtail/2, reverse/1, seq/2, sum/1, zip/2,
0
+ zipwith/3]).
0
 
0
--export([init/1, random/1]).
0
--export([intersperse/2, join/2, intercalate/2, transpose/1]).
0
--export([product/1, average/1]).
0
--export([loop/2]).
0
--export([shuffle/1]).
0
-
0
-%%-compile(export_all).
0
+-export([init/1, mid/1, random/1, intersperse/2, join/2, intercalate/2,
0
+ transpose/1, product/1, average/1, loop/2, shuffle/1, uniq/1,
0
+ enumerate/1, drop/2]).
0
 
0
 %% @doc Returns all the elements of a list except the last one.
0
-init([]) ->
0
- undefined;
0
 init(L) ->
0
     reverse(tl(reverse(L))).
0
 
0
+%% @doc Returns list without the first and the last element.
0
+mid(L) when length(L) > 1 ->
0
+ reverse(tl(reverse(tl(L)))).
0
+
0
 %% @doc Takes an element and a list and intersperses that element between
0
 %% the elements of the list.
0
 intersperse(_Elt, []) ->
0
@@ -63,8 +62,6 @@ product([Hd | Tl]) ->
0
     foldl(fun(X, Acc) -> X * Acc end,
0
           Hd, Tl).
0
 
0
-average([]) ->
0
- undefined;
0
 average(L) ->
0
     sum(L) / length(L).
0
 
0
@@ -79,7 +76,7 @@ loop(Fun, L) when is_list(L) ->
0
 loop1(_Fun, Curr, N, Acc) when Curr > N ->
0
     reverse(Acc);
0
 loop1(Fun, Curr, N, Acc) ->
0
- loop1(Fun, Curr + 1, N, [Fun(N) | Acc]).
0
+ loop1(Fun, Curr + 1, N, [Fun(Curr) | Acc]).
0
 
0
 loop2(_Fun, [], _Curr, _N, Acc) ->
0
     reverse(Acc);
0
@@ -98,3 +95,20 @@ shuffle1([], Acc) ->
0
 shuffle1(L, Acc) ->
0
     RandElt = random(L),
0
     shuffle1(L -- [RandElt], [RandElt | Acc]).
0
+
0
+%% @doc Return a copy of list with duplicates removed.
0
+uniq([Hd | Tl]) ->
0
+ [ Hd | uniq([Y || Y <- Tl, Y =/= Hd])];
0
+uniq([]) ->
0
+ [].
0
+
0
+%% @doc Return list with sequence numbers attached to elements.
0
+enumerate(L) ->
0
+ zip(L, seq(1, length(L))).
0
+
0
+%% @doc Return list with first N elements dropped. If N is negative, then
0
+%% elements are dropped from the end of the list.
0
+drop(L, N) when N > 0 ->
0
+ nthtail(N, L);
0
+drop(L, N) when N < 0 ->
0
+ reverse(nthtail(N * -1, reverse(L))).
...
1
 
2
3
4
 
 
 
5
6
7
...
10
11
12
 
 
 
 
 
 
 
13
14
15
16
 
 
17
18
19
20
21
22
23
24
25
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
 
1
2
3
 
4
5
6
7
8
9
...
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
0
@@ -1,7 +1,9 @@
0
--module(test_hslist).
0
+-module(test_hslists).
0
 -include_lib("eunit/include/eunit.hrl").
0
 
0
--import(hslists, [init/1, intersperse/2, intercalate/2, transpose/1]).
0
+-import(hslists, [init/1, mid/1, random/1, intersperse/2, join/2, intercalate/2,
0
+ transpose/1, product/1, average/1, loop/2, shuffle/1, uniq/1,
0
+ enumerate/1, drop/2]).
0
 
0
 init_test_() ->
0
     [
0
@@ -10,17 +12,64 @@ init_test_() ->
0
      ?_assert(init([[1], [a], ["string"]]) == [[1], [a]])
0
     ].
0
 
0
+mid_test_() ->
0
+ [
0
+ ?_assert(mid([1, 2]) == []),
0
+ ?_assert(mid([1, 2, 3]) == [2]),
0
+ ?_assert(mid(["hello", "world", a, b, c]) == ["world", a, b])
0
+ ].
0
+
0
 intersperse_test_() ->
0
     [
0
      ?_assert(intersperse(x, [1]) == [1]),
0
- ?_assert(intersperse(x, [1, 2, 3]) == [1, x, 2, x, 3])
0
+ ?_assert(intersperse(x, [1, 2, 3]) == [1, x, 2, x, 3]),
0
+ ?_assert(intersperse(x, []) == [])
0
     ].
0
 
0
-%% TODO: Tests for intercalate.
0
-
0
 transpose_test_() ->
0
     [
0
      ?_assert(transpose([[1]]) == [[1]]),
0
      ?_assert(transpose([[1,2,3],[4,5,6]]) == [[1,4],[2,5],[3,6]]),
0
      ?_assert(transpose([[a,b,c],[1,2,3],[4,5,6]]) == [[a,1,4],[b,2,5],[c,3,6]])
0
     ].
0
+
0
+product_test_() ->
0
+ [
0
+ ?_assert(product([0]) == 0),
0
+ ?_assert(product([1, 2, 3]) == 6),
0
+ ?_assert(product([111, 222, 333, 0, 444]) == 0)
0
+ ].
0
+
0
+average_test_() ->
0
+ [
0
+ ?_assert(average([0]) == 0),
0
+ ?_assert(average([0,1,2,3,4]) == 2)
0
+ ].
0
+
0
+loop_test_() ->
0
+ [
0
+ ?_assert(loop(fun(N) -> 96 + N end, 10) == "abcdefghij"),
0
+ ?_assert(loop(fun(X, Idx) -> {X, Idx} end,
0
+ ["hello", "world"]) == [{"hello", 1}, {"world", 2}])
0
+ ].
0
+
0
+uniq_test_() ->
0
+ [
0
+ ?_assert(uniq([x]) == [x]),
0
+ ?_assert(uniq([x, 1, 2, "hello world"]) == [x, 1, 2, "hello world"]),
0
+ ?_assert(uniq([x, 1, 2, "hello world", 2, 1]) ==
0
+ [x, 1, 2, "hello world"])
0
+ ].
0
+
0
+enumerate_test_() ->
0
+ [
0
+ ?_assert(enumerate([a, b, c]) == [{a, 1}, {b, 2}, {c, 3}])
0
+ ].
0
+
0
+drop_test_() ->
0
+ [
0
+ ?_assert(drop([1, 2, 3, 4, 5], 2) == [3, 4, 5]),
0
+ ?_assert(drop([a, b, c], 3) == []),
0
+ ?_assert(drop([a, b, c, d, e], -2) == [a, b, c]),
0
+ ?_assert(drop([a, b, c, d, e], -5) == [])
0
+ ].

Comments

    No one has commented yet.