-
Notifications
You must be signed in to change notification settings - Fork 9
/
traversable.erl
124 lines (104 loc) · 3.82 KB
/
traversable.erl
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
%% @doc
%% Class of data structures that can be traversed.
-module(traversable).
-export([behaviour_info/1]).
behaviour_info(callbacks) ->
[
%%
%% take collection and return head element of collection
%%
%% -spec head(datum:traversable(_)) -> datum:option(_).
{head, 1},
%%
%% take collection and return its suffix (all elements except the first)
%%
%% -spec tail(datum:traversable(_)) -> datum:traversable(_).
{tail, 1},
%%
%% build a new collection from Erlang list
%%
%% -spec build([_]) -> datum:traversable(_).
{build, 1},
%%
%% converts the collection to Erlang list
%%
%% -spec list(datum:traversable(_)) -> [_].
{list, 1},
%%
%% return true if collection is empty
%%
%% -spec is_empty(datum:traversable(_)) -> true | false.
{is_empty, 1},
%%
%% length of the collection
%%
%% -spec length(datum:traversable(_)) -> integer().
% {length, 1},
%%
%% return the suffix of collection that starts at the next element after nth.
%% drop first n elements
%%
%% -spec drop(integer(), datum:traversable(_)) -> datum:traversable(_).
{drop, 2},
%%
%% drops elements from collection while predicate returns true and
%% returns remaining stream suffix.
%%
%% -spec dropwhile(datum:predicate(_), datum:traversable(_)) -> datum:traversable(_).
{dropwhile, 2},
%%
%% returns a newly-allocated collection that contains only those elements of the
%% input collection for which predicate is true.
%%
%% -spec filter(datum:predicate(_), datum:traversable(_)) -> datum:traversable(_).
{filter, 2},
%%
%% applies a function to each collection element for its side-effects;
%% it returns nothing.
%%
%% -spec foreach(datum:effect(_), datum:traversable(_)) -> ok.
{foreach, 2},
%%
%% build a new collection by applying a function to all elements of this collection
%% and flattering resulting collection
%%
%% -spec flatmap(fun((_) -> datum:traversable(_)), datum:traversable(_)) -> datum:traversable(_).
% {flatmap, 2},
%%
%% create a new collection by apply a function to each element of input collection.
%%
%% -spec map(fun((_) -> _), datum:traversable(_)) -> datum:traversable(_).
{map, 2},
%%
%% partition the collection in two collections according to a predicate
%%
%% -spec partition(datum:predicate(_), datum:traversable(_)) -> {datum:traversable(_), datum:traversable(_)}.
% {partition, 2},
%%
%% partitions collection into two collection. The split behaves as if it is defined as
%% consequent take(N, Seq), drop(N, Seq).
%%
%% -spec split(integer(), datum:traversable(_)) -> {datum:traversable(_), datum:traversable(_)}.
{split, 2},
%%
%% partitions stream into two streams according to predicate.
%% The splitwith/2 behaves as if it is defined as consequent
%% takewhile(Pred, Seq), dropwhile(Pred, Seq)
%%
%% -spec splitwhile(datum:predicate(_), datum:traversable(_)) -> {datum:traversable(_), datum:traversable(_)}.
{splitwhile, 2},
%%
%% returns a newly-allocated collection containing the first n elements of
%% the input collection.
%%
%% -spec take(integer(), datum:traversable(_)) -> datum:traversable(_).
{take, 2},
%%
%% returns a newly-allocated collection that contains those elements from
%% input collection while predicate returns true.
%%
%% -spec takewhile(datum:predicate(_), datum:traversable(_)) -> datum:traversable(_).
{takewhile, 2}
];
behaviour_info(_Other) ->
undefined.