Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Kozlov Yakov committed Feb 28, 2016
0 parents commit 6f9a847
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
logs
_build
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: erlang
otp_release:
- 18.2.1
- 17.5
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2016 Yakov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

68 changes: 68 additions & 0 deletions README.md
@@ -0,0 +1,68 @@
# Erlang zlist: a lazy sequences library.
----------------------------------------------------

## Description ##

Unlike https://github.com/vjache/erlang-zlists
zlist() is a function that returns improper list with data and next zlist or
an empty list.
You can use it to organize lazy evolution or backpressure.

Simple infinite iterator:

```erlang
1> SimpleZ = fun Loop() -> [1|Loop] end.
#Fun<erl_eval.44.90072148>

2> zlist:take(10, SimpleZ).
{[1,1,1,1,1,1,1,1,1,1],#Fun<erl_eval.62.90072148>}
```

## Usage ##

### Integrate to your project ###

This is a rebar'ized project, so, if you are already using rebar, just insert a reference
to this git repo at your rebar.config.

### Examples ###

```erlang
1> Z = zlist:recurrent(fun(A) -> A + 1 end, 0).
#Fun<zlist.14.21462417>

2> zlist:take(10, Z).
{[1,2,3,4,5,6,7,8,9,10],#Fun<zlist.14.21462417>}

3> Z2 = zlist:map(fun(A) -> A * 2 end, Z).
#Fun<zlist.3.21462417>

4> zlist:take(10, Z2).
{[2,4,6,8,10,12,14,16,18,20],#Fun<zlist.3.21462417>}

5> ZL = zlist:from_list([1,2,5]).
#Fun<zlist.12.21462417>

6> zlist:to_list(ZL).
[1,2,5]

7> ZR = zlist:ciclyc(ZL).
#Fun<zlist.17.21462417>

8> zlist:take(10, ZR).
{[1,2,5,1,2,5,1,2,5,1],#Fun<zlist.17.21462417>}

9> Z3 = zlist:flatmap(fun(A) -> zlist:from_list([-A, A]) end, Z2).
#Fun<zlist.6.21462417>

10> zlist:take(10, Z3).
{[-2,2,-4,4,-6,6,-8,8,-10,10],#Fun<zlist.10.21462417>}

```

### Warnings !!! ###

With zlist you are working with potentially infinite data.
Don't use ```to_list/1```, ```foreach/2```, ```fold/3``` functions
until you know that iterator is finite state.
Use ```dropwhen/1``` before, or ```take/2``` instead.
15 changes: 15 additions & 0 deletions src/zlist.app.src
@@ -0,0 +1,15 @@
{application, zlist,
[{description, "Erlang lazy list library"},
{vsn, git},
{registered, []},
{applications,
[kernel,
stdlib
]},
{env,[]},
{modules, []},

{maintainers, ["Yakov Kozlov"]},
{licenses, ["MIT"]},
{links, [{"Github", "https://github.com/egobrain/zlist"}]}
]}.
246 changes: 246 additions & 0 deletions src/zlist.erl
@@ -0,0 +1,246 @@
-module(zlist).

-export([
map/2,
filter/2,
filtermap/2,
flatmap/2,
over/3,
dropwhen/2,
dropwhile/2,

append/2,
ciclyc/1,

empty/0,
seq/2,
seq/3,
recurrent/2,

foreach/2,
fold/3,
take/2,
takewhile/2,

from_list/1,
to_list/1
]).

-type zlist(A) :: fun(() -> maybe_improper_list(A, zlist(A))) | empty_zlist().
-type empty_zlist() :: fun(() -> []).

-export_type([
zlist/1,
empty_zlist/0
]).

%% =============================================================================
%% API functions
%% =============================================================================

-spec empty() -> empty_zlist().
empty() -> fun() -> [] end.

-spec seq(From, To) -> zlist(integer()) when
From :: integer(),
To :: integer().
seq(First, Last) when is_integer(First), is_integer(Last), First-1 =< Last ->
seq_(First, Last).

seq_(Curr, Last) ->
fun() ->
case Curr > Last of
true -> [];
false -> [Curr] ++ seq_(Curr+1, Last)
end
end.

-spec seq(From, To, Incr) -> zlist(integer()) when
From :: integer(),
To :: integer(),
Incr :: integer().
seq(First, Last, Inc) when is_integer(First), is_integer(Last), is_integer(Inc) ->
if
Inc > 0, First - Inc =< Last;
Inc < 0, First - Inc >= Last ->
N = (Last - First + Inc) div Inc,
seq_(N, First, Inc);
Inc =:= 0, First =:= Last ->
seq_(1, First, Inc)
end.

seq_(N, X, D) ->
fun() ->
case N of
0 -> [];
_ -> [X] ++ seq_(N-1, X+D, D)
end
end.

-spec map(fun((A) -> B), zlist(A)) -> zlist(B).
map(Fun, Zlist) ->
fun() ->
case Zlist() of
[Data|Next] ->
[Fun(Data)] ++ map(Fun, Next);
Done -> Done
end
end.

-spec foreach(fun((A) -> ok), zlist(A)) -> ok.
foreach(Fun, Zlist) ->
case Zlist() of
[Data|Next] ->
_ = Fun(Data),
foreach(Fun, Next);
_Done -> ok
end.

-spec filter(fun((A) -> boolean()), zlist(A)) -> zlist(A).
filter(Fun, Zlist) ->
fun() ->
(fun Loop(Z) ->
case Z() of
[Data|Next] ->
case Fun(Data) of
true -> [Data] ++ filter(Fun, Next);
false -> Loop(Next)
end;
Done -> Done
end
end)(Zlist)
end.

-spec filtermap(fun((A) -> {true, B} | false), zlist(A)) -> zlist(B).
filtermap(Fun, Zlist) ->
fun() ->
(fun Loop(Z) ->
case Z() of
[Data|Next] ->
case Fun(Data) of
{true, Data2} -> [Data2] ++ filtermap(Fun, Next);
false -> Loop(Next)
end;
Done -> Done
end
end)(Zlist)
end.

-spec fold(fun((A, S) -> S), S, zlist(A)) -> S.
fold(Fun, State, Zlist) ->
case Zlist() of
[Data|Next] ->
fold(Fun, Fun(Data, State), Next);
_Done -> State
end.

-spec flatmap(fun((A) -> zlist(B)), zlist(A)) -> zlist(B).
flatmap(Fun, Zlist) ->
fun() ->
case Zlist() of
[Data|Next] ->
(append(Fun(Data), flatmap(Fun, Next)))();
Done -> Done
end
end.

-spec over(fun((A,S) -> {B, S}), S, zlist(A)) -> zlist(B).
over(Fun, S, Zlist) ->
fun() ->
case Zlist() of
[Data|Next] ->
{Value, S2} = Fun(Data, S),
[Value] ++ over(Fun, S2, Next);
Done -> Done
end
end.

-spec dropwhen(fun((A) -> boolean()), zlist(A)) -> zlist(A).
dropwhen(Fun, Zlist) ->
fun() ->
case Zlist() of
[Data|Next] ->
case Fun(Data) of
true -> [];
false -> [Data] ++ dropwhen(Fun, Next)
end;
Done -> Done
end
end.

-spec dropwhile(fun((A) -> boolean()), zlist(A)) -> zlist(A).
dropwhile(Fun, Zlist) ->
fun() ->
(fun Loop(Z) ->
case Z() of
[Data|Next]=R ->
case Fun(Data) of
true -> Loop(Next);
false -> R
end;
Done -> Done

end
end)(Zlist)
end.

-spec append(zlist(A), zlist(B)) -> zlist(A|B).
append(Zlist1, Zlist2) ->
fun() ->
case Zlist1() of
[Data|Next] -> [Data] ++ append(Next, Zlist2);
_Done -> Zlist2()
end
end.

-spec ciclyc(zlist(A)) -> zlist(A).
ciclyc(Zlist) ->
(fun Loop(Z) ->
fun() ->
case Z() of
[Data|Next] -> [Data] ++ Loop(Next);
_Done -> (ciclyc(Zlist))()
end
end
end)(Zlist).

-spec from_list([A]) -> zlist(A).
from_list(List) ->
fun() ->
case List of
[H|T] -> [H] ++ from_list(T);
_ -> List
end
end.

-spec to_list(zlist(A)) -> [A].
to_list(Zlist) -> lists:reverse(fold(fun(H, T) -> [H|T] end, [], Zlist)).

-spec recurrent(fun((A) -> A), A) -> zlist(A).
recurrent(Fun, S) ->
fun() ->
Next = Fun(S),
[Next] ++ recurrent(Fun, Next)
end.

-spec take(N :: pos_integer(), zlist(A)) -> {[A], zlist(A)}.
take(N, Zlist) when N > 0 ->
take_(N, [], Zlist).
take_(0, Acc, Z) -> {lists:reverse(Acc), Z};
take_(C, Acc, Z) ->
case Z() of
[Data|Next] -> take_(C-1, [Data|Acc], Next);
_Done -> {lists:reverse(Acc), empty()}
end.

-spec takewhile(fun((A) -> boolean()), zlist(A)) -> {[A], zlist(A)}.
takewhile(Fun, Zlist) -> takewhile_(Fun, [], Zlist).
takewhile_(Fun, Acc, Z) ->
case Z() of
[Data|Next] = R ->
case Fun(Data) of
true -> takewhile_(Fun, [Data|Acc], Next);
false -> {lists:reverse(Acc), fun() -> R end}
end;
_Done -> {lists:reverse(Acc), empty()}
end.
1 change: 1 addition & 0 deletions test/rebar.lock
@@ -0,0 +1 @@
[].

0 comments on commit 6f9a847

Please sign in to comment.