Skip to content

Commit

Permalink
Merge 74cdcdd into b16b3e1
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobsEnslaver committed Aug 7, 2020
2 parents b16b3e1 + 74cdcdd commit a9ce3fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,26 @@ With zlist you are working with potentially infinite data.
Don't use ```to_list/1```, ```foreach/2```, ```fold/3``` functions
until you know that zlist is finite state.
Use ```dropwhen/1``` before, or ```take/2``` instead.

### QLC ###

Zlists supports alternative syntax with QLC query language, example:

```
> Zs = zlist:seq(1, 20),
> Q1 = qlc:q([{C,B,A} || A <- zlist:table(Zs), B <- zlist:table(Zs), C <- zlist:table(Zs),
A =< B, A*A + B*B == C*C]),
> Q2 = qlc:sort(Q1, [{order, descending}]),
> Cursor = qlc:cursor(Q2).
> qlc:next_answers(Cursor, 2).
> [{20,16,12},{17,15,8}]
>
> qlc:next_answers(Cursor, 5). %only 4 available
> [{15,12,9}, {13,12,5},{10,8,6},{5,4,3}]
>
> qlc:next_answers(Cursor, 1). %no answers available
> []
```

Use this with care, because it's may make your lazy lists strict.
For another QLC functionality, like cache, sorting, joining etc see http://erlang.org/doc/man/qlc.html
7 changes: 6 additions & 1 deletion src/zlist.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
take_by/2,

from_list/1,
to_list/1
to_list/1,

table/1
]).

-type zlist(A) :: fun(() -> maybe_improper_list(A, zlist(A))) | empty_zlist().
Expand Down Expand Up @@ -248,3 +250,6 @@ take_by(N, Zlist) when N > 0 ->
{List, RestZ} -> [List] ++ take_by(N, RestZ)
end
end.

table(Zs) ->
qlc:table(Zs, []).
12 changes: 12 additions & 0 deletions test/zlist_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,15 @@ take_by_test() ->
GroupedZ = zlist:take_by(100, ZSeq),
[_|RestZ] = GroupedZ(),
?assertEqual([], RestZ()).

-include_lib("stdlib/include/qlc.hrl").
qlc_test() ->
Zs = zlist:seq(1, 20),
Q1 = qlc:q([{C,B,A} || A <- zlist:table(Zs), B <- zlist:table(Zs), C <- zlist:table(Zs),
A =< B, A*A + B*B == C*C]),
Q2 = qlc:sort(Q1, [{order, descending}]),
Cursor = qlc:cursor(Q2),
?assertEqual([{20,16,12},{17,15,8}], qlc:next_answers(Cursor, 2)),
?assertEqual([{15,12,9}, {13,12,5},{10,8,6},{5,4,3}], qlc:next_answers(Cursor, 5)), %only 4 available
?assertEqual([], qlc:next_answers(Cursor, 1)), %no answers
ok = qlc:delete_cursor(Cursor).

0 comments on commit a9ce3fd

Please sign in to comment.