/modules/mod_base/filters/filter_chunk.erl

http://github.com/zotonic/zotonic · Erlang · 39 lines · 17 code · 6 blank · 16 comment · 0 complexity · 1f564e2583026aaee589917fc7d1a50a MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2010 Marc Worrell
  3. %% @doc 'chunk' filter, split a list in sublists of a maximum length
  4. %% Copyright 2010 Marc Worrell
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(filter_chunk).
  18. -export([chunk/3]).
  19. chunk(undefined, _N, _Context) ->
  20. undefined;
  21. chunk(In, N, Context) ->
  22. chunk1(z_template_compiler_runtime:to_list(In, Context), N, []).
  23. chunk1([], _, Acc) ->
  24. lists:reverse(Acc);
  25. chunk1(List, N, Acc) ->
  26. {Chunk, List1} = chunk_take(List, N, []),
  27. chunk1(List1, N, [Chunk|Acc]).
  28. chunk_take([], _, Acc) ->
  29. {lists:reverse(Acc), []};
  30. chunk_take(L, 0, Acc) ->
  31. {lists:reverse(Acc), L};
  32. chunk_take([H|T], N, Acc) ->
  33. chunk_take(T, N-1, [H|Acc]).