PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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