Skip to content

Instantly share code, notes, and snippets.

@schacon
Created September 16, 2009 19:48
Show Gist options
  • Save schacon/188162 to your computer and use it in GitHub Desktop.
Save schacon/188162 to your computer and use it in GitHub Desktop.
-module(list_utils).
-export([split_list/2]).
split_list(List, Splits) ->
case Splits > 1 of
true -> Each = round(length(List) / Splits),
list_range([], List, Each);
false -> List
end.
list_range([], List, Each) ->
Len = length(List),
case Len > Each of
true -> {SubList, Rest} = lists:split(Each, List),
list_range([SubList], Rest, Each);
false -> [List]
end;
list_range(Final, [], _Each) ->
Final;
list_range(Final, List, Each) ->
Len = length(List),
case Len > Each of
true -> {SubList, Rest} = lists:split(Each, List),
list_range([SubList|Final], Rest, Each);
false -> [List|Final]
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment