<commit_before>%
% List: Methods of the List builtin
% Copyright (C)2009 <NAME>
% 
% Redistribution is permitted under the MIT license.  See LICENSE for details.
%

-module('List').
-export([call/4]).
-include("../core/reia_types.hrl").
-include("../core/reia_invoke.hrl").

call(List, '[]', {Index}, _Block) ->
  if
    Index < 0 -> lists:nth(length(List) + Index + 1, List);
    true      -> lists:nth(Index + 1, List)
  end;
  
call(List, '[]=', {Index, Value}, _Block) ->
  if
    Index < 0 -> replace(List, length(List) + Index, Value);
    true      -> replace(List, Index, Value)
  end;
  
call(List, size, _Args, _Block) ->
  length(List);

call(List, to_string, _Args, _Block) ->
  #reia_string{elements=List};

call(List, to_s, _Args, _Block) ->
  stringify(List, to_s);
  
call(List, inspect, _Args, _Block) ->
  stringify(List, inspect);
  
call(List, reverse, _Args, _Block) ->
  lists:reverse(List);
  
call(List, join, {}, Block) ->
  call(List, join, {#reia_string{elements=[]}}, Block);
call(List, join, {#reia_string{elements=Separator}}, _Block) ->
  Elements = case List of
    [] -> [];
    _  -> join_list([], List, Separator)
  end,
  #reia_string{elements=Elements};
  
call(List, each, {}, Block) ->
  lists:foreach(Block, List);
  
call(List, map, {}, Block) ->
  lists:map(Block, List);
  
call(List, flatten, {}, _Block) ->
  lists:flatten(List);
  
call(List, to_list, {}, _Block) ->
  List.

replace(List, Index, Value) ->
  replace(List, 0, Index, Value).

replace([], _, _, _) ->
  throw('bad argument');
replace([_|Tail], Index, Index, Value) ->
  [Value|Tail];
replace([Head|Tail], N, Index, Value) ->
  [Head|replace(Tail, N + 1, Index, Value)].

stringify(List, Method) ->
  Res = lists:flatten(["[", string:join([convert(Elem, Method) || Elem <- List], ","), "]"]),
  call(Res, to_string, {}, nil).

convert(Value, Method) -> 
  ?invoke(?invoke(Value, Method, {}, nil), to_list, {}, nil).

join_list(Result, [Elem], _) ->
  lists:reverse([convert(Elem, to_s)|Result]);
join_list(Result, [Elem|Rest], Separator) ->
  join_list([Separator, convert(Elem, to_s)|Result], Rest, Separator).<commit_msg>Return nil from a list if there is no member for the requested index<commit_after>%
% List: Methods of the List builtin
% Copyright (C)2009 <NAME>
% 
% Redistribution is permitted under the MIT license.  See LICENSE for details.
%

-module('List').
-export([call/4]).
-include("../core/reia_types.hrl").
-include("../core/reia_invoke.hrl").

call(List, '[]', {Index}, _Block) ->
  Length = length(List),
  Index2 = if
    Index < 0 -> length(List) + Index + 1;
    true      -> Index + 1
  end,
  if
    Index2 < 1      -> nil;
    Index2 > Length -> nil;
    true            -> lists:nth(Index2, List)
  end;
  
call(List, '[]=', {Index, Value}, _Block) ->
  if
    Index < 0 -> replace(List, length(List) + Index, Value);
    true      -> replace(List, Index, Value)
  end;
  
call(List, size, _Args, _Block) ->
  length(List);

call(List, to_string, _Args, _Block) ->
  #reia_string{elements=List};

call(List, to_s, _Args, _Block) ->
  stringify(List, to_s);
  
call(List, inspect, _Args, _Block) ->
  stringify(List, inspect);
  
call(List, reverse, _Args, _Block) ->
  lists:reverse(List);
  
call(List, join, {}, Block) ->
  call(List, join, {#reia_string{elements=[]}}, Block);
call(List, join, {#reia_string{elements=Separator}}, _Block) ->
  Elements = case List of
    [] -> [];
    _  -> join_list([], List, Separator)
  end,
  #reia_string{elements=Elements};
  
call(List, each, {}, Block) ->
  lists:foreach(Block, List);
  
call(List, map, {}, Block) ->
  lists:map(Block, List);
  
call(List, flatten, {}, _Block) ->
  lists:flatten(List);
  
call(List, to_list, {}, _Block) ->
  List.

replace(List, Index, Value) ->
  replace(List, 0, Index, Value).

replace([], _, _, _) ->
  throw('bad argument');
replace([_|Tail], Index, Index, Value) ->
  [Value|Tail];
replace([Head|Tail], N, Index, Value) ->
  [Head|replace(Tail, N + 1, Index, Value)].

stringify(List, Method) ->
  Res = lists:flatten(["[", string:join([convert(Elem, Method) || Elem <- List], ","), "]"]),
  call(Res, to_string, {}, nil).

convert(Value, Method) -> 
  ?invoke(?invoke(Value, Method, {}, nil), to_list, {}, nil).

join_list(Result, [Elem], _) ->
  lists:reverse([convert(Elem, to_s)|Result]);
join_list(Result, [Elem|Rest], Separator) ->
  join_list([Separator, convert(Elem, to_s)|Result], Rest, Separator).
{'fraction_non_alphanumeric': 0.2037871956717764, 'fraction_numerical': 0.006086564472497746, 'mean_word_length': 3.9081858407079646, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5357141', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1616, 'n_words': 374}