[erlang-questions] specs for fixed length lists

Richard O'Keefe ok@REDACTED
Tue Jul 10 03:51:09 CEST 2012


On 10/07/2012, at 8:54 AM, Lukas Larsson wrote:

> Writing a generic tuple:map/2 would not be that hard either:
> 
> map(F, Tuple) ->
>   map(F, Tuple, size(Tuple)).
> 
> map(F,Tuple,1) ->
>   setelement(1,Tuple,F(element(1,Tuple)));
> map(F,Tuple,N) ->
>   map(F,setelement(N,Tuple,F(element(N,Tuple))),N-1).

Code golf!

tuple_map(F, Tuple) ->
    list_to_tuple([F(X) || X <- tuple_to_list(Tuple)]).


Exercise for the reader:  why is my version O(|Tuple|)
but Lukas Larsson's O(|Tuple|**2)?




More information about the erlang-questions mailing list