[erlang-questions] specs for fixed length lists

Erik Søe Sørensen <>
Tue Jul 10 01:50:03 CEST 2012


Using setelement is a bad idea, at least unless the tuples are very small:
the cost in time and allocation is n^2.
A better scaling approach is to use tuple_to_list + map + list_to_tuple;
that should be linear, and my guess is that it'd be faster even for small
tuple sizes.
(For the actual n=4 case, I don't know which is better; must  measure...)
/Erik
Den 09/07/2012 22.54 skrev "Lukas Larsson" <>:

> 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).
>
> It's not very pretty, or efficient, but it gets the job done.
>
> Lukas
>
> On Mon, Jul 9, 2012 at 9:59 PM, Daniel Goertzen
> <> wrote:
> >
> > On Sun, Jul 8, 2012 at 1:22 AM, <> wrote:
> >>
> >> I'm with /Joe on this one.  There aren't all _that_ many
> >> list operations that make sense on tuples, and
> >>     map4(F, {A,B,C,D}) -> {F(A), F(B), F(C), F(D)}.
> >> is easy enough to define.  Perhaps the code needs turning
> >> inside out.
> >>
> >
> > Thanks for the idea; my functional instincts are still developing.  I
> will
> > go with this approach since my sequence size really is only 4.
> >
> > Dan.
> >
> > _______________________________________________
> > erlang-questions mailing list
> > 
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120710/603751ea/attachment.html>


More information about the erlang-questions mailing list