[erlang-questions] [erlang-question] How to comprehend two lists synchronously?

Ladislav Lenart lenartlad@REDACTED
Fri Nov 18 11:02:22 CET 2011


Hello.

I add only one or two notes/ideas...

I think the Fun application should look like:

     Value = Fun(lists:reverse(HeadElements)).

to preserve the order of the Fun arguments.

You can also change it to:

     apply(Fun, lists:reverse(HeadElements)).

You will then be able to use it like this:

     some_fun(A, B, C) -> ...

     some_mod:mapn(fun some_fun/3, [L1, L2, L3]).

instead of the current form:

     some_fun([A, B, C]) -> ...

     some_mod:mapn(fun some_fun/1, [L1, L2, L3])

But this is just a cosmetic issue which comes with
a performance penalty.


HTH,

Ladislav Lenart


On 18.11.2011 07:45, Ryan Huffman wrote:
> If you wanted to be able to do n-lists I don't think there is a
> "pretty" way to do it.  You could modify Dmitry's suggestion:
>
> mapn(Fun, Lists) ->
>      mapn(Fun, Lists, []).
>
> mapn(_Fun, [[] | _], MappedList) ->
>      lists:reverse(MappedList);
> mapn(Fun, Lists, MappedList) ->
>      {HeadElements, RestLists} = lists:foldl(
>          fun([H | Rest], {HeadElements, RestLists}) ->
>                  {[H | HeadElements], [Rest | RestLists]}
>          end, {[], []}, Lists),
>      Value = Fun(HeadElements),
>      mapn(Fun, lists:reverse(RestLists), [Value | MappedList]).
>
> and call it like so:
>
> mapn(fun(L) ->  lists:sum(L) end, [[1,2,3],[4,5,6],[7,8,9]]).
>
> Ryan
>
> On Thu, Nov 17, 2011 at 10:38 PM, Barco You<barcojie@REDACTED>  wrote:
>> Hi Dmitry,
>> What your suggested can really solve my problem, but it's not
>> Tail-Recursion. The tail-recursed solution should look like this;
>> map2(_Fun, [], []) ->
>>     [];
>> map2(Fun, L1, L2) ->
>>     map2(Fun, L1, L2, []).
>> map2(_Fun, [], [], L) ->
>>     lists:reverse(L);
>> map2(Fun, [H1 | T1], [H2 | T2], L) ->
>>     map2(Fun, T1, T2, [Fun(H1, H2) | L]).
>>
>> However, I'm still disappointed with the list comprehension which is
>> different from what I intuitively imagine about it.
>>
>> Regards,
>> Barco
>> On Fri, Nov 18, 2011 at 1:49 PM, Dmitry Demeshchuk<demeshchuk@REDACTED>
>> wrote:
>>>
>>> My guess is you have to zip them together, or just write a
>>> tail-recursed function:
>>>
>>> map2(Fun, [H1 | T1], [H2 | T2]) ->
>>>     [Fun(H1, H2) | map2(Fun, T1, T2)];
>>> map2(Fun, [], []) ->
>>>     [].
>>>
>>> The second option definitely isn't a list comprehension, but it
>>> requires less memory and has lesser complexity.
>>>
>>> On Fri, Nov 18, 2011 at 9:45 AM, Barco You<barcojie@REDACTED>  wrote:
>>>> Dear Erlangers,
>>>>
>>>> I hope to get a list from two lists like this:
>>>> [{a1,b1}, {a2,b2}, {a3,b3}]<-     [a1, a2 a3],  [b1, b2, b3].
>>>> But if I use list comprehension, I got:
>>>> 10>    [{D1,D2} ||  D1<- [a1,a2,a3], D2<- [b1,b2,b3]].
>>>> [{a1,b1},
>>>>   {a1,b2},
>>>>   {a1,b3},
>>>>   {a2,b1},
>>>>   {a2,b2},
>>>>   {a2,b3},
>>>>   {a3,b1},
>>>>   {a3,b2},
>>>>   {a3,b3}]
>>>>
>>>> So, my questions is how to comprehend list in synchronous way in order
>>>> to
>>>> get what I want, rather than to compose the elements from two lists in
>>>> all
>>>> possible situations.
>>>>
>>>> Thank you,
>>>> Barco
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> erlang-questions@REDACTED
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Best regards,
>>> Dmitry Demeshchuk
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>





More information about the erlang-questions mailing list