Erlang un-zip-flatten - erlang

I have a list of items that I would like to "un-zip-flatten". Basically what that means is that if I have a list of items:
[a, b, c, d, e, f, g]
I want to turn it into a list of lists like the following:
[[a, d, g], [b, e], [c, f]]
So far my solution looks like this:
unzipflatten(NumberOfLists, List) ->
lists:map(fun(Start) ->
lists:map(fun(N) ->
lists:nth(N, List)
end,
lists:seq(Start, length(List), NumberOfLists))
end,
lists:seq(1, NumberOfLists)).
I'm pretty new to Erlang so I'm wondering if I've missed some standard library function that would do what I want, or if there's a more "Erlangish" way to do this, or if the performance of my above solution will stink.

I think this would be a more "Erlangish" method to do this. Basically you would create the list of lists that will be your result, and use two lists to manage those lists like a queue. The "Heads" list contains the lists that you will prepend to next, and the "Tails" list are the ones most recently prepended to. When Heads is empty you simply reverse Tails and use that as the new Heads. Before returning the result you will need to reverse all of the lists inside Tails and Heads and then append Heads as-is to the reversed Tails. Excuse the confusing variable names, I think coming up with several good names for breaking up lists in an Erlang program is the hardest part ;)
unzipflatten(NumberOfLists, List) when NumberOfLists > 0 ->
unzipflatten(List, lists:duplicate(NumberOfLists, []), []).
unzipflatten([], Heads, Tails) ->
[lists:reverse(L) || L <- lists:reverse(Tails, Heads)];
unzipflatten(L, [], Tails) ->
unzipflatten(L, lists:reverse(Tails), []);
unzipflatten([Elem | Rest], [Head | Tail], Tails) ->
unzipflatten(Rest, Tail, [[Elem | Head] | Tails]).
It's also possible to do the "unzip" phase in a non tail-recursive way to avoid the lists:reverse step, but that is a more complicated solution. Something like this:
unzipflatten(NumberOfLists, List) when NumberOfLists > 0 ->
unzipflatten({List, lists:duplicate(NumberOfLists, [])}).
unzipflatten({[], Heads}) ->
[lists:reverse(L) || L <- Heads];
unzipflatten({L, Heads}) ->
unzipflatten(unzipper({L, Heads})).
unzipper({[], Heads}) ->
{[], Heads};
unzipper({L, []}) ->
{L, []};
unzipper({[H | T], [Head | Tail]}) ->
{T1, Tail1} = unzipper({T, Tail}),
{T1, [[H | Head] | Tail1]}.

Yes, performance will stink (basic advice for using lists:nth: never call it several times with growing N!). Something like this should be better (not tested):
unzipflatten(NumberOfLists, List) ->
unzipflatten(NumberOfLists, List, array:new(NumberOfLists, {default, []}), 0).
unzipflatten(_, [], Lists, _) ->
lists:map(fun lists:reverse/1, array:to_list(Lists));
unzipflatten(NumberOfLists, [H | T], Lists, CurrentIndex) ->
NewLists = array:set(CurrentIndex, [H | array:get(CurrentIndex, Lists)], Lists),
unzipflatten(NumberOfLists, T, NewLists, (CurrentIndex + 1) rem NumberOfLists).

Related

Erlang: serial implementation of accumulator

I am trying to create a method that takes an associative and commutative operator, as well a list of values, and then returns the answer by applying an operator to the values in the list.
The following two examples represent what the input/output are supposed to look like.
Example 1
Input: sum(fun(A,B) -> A+B end, [2,6,7,10,12]).
Output: 37
Example 2
Input: sum(fun (A,B) -> A++B end , ["C", "D", "E"]).
Output: "CDE"
This is the code I am working with so far.
-module(tester).
-compile(export_all).
sum(Func, Data, Acc) ->
lists:foldr(Func, Acc, Data).
This code produces the correct result, however, there are two problems I am trying to figure out how to approach answering.
(1) In order for this code to work, it requires an empty list to be included at the end of the command line statements. In other words, if I enter the input above (as in the examples), it will err out, because I did not write it in the following way:
12> tester:sum(fun(X, Acc) -> X+Acc end, [2,6,7,10,12], 0).
How would I implement this without an empty list as in the examples above and get the same result?
(2) Also, how would the code be implemented without the list function, or in an even more serial way?
How would I implement this without an empty list as in the examples above and get the same result?
Assuming the list always has one element (you can't really do it without this assumption), you can extract the first element from the list and pass that as the initial accumulator. You'll need to switch to foldl to do this efficiently. (With foldr you'll essentially need to make a copy of the list to drop the last element.)
sum(Func, [X | Xs]) ->
lists:foldl(fun (A, B) -> Func(B, A) end, X, Xs).
1> a:sum(fun(A,B) -> A+B end, [2,6,7,10,12]).
37
2> a:sum(fun (A,B) -> A++B end , ["C", "D", "E"]).
"CDE"
Also, how would the code be implemented without the list function, or in an even more serial way?
Here's a simple implementation using recursion and pattern matching:
sum2(Func, [X | Xs]) ->
sum2(Func, Xs, X).
sum2(Func, [], Acc) ->
Acc;
sum2(Func, [X | Xs], Acc) ->
sum2(Func, Xs, Func(Acc, X)).
We define two versions of the function. The first one extracts the head and uses that as the initial accumulator. The second one, with arity 3, does essentially what the fold functions in lists do.
After working on this for a while, this was my solution. I've left some comments about the general idea of what I did, but there's a lot more to be said.
-module(erlang2).
-compile(export_all).
-export([reduce/2]).
reduce(Func, List) ->
reduce(root, Func, List).
%When done send results to Parent
reduce(Parent, _, [A]) ->
%send to parent
Parent ! { self(), A};
%I tried this at first to take care of one el in list, but it didn't work
%length ([]) ->
% Parent ! {self(), A};
%get contents of list, apply function and store in Parent
reduce(Parent, Func, List) ->
{ Left, Right } = lists:split(trunc(length(List)/2), List),
Me = self(),
%io:format("Splitting in two~n"),
Pl = spawn(fun() -> reduce(Me, Func, Left) end),
Pr = spawn(fun() -> reduce(Me, Func, Right) end),
%merge results in parent and call Func on final left and right halves
combine(Parent, Func,[Pl, Pr]).
%merge pl and pl and combine in parent
combine(Parent, Func, [Pl, Pr]) ->
%wait for processes to complete (using receive) and then send to Parent
receive
{ Pl, Sorted } -> combine(Parent, Func, Pr, Sorted);
{ Pr, Sorted } -> combine(Parent, Func, Pl, Sorted)
end.
combine(Parent, Func, P, List) ->
%wait and store in results and then call ! to send
receive
{ P, Sorted } ->
Results = Func(Sorted, List),
case Parent of
root ->
Results;
%send results to parent
_ -> Parent ! {self(), Results}
end
end.

Erlang, replacing an atom with another one in a list

I want to write a function to replace a specific atom with the given atom in an input list. But I want to do it using pattern matching and not using conditional statements. Any idea?
And also I want to write a function to return unique atoms in an expression.
e.g.
Input:
[a, b, c, a, b]
Output:
c
Input:
[b, b, b, r, t, y, y]
Output:
[t, r]
Assuming you want to replace all instances and keep the order of the list (works with all terms):
replace(Old, New, List) -> replace(Old, New, List, []).
replace(_Old, _New, [], Acc) -> lists:reverse(Acc);
replace(Old, New, [Old|List], Acc) -> replace(Old, New, List, [New|Acc]);
replace(Old, New, [Other|List], Acc) -> replace(Old, New, List, [Other|Acc]).
For the unique elements filter, you need to keep a state of which elements you have looked at already.
It would be really awkward to implement such a function using only pattern matching in the function headers and you would not really gain anything (performance) from it. The awkwardness would come from having to loop through both the list in question and the list(s) keeping your state of already parsed elements. You would also loose a lot of readability.
I would recommend going for something simpler (works with all terms, not just atoms):
unique(List) -> unique(List, []).
unique([], Counts) ->
lists:foldl(fun({E, 1}, Acc) -> [E|Acc];
(_, Acc) -> Acc
end, [], Counts);
unique([E|List], Counts) ->
unique(List, count(E, Counts).
count(E, []) -> [{E, 1}];
count(E, [{E, N}|Rest]) -> [{E, N + 1}|Rest];
count(E, [{X, N}|Rest]) -> [{X, N}|count(E, Rest)].
One way I'm looking for solving your first question would be to use guards, instead of if statements. Using only pattern matching doesn't seem possible (or desirable, even if you can do it).
So, for instance, you could do something like:
my_replace([H|T], ToReplace, Replacement, Accum) when H == ToReplace ->
my_replace(T, ToReplace, Replacement, [Replacement|Accum]);
my_replace([H|T], ToReplace, Replacement, Accum) ->
my_replace(T, ToReplace, Replacement, [H|Accum]);
my_replace([], ToReplace, Replacement, Accum) ->
lists:reverse(Accum).
EDIT: Edited for simplicity and style, thanks for the comments. :)
For the second part of your question, what do you consider an "expression"?
EDIT: Nevermind that, usort doesn't completely remove duplicates, sorry.

Erlang: "prepending" an element to a tuple

Is it possible to write a faster equivalent to this function?
prepend(X, Tuple) ->
list_to_tuple([X | tuple_to_list(Tuple)]).
It looks to me like that sort of thing is discouraged. If you want a list, use one.
Getting Started with Erlang:
Tuples have a fixed number of things in them.
If you have a finite number of possible tuple lengths, you could do this:
prepend(X, {}) -> {X};
prepend(X, {A}) -> {X, A};
prepend(X, {A, B}) -> {X, A, B};
prepend(X, {A, B, C}) -> {X, A, B, C}.
You can continue this pattern for as long as you need.
As prepending an element is the same as inserting it at position 1, you can use the built-in function erlang:insert_element/3:
> erlang:insert_element(1, {a, b}, z).
{z,a,b}
This function was added in Erlang/OTP R16A.

Erlang code critique

I am trying to get my head round some basic erlang functionality and I could do with some comments on the following.
I have the following erlang code that takes a list of tuples and returns a list minus an element if a key is found:
delete(Key, Database) ->
remove(Database, Key, []).
remove([], Key, Acc) ->
Acc;
remove([H|T], Key, Acc) ->
if
element(1, H) /= Key ->
[H| remove(T, Key, Acc)];
true ->
remove(T, Key, Acc)
end.
Is this a good way of doing this?
The if statement seems incorrect.
Also is my use of the accumulator Acc making this tail recursive?
No, your usage of Acc doesn't make it tail recursive. Your branch of if returns [H| remove(T, Key, Acc)] which is not tail call and this branch would be used most of time. To be more precise your usage of Acc is useless because it would be [] whole time, you don't change its value at all. Correct code should look like.
delete(Key, Database) ->
remove(Database, Key, []).
remove([], Key, Acc) ->
lists:reverse(Acc);
remove([H|T], Key, Acc) ->
if
element(1, H) /= Key ->
remove(T, Key, [H|Acc]);
true ->
remove(T, Key, Acc)
end.
But if your list members are always pairs I would prefer direct pattern match:
delete(Key, Database) ->
remove(Database, Key, []).
remove([], Key, Acc) ->
lists:reverse(Acc);
remove([{Key, _}|T], Key, Acc) ->
remove(T, Key, Acc);
% if it should delete only first occurrence then lists:reverse(Acc, T);
remove([H|T], Key, Acc) ->
remove(T, Key, [H|Acc]).
But I think this is example where can apply Myth: Tail-recursive functions are MUCH faster than recursive functions so I would use much simpler recursive version:
delete(Key, []) -> [];
delete(Key, [{Key, _}|T]) -> delete(Key, T);
% if it should delete only first occurrence then just T;
delete(Key, [H|T]) -> [H | delete(Key, T)].
As mentioned, there is a standard module function that already does this (proplists:delete). Shouldn't need to say more, but...
I'd tend to keep the original method name (delete), but have a local version including the accumulator as parameter. The context makes me think the order of the tuples in the "database" doesn't matter, so that a lists:reverse isn't necessary.
-module(foo).
-export([delete/2]).
delete(Key, Database) ->
delete(Key, Database, []).
delete(_Key, [], Acc) ->
Acc;
delete(Key, [{Key, _} | T], Acc) ->
delete(Key, T, Acc);
delete(Key, [Entry={_, _} | T], Acc) ->
delete(Key, T, [Entry | Acc]).
A couple things here:
tail-recursive; in general I think it is safer to stick with tail-recursion -- while there are optimizations for body recursive calls, you'd really need to do some performance measurement with realistic (for your application) data to make a comparison
note that we're not accepting any old list here; the Entry pattern match in delete/3 helps ensure this (depending on what this is for, you may or may not want this)

How do I turn a list of tuple pairs into a record in Erlang?

Let's say I have this:
-record(my_record, {foo, bar, baz}).
Keyvalpairs = [{foo, val1},
{bar, val2},
{baz, val3}].
Foorecord = #my_record{foo=val1, bar=val2, baz=val3}.
How do I convert Keyvalpairs into Foorecord?
The simplest thing to do is:
Foorecord = #my_record{foo=proplists:get_value(foo, Keyvalpairs),
bar=proplists:get_value(bar, Keyvalpairs),
baz=proplists:get_value(baz, Keyvalpairs)}.
If this is too repetitive you can do something like:
Foorecord = list_to_tuple([my_record|[proplists:get_value(X, Keyvalpairs)
|| X <- record_info(fields, my_record)]]).
Like the other answers point out, you need to roll your own solution to accomplish this. The solutions proposed are however incomplete. For example, it doesn't take into account default values for record entries. I use the following code snippet to take care of this conversion:
%% #doc returns a "RECSPEC" that can be used by to_rec in order to
%% perform conversions
-define(RECSPEC(R), {R, tuple_to_list(#R{}), record_info(fields, R)}).
%% #doc converts a property list into a record.
-spec to_rec(recspec(), proplist()) -> record().
to_rec({R, [_ | N], Spec}, P) when is_atom(R) and is_list(Spec) ->
list_to_tuple(
[R | lists:foldl(
fun ({K,V}, A) ->
case index_of(K, Spec) of
undefined ->
A;
I ->
{Head, Tail} = lists:split(I, A),
Rest = case Tail of
[_ | M] -> M;
[] -> []
end,
Head ++ [V | Rest]
end
end, N, P)]).
Now one can simply do:
-record(frob, {foo, bar="bar", baz}).
to_rec(?RECSPEC(frob), [{baz, "baz"}, {foo, "foo"}])
which yields
#frob{foo="foo", bar="bar", baz="baz"}
I put this into a little "toolbox" library I am building to collect these little "snippets" that just make life easier when developing Erlang applications: ETBX
If you have the values in the same order as in the record, you can convert directly into the record, you just need to precede the name of the record at the first element of the list and then convert the list into a tuple.
Foorecord = list_to_tuple([my_record]++[Val || {_,Val} <- [{foo, val1},{bar, val2},{baz, val3}] ]).

Resources