Elixir Get Map containing maximum values of specific field - functional-programming

I'm a beginner in Elixir programming language.
I have an object something like this:
{
id: uuid,
created_at: DateTime.t,
updated_at: DateTime.t,
type: my_type
}
let's say my_type is one of ~w[A B C D]
I want to write a function which takes a list of these objects and returns a following map:
%{
A: 120,
B: 220,
C: 560,
D: 0,
any: 560
}
The values here has to be the MAXIMUM difference between updated_at and created_at columns(Timex.diff(updated_at, created_at, :seconds)) per my_type + any in addition.
In case of any the my_type is not considered and takes the maximum among all objects in the list.
What is the best way to do it in Elixir? Thanks.

The following will group the list by its type, then calculate the max difference for each group, and finally result in a map containing each type as the key and max difference as the value.
map = list
|> Enum.group_by(& &1.type)
|> Enum.map(fn {type, values} ->
max =
values
|> Enum.map(fn %{created_at: created_at, updated_at: updated_at} ->
# return the difference here
end)
|> Enum.max
{type, max}
end)
|> Map.new
This should give you something like:
%{
A: 120,
B: 220,
C: 560,
D: 0
}
You can calculate the value for any now by doing map |> Map.values |> Enum.max.

I am posting this here for the sake of formatting and diversity. The answer by #Dogbert is likely better fit, although this approach could be more straightforward and arguably flexible.
list = [%{type: :a, v1: 10, v2: 20},
%{type: :a, v1: 10, v2: 30},
%{type: :b, v1: 10, v2: 20},
%{type: :c, v1: 10, v2: 20}]
kw = for %{type: t, v1: v1, v2: v2} <- list,
do: {t, v2 - v1}, into: []
#⇒ [a: 10, a: 20, b: 10, c: 10]
kw
|> Enum.sort_by(fn {_, v} -> v end, &>=/2)
|> Enum.uniq_by(fn {k, _} -> k end)
#⇒ [a: 20, b: 10, c: 10]

Related

Why can't I aplly anonymous functions to lists?

So I'm trying to learn Elixir (I have a background o F# and Haskell) and I'm having difficulties understanging what is going on in my code:
fizz_buzz = fn
(0, 0, _) -> "FizzBuzz"
(0, _, _) -> "Fizz"
(_, 0, _) -> "Buzz"
(_, _, c) -> c
end
fizz_buzz_rem = fn n -> fizz_buzz.(rem(n, 3), rem(n, 5), n) end
# This works
IO.puts(fizz_buzz_rem.(10))
IO.puts(fizz_buzz_rem.(11))
IO.puts(fizz_buzz_rem.(12))
IO.puts(fizz_buzz_rem.(13))
IO.puts(fizz_buzz_rem.(14))
IO.puts(fizz_buzz_rem.(15))
IO.puts(fizz_buzz_rem.(16))
IO.puts(fizz_buzz_rem.(17))
IO.puts("----------------")
inputs =
10..17
|> Enum.to_list
# Doesn't work
inputs
|> Enum.map(fizz_buzz_rem)
|> IO.puts
IO.puts("----------------")
# Doesn't work
inputs
|> Enum.map(fn n -> fizz_buzz.(rem(n, 3), rem(n, 5), n) end)
|> IO.puts
IO.puts("----------------")
manual_inputs = [10, 11, 12, 13, 14, 15, 16, 17]
# Doesn't work
manual_inputs
|> Enum.map(fizz_buzz_rem)
|> IO.puts
IO.puts("----------------")
# Doesn't work
manual_inputs
|> Enum.map(fn n -> fizz_buzz.(rem(n, 3), rem(n, 5), n) end)
|> IO.puts
IO.puts("----------------")
# The idiotic way (that doesn't work)
result = [
fizz_buzz_rem.(10),
fizz_buzz_rem.(11),
fizz_buzz_rem.(12),
fizz_buzz_rem.(13),
fizz_buzz_rem.(14),
fizz_buzz_rem.(15),
fizz_buzz_rem.(16),
fizz_buzz_rem.(17),
]
IO.puts result
# ???????????
When I run elixir ex_02.exs the output is:
Buzz
FizzBuzz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
----------------
Buzz
FizzBuzz
----------------
Buzz
FizzBuzz
----------------
Buzz
FizzBuzz
----------------
Buzz
FizzBuzz
So as you can see when I apply the anonymous function to each value individually I get the right answer bu when I try to use ranges, maps and even apply the function to each element of a list manually I end up with the wrong result.
What am I getting wrong about aplying a anonymous function to a list in elixir?
If you use IO.inspect instead of IO.puts, you can see what's going on:
["Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17]
Your fizzbuzz function returns either a string or an integer, depending on the input. IO.puts treats integers differently depending on whether they're in a list:
iex(1)> IO.puts(65)
65
:ok
iex(2)> IO.puts([65])
A
:ok
So in your code, IO.puts actually prints the control codes corresponding to the integers 11, 13, 14, 16 and 17. In my terminal it shows up as:
Buzz^KFizz^M^NFizzBuzz^P^Q
You can fix this by making your function always return strings:
fizz_buzz = fn
(0, 0, _) -> "FizzBuzz"
(0, _, _) -> "Fizz"
(_, 0, _) -> "Buzz"
(_, _, c) -> "#{c}"
end

Which approach to find multiple totals more closely follows the functional programming paradigm?

Say you have an array of objects with the structure like {id: 1, type: 'A', value: 10} want to find the total of type A, type B, and type C.
It would be more efficient to initialize the total variables and then loop through the array once, adding the the total variables based on type, than to use a reduce function for each total, in effect looping over the array 3 times.
However, from what I understand from the functional programming paradigm, functions should not manipulate anything outside of it internal scope and functions should have just one purpose, so the latter approach would be preferred.
Approach 1: initialize a variable for each of the three types, loop once and add to each total based on type
Approach 2: use reduce function for each total type.
Which one is preferred?
You can use a single fold/reduce if you use a record containing the three values as the state e.g. in clojure:
(defn sum-inputs [inputs]
(reduce (fn [acc {:keys [type value]}]
(update acc (keyword type) + value))
{:A 0 :B 0 :C 0}
inputs))
then
(sum-inputs [{:id 1 :type "A" :value 10}
{:id 2 :type "B" :value 12}
{:id 3 :type "B" :value 7}
{:id 4 :type "C" :value 40}])
in Javascript it looks like you can use Array.reduce:
const input = [{id: 1, type: "A", value: 4}, {id: 2, type: "B", value: 3}, {id: 3, type: "B", value: 9}, {id: 4, type: "C", value: 2}]
input.reduce(function(acc, i) { acc[i.type] += i.value; return acc; }, {A: 0, B: 0, C: 0})
note this mutates the accumulator record in place.

Pattern match against the other element of a map

I just want to know if there's a clean way to get the "other" element from a map in Elixir. By "other" I mean a second key-value pair, whose key I don't know.
Example: %{success: boolean, other => value}
This is the best I could come up with:
case map do
%{success: true} ->
other = map |> Map.delete(:success) |> Map.values |> List.first
# Do something with other
%{success: false} ->
error = map |> Map.delete(:success) |> Map.values |> List.first
# Do something with error
end
There's Map.pop/3 function, which accepts map and a key and returns a tuple with the value and a map without the key:
Map.pop %{ a: 1, b: 2 }, :a
# => {1, %{b: 2}}
and will refactor your code into something like:
case Map.pop(map, :success) do
{true, other_map} ->
other = other_map |> Map.values |> List.first
# Do something with other
{false, other_map} ->
error = other_map |> Map.values |> List.first
# Do something with error
end
I would go with old good Enum.reduce/3:
Enum.reduce %{success: true, foo: 42}, %{state: nil, map: %{}}, fn
{:success, value}, acc -> %{acc | state: value}
{key, value}, acc -> %{acc | map: Map.put(acc.map, key, value)}
end
#⇒ %{map: %{foo: 42}, state: true}
Now you might do whatever is needed without code duplication. Actually, the tuple is fine for collecting the result:
{success, map} =
Enum.reduce %{success: true, foo: 42}, {nil, %{}}, fn
{:success, value}, {_, acc} -> {value, acc}
{key, value}, {state, acc} -> {state, Map.put(acc, key, value)}
end
#⇒ {true, %{foo: 42}}
iex(9)> map = %{:success => true, {1,2,3} => 10}
%{:success => true, {1, 2, 3} => 10}
iex(10)> List.first(for {key, val} <- map, key != :success, do: val)
10

Return the index for a list from a list in Erlang

I've been practicing using recursion to define the index in Erlang. Here I need to implement a function to return the index for a list from a list.
eg.
([2, 4, 4], [1, 1, 2, 4, 4, 3, 4 ]) ----> 2
([1, 3], [5, 2, 2, 3, 1, 3, 5]) ----> 4
([1], [3, 2, a, {1, 1}, 1] ----> 4
Here is my code:
-module(project).
-export([index/2]).
index([X|XS],[_]) -> index([X|XS],[_],1).
index(_,[],_) -> [];
index([X|XS],[X|_], ACC) -> ACC;
index([X|XS],[_|rest],ACC) ->index([X|XS],rest,ACC+1).
I modified and coded logically but it still can not being compiled. I hope someone who can help me with it. Thanks!
Just for fun, here is an implementation that is not written a very clean way, but illustrates the techniques I think you are looking for. Note there are two basic states: "checking" and "matching".
-module(sublistmatch).
-export([check/2]).
check(Segment, List) ->
SegLen = length(Segment),
ListLen = length(List),
Index = 1,
check(Segment, List, SegLen, ListLen, Index).
check(S, S, _, _, I) ->
{ok, I};
check(_, _, SL, LL, _) when SL >= LL ->
nomatch;
check(S = [H|Ss], [H|Ls], SL, LL, I) ->
case matches(Ss, Ls) of
true -> {ok, I};
false -> check(S, Ls, SL, LL - 1, I + 1)
end;
check(S, [_|L], SL, LL, I) ->
check(S, L, SL, LL - 1, I + 1).
matches([H|S], [H|L]) -> matches(S, L);
matches([], _) -> true;
matches(_, _) -> false.
Note that this depends on knowing the lengths of both the segment you are checking for, and the current length of the remaining list to check. Consider why this is necessary. Also consider how using the utility function matches/2 gives us a natural place to explore whether an option matches, and backtracks if it does not.
In real programs you would use the standard library functions such as lists:prefix/2, lists:suffix/2, or sets:is_subset/2, or maybe some key or member operation over a gb_tree, dict, map or array depending on the situation.
To Compile the code you have to change it to:
-module(project).
-export([index/2]).
%%index([X|XS],[_]) -> index([X|XS],[_],1).
index([X|XS],List) -> index([X|XS],List,1).
%% you shuld not pass '_' as parameter it's will be marked as unbound
index(_,[],_) -> [];
index([X|XS],[X|_], ACC) -> ACC;
%%index([X|XS],[_|rest],ACC) ->index([X|XS],rest,ACC+1).
index([X|XS],[_|Rest],ACC) ->index([X|XS],Rest,ACC+1).
%% rest is an atom, it's not the case you need to use here.
%%Variables should start with upper case letter.
This code will compiled but wrong results as some cases.

F# counting repeats in a list

In F# is there a way to map for example [2;2;2;2;5;5;5;7;7] to [4,3,2] without recursion and without mutable? I looked through the Array and List members and found Reduce but that does not seem to help.
You can implement it quickly using Seq.countBy. Using F# interactive, it looks like this:
> [2;2;2;2;5;5;5;7;7] |> Seq.countBy id;;
val it : seq<int * int> = seq [(2, 4); (5, 3); (7, 2)]
If you only want the counts (and not the values which were repeated), you can just pipe the result into Seq.map:
> [2;2;2;2;5;5;5;7;7] |> Seq.countBy id |> Seq.map snd;;
val it : seq<int> = seq [4; 3; 2]
Note that you can implement this using Seq.groupBy, but Seq.countBy is much more efficient: Seq.groupBy consumes more memory because it has to store all of the groups, whereas Seq.countBy stores just one int (the counter) for each key in the sequence.
Try this:
[2;2;2;2;5;5;5;7;7] |> Seq.groupBy id |> Seq.map (snd >> Seq.length)
Seq.groupBy id collects the list up into groups of equal elements - using the identity function id means that the elements of the sequence are used directly as the "keys" for the equality check. This gives us a sequence of the original elements paired up with the repeats:
seq [(2, seq [2; 2; 2; 2]); (5, seq [5; 5; 5]); (7, seq [7; 7])]
Then for each of the inner sequences, we use snd to just get the sequence of repeats, and Seq.length to get its length. >> is the composition operator that applies the first function and then the second.

Resources