owaisqayum

owaisqayum

Understanding Enum.scan

Hi,

I am having a list of tuples

list = [{-1, 3}, {1, 5}, {10, 15}]

When i pass it through Enum.scan, it gives me the following result. Its a program i saw on codewars but i really dont understand how the Enum.scan is working over here.

Enum.scan(list, fn {b, e}, {_, x} -> {max(b, x), max(e, x)} end)

output: [{-1, 3}, {3, 5}, {10, 15}]

I tried IO.inspect to look at the results but still, it doesn’t make any sense. Here is the result of IO.inspect

b: 1
x1: 3
max(b, x): 3
e: 5
x2: 3
max(e, x): 5
b: 10
x1: 5
max(b, x): 10
e: 15
x2: 5
max(e, x): 15
[{-1, 3}, {3, 5}, {10, 15}]

First Post!

fuelen

fuelen

Hi,
It makes sense :slight_smile: As you don’t pass initial accumulator, then the first element of the list was taken and the first element on output will be the same as on input - {-1, 3}.
Then, on first iteration

[b: 1, e: 5, x: 3]

b and e are from the second element of the list and x is from accumulator which is the first element of the list.
Then you build new two-element tuple which is {3, 5}, it is used as second value in output list and as accumulator for the next element.
Then on the next iteration

[b: 10, e: 15, x: 5]

b and e are from the last element and x is from accumulator. Newly built tuple is {10, 15}, it is used as third element.
So, we have

[{-1, 3}, {3, 5}, {10, 15}]

Maybe, it would be easier for you if we reimplement Enum.scan using Enum.map_reduce:

def my_scan([], _callback), do: []
def my_scan([head | tail], callback) do
  {map_result, _reduce_result} = Enum.map_reduce(tail, head,  fn x, acc ->
    result = callback.(x, acc)
    {result, result}
  end)
  [head | map_result]
end

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement