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

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
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
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement