ealmz

ealmz

Request for blog post feedback: understanding Enum.reduce at a fundamental level

Hey y’all. Long time reader of the forum. I’m new to Elixir and I’m hoping writing technical blog posts will help me solidify my understanding.

I was wondering if anyone could take a look at a draft I wrote here: https://github.com/ealmz/elixir_from_first_principles/blob/master/reduce.md

It’s basically my current understanding of how reduce in Elixir works. I’m aware that there are many gaps in my knowledge but I’d love to hear what you think, especially strong critical feedback of how to make this post better.

What I think I’m missing:

  • How Elixir’s reduce method compares to those in other languages
  • Why reduce is important? In other words, what are the alternative ways to solve for the problem reduce addresses if not in this way.
  • Core CS concepts I’m unaware of.

What are your thoughts on that list or what else do you think I should address to really get at the core of what this function is doing?

Most Liked

hauleth

hauleth

Reduce (also know as left fold) is a way to “loop” in FP. It is defined as (in general case, not exactly in Elixir):

def reduce([], agg, _fun), do: agg
def reduce([hd | tl], agg, fun), do: reduce(tl, fun.(hd, agg), fun)

This is very basic operation, as from there you can define all other operations:

def reverse(list), do: reduce(list, [], &[&1 | &2])
def map(list, fun), do: list |> reduce([], &[fun.(&1) | &2]) |> reverse()
def filter(list, pred) do
  list
  |> reduce([], fn x, agg ->
    if pred.(x), do: [x | agg], else: agg
  end)
  |> reverse()
end
# Etc.

In other words - reduce is another way to represent TCO-recursive function.

NobbZ

NobbZ

Some things in your post:

  1. There are no methods in Elixir, only functions.
  2. Enum.reduce/2 will use the head of the input as initial accumulator, while reducing over the tail. The head is not included in the reduction, but in your post you suggest it were.

Those are the 2 points that I stumbled over.

Where Next?

Popular in Chat/Questions Top

satoru
I’m working on the “Bob” exercise on the Elixir Track in Exercism. I am testing for uppercase letter with this simple check: c in ?A..?Z...
New
New
markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
New
gouvermxt
I just finished the “Learn Functional Programming with Elixir (Pragprog)” book. I have 5+ years of experience with Ruby/Rails, my goal is...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
Nvim
Anybody know of a Pragmatic Studio 40% off coupon code for video course like Phoenix?
New
phykos
In Ruby, which is very similar to Elixir I do this: def test yield end test do puts("sup there") end Here, the yield keyword will be...
New
AstonJ
It finally feels like I’ve got some time to catch up on my reading - though I am sure lots of people will be wondering the same: what are...
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement