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

Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
Fl4m3Ph03n1x
Background Hey guys, recently I bought a book on TDD that I am reading. The books is really nice and has some really juicy things on arch...
New
roshan
Hi everyone, I’m looking for a book on Phoenix server hosting / deployment like the following books for Rails, Docker for Rails Develop...
New
Santheepkumar
Hi all, I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
Fl4m3Ph03n1x
Background I am trying to recycle myself and improve my knowledge about Phoenix. With 1.7 now out, this seems like a good opportunity. W...
New
Fl4m3Ph03n1x
GenStage and Flow resources? I have been hearing about GenStage and Flow, a tool built upon it, for quite some time. I understand it allo...
New
New
Besto
I’ve been trying to start learning Elixir for a couple of weeks while I develop a tiny project I have on node.js, but every time I try to...
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement