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

Yoga
Or at least, in the works? All I can find are bits and pieces but not a single project from start to finish. Including things like i18n,...
New
RKC07
I’m new to elixir. I did some coding in python and C. I want to learn elixir for starting my career in web development. I need suggestion...
New
New
yachnytskyi
Hello everyone. I am gonna start with Elixir/Phoenix, thinking to use Stephen Grider as a start point, then elixir school and other sourc...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size fro...
New
eliottramirez
Hello, I’m trying to learn Phoenix but I constantly find difficult understanding how the framework works, and I think part of this is th...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
Nopp
Hello there, i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New
QueenSvetlana
My bookstore has Introducing Elixir: Getting Started In Functional Programming and the front cover says the book uses Elixir 1.4. The cur...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement