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?

First 3 of 3 Posts Switch mode

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.

ealmz

ealmz

TCO was the rabbit hole I was looking for. Thanks!

— All posts loaded —

Where Next?

Trending in Chat/Questions Top

krisleech
Hey folks, I’ve been using Elixir for over a couple of years (phx, ecto, broadway, oban). For this kind of work you do not tend, in my e...
New
mhindujadheerajsudan
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement