alamba78
I’ve been reading “Introduction to Elixir” and I’m trying to understand where List.foldl and List.foldr would be useful. Basically, I’m looking for a use case. In what situation would the lightbulb go on that hey I should fold the list in this situation.
I believe List fold is very cheap to use (as long as it’s List.foldl as it’s tail recursive) but many people fail to understand a situation where it may be the tool for the job.
Thanks!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
JEG2
Well, you know all those iterators in
Enum? The truth is that you could choose to handle most of that withfold*(). Here are examples formap(),sum(), andfilter():ejc123
Here is an explanation of folds on lists: List Folds at BFPG
I only briefly read through the slides, so I can’t comment on the presentation, but I believe it should shed some light on the mystery of folds. Folds for Imperative Programmers is just what the title says. The code is in Scala, but it should be navigable.
I’ve read some of Tony’s other work and he knows his category theory concepts. His Monad Exercises in Scala (Haskell too) will change the way you think about programming.
alamba78
Not to nit pick, but the doubles example would probably benefit from using List.foldl as multiplication doesn’t matter if it is left to right or right to left. Granted, in such a simple example, it won’t make much of a difference in efficiency.
JEG2
It reverses the order of the list as it works, if you use
foldl().alamba78
Ahh, I should have run it in iex before. Now I see. List.foldl traverses left to right but reverses the result, hence Fold. I was remembering an example in the book and they mentioned that if order doesn’t change the result, then use Lsit.foldl. In the example in the book they were using an accumulator so with multiplication it didn’t matter. But, in your example you were returning the List doubled.
Also, I’m a bit confused what fn n, doubles → [n * 2 | doubles] end) is doing. You have an anonymous function which is passing in each list item and multiplying by 2. But, is doubles the resulting list? It is an empty list to begin with and each iteration is passing the original list item multiplied by 2?
So, you start with [1,2,3,4,5]
Then the doubles list ends up being [10,8,6,4,2]
and the List.foldr flips it making doubles [2.4.6.8.10]?
alamba78
No, I think I am wrong again. What’s happening is this:
original list is [1,2,3,4,5]
List.foldr traverses the list right to left so we start with n being 5 and work right to left. But you are making each iteration through the list part of the tails, basically reordering the list from small to big, like so:
doubles = [10] //first pass
doubles = [8,10] //seconds pass
doubles = [6,8,10] //third pass
etc…
I think I got it.
rvirding
More like you are stepping down the whole list to the end then on the way back up you folding the accumulator through function and returning the result.
alamba78
Why does this give a funny result?
List.foldl(nums, , fn n, doubles → [doubles | n * 2] end)
NobbZ
I think that
folds are best understood when source is provided.Therefore I’ll do a quick translate of my haskell-lecture-notes to elixir
without taking optimisations into accont. I use my haskell notes since they are
(nearly) always around my desk
Using these definitions, I will use each of the folds with 2 very simple
functions:
foldr [1,2,3], &(&1 + &2), 0foldl [1,2,3], &(&1 + &2), 0foldr [1,2,3], &([&1|&2]), []foldl [1,2,3], &([&1|&2]), []Personally I think, the best way to understand higher order functions, is just
to take a small example output, look at the definition and go through it by
hand. We had to do this a lot during FP exercises…
Pen and Paper, vim, emacs, whatever you like most, but beeing capable of
monospaced fonts helps a lot
edit
I’ve seen @alamba78’s latest reply to late, but I had not used that function anyway, since it produces improper lists!
Improper lists are something you want to avoid, that are lists, where the last “element” is not the empty list, but something else. They can really confuse your code. The example you are showing is even nesting them…
rvirding
You are building the list in the wrong direction. You cannot use the
[ | ]syntax to add an element to the end of the list, only to the front of the list in which case you would write[n * 2 | double]. There is no efficient way to add an element to the end of a list. So if you useList.foldlto build a list you will get the resulting in the “wrong” order. Think stack. To build in the “right” order you would useList.foldr.