# Scrap your iteration combinators

**URL:** <https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024>\
**Category:** Links\
**Created:** [May 6, 2025, 9:40am UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024 "2025-05-06T09:40:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 9:40am UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/1 "2025-05-06T09:40:26Z")

</div>

My latest article, “Scrap your iteration combinators”. I would be interested to know what people think!

- [https://h2.jaguarpaw.co.uk/posts/scrap-your-iteration-combinators/](https://h2.jaguarpaw.co.uk/posts/scrap-your-iteration-combinators/)

---

<div class="post-metadata">

**Author:** ![Lysxia](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/lysxia/32/2916_2.png) [@Lysxia](https://discourse.haskell.org/u/Lysxia)\
**Post date:** [May 6, 2025, 3:05pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/2 "2025-05-06T15:05:52Z")

</div>

At least for `foldl'`, make sure to use the _strict_ state monad, otherwise the `put $! s'` does nothing! If I compile your definition with the lazy state monad, `ghc -O -ddump-simpl -dsuppress-all` shows a definition equivalent to `foldl`.

> > The `mapAccumR` function … applies a function to each element of a structure, passing an accumulating parameter from right to left
> 
> (“Right” and “left” here really mean “end” and “start”, but because we write English from left to right the we call the last element of a list the “rightmost” one and the first the “leftmost” one.)

Implicit here is that you’re only thinking of cons lists.

For snoc lists, there are arguments both ways for which is the “start” and “end”: some may think about reading from left to right, or others about starting from the head. And snoc lists swap the roles of `foldl` and `foldr`. “Never use `foldr`!” is now a thing you can say.

It’s also interesting to compare the various left and right folds on vectors. Unlike the situation with lists, we can find uses for both `foldl` and `foldr` on vectors.

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 3:42pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/3 "2025-05-06T15:42:30Z")

</div>

> [@Lysxia](#):
>
> At least for `foldl'`, make sure to use the _strict_ state monad

Right. `Control.Monad.Trans.State.Strict` is the one linked from “becomes the “state parameter” of a [`State`](https://hackage-content.haskell.org/package/transformers-0.6.2.0/docs/Control-Monad-Trans-State-Strict.html#t:State) monad operation”. Perhaps I should have made it more clear, but since I’ve never found any practical reason to use any of the `....Lazy` monads I think it’s better to simply always assume `....Strict. `

> [@Lysxia](#):
>
> Implicit here is that you’re only thinking of cons lists.

That’s right, I’m thinking of the lists that Haskell has by default, whose syntax is built in, and which all Haskellers assume you’re thinking of when you say “list”!

> [@Lysxia](#):
>
> For snoc lists

And that fact that we even have a notion of snoc list is yet another symptom of this dubious “left/right” nomenclature.

---

<div class="post-metadata">

**Author:** ![Lysxia](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/lysxia/32/2916_2.png) [@Lysxia](https://discourse.haskell.org/u/Lysxia)\
**Post date:** [May 6, 2025, 4:00pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/4 "2025-05-06T16:00:47Z")

</div>

> Perhaps I should have made it more clear, but since I’ve never found any practical reason to use any of the `....Lazy` monads I think it’s better to simply always assume `....Strict. `

I agree with that stance, but it’s worth being extra explicit about it because if a reader is not already aware of it, it’s easy to naively import `Control.Monad.Trans.State` which reexports `Lazy`.

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 4:17pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/5 "2025-05-06T16:17:11Z")

</div>

OK, thanks for the suggestion. I’ve added a clarification.

- [scrap-your-iteration-combinators](https://h2.jaguarpaw.co.uk/posts/scrap-your-iteration-combinators/)
- [Clarify that we are always taking about the \*strict\* state monad · tomjaguarpaw/H2@163fac1 · GitHub](https://github.com/tomjaguarpaw/H2/commit/163fac1437342368f5aa3d99168bec79e77a8c03)

---

<div class="post-metadata">

**Author:** ![meooow](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/meooow/32/3873_2.png) [@meooow](https://discourse.haskell.org/u/meooow)\
**Post date:** [May 6, 2025, 5:16pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/6 "2025-05-06T17:16:38Z")

</div>

These are interesting examples to think about, but I’m less convinced that this is a good idea to apply everywhere. It’s not surprising that one can use overpowered constructs (`Applicative`/`Monad`) to solve simple tasks. But should they? I’m certainly not replacing `mapMaybe` with a streaming library and nested for-loops.

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 5:31pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/7 "2025-05-06T17:31:58Z")

</div>

Can you elaborate on why you consider `Applicative`/`Monad` overpowered, and why you wouldn’t use a streaming library and nested for-loops to replace `mapMaybe`?

---

<div class="post-metadata">

**Author:** ![TeofilC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/teofilc/32/3987_2.png) [@TeofilC](https://discourse.haskell.org/u/TeofilC)\
**Post date:** [May 6, 2025, 5:51pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/8 "2025-05-06T17:51:31Z")

</div>

> [@meooow](#):
>
> These are interesting examples to think about, but I’m less convinced that this is a good idea to apply everywhere

I agree with this.  
I think the issue with using `for_` is that you are making code more polymorphic, which means that whoever is reading your code needs to run a typechecker in their head to figure out which traversal is actually being called.  
I find that this section of the GHC style guide expresses it well: [coding style · Wiki · Glasgow Haskell Compiler / GHC · GitLab](https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/coding-style#13-type-classes)

---

<div class="post-metadata">

**Author:** ![BurningWitness](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/burningwitness/32/3665_2.png) [@BurningWitness](https://discourse.haskell.org/u/BurningWitness)\
**Post date:** [May 6, 2025, 8:34pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/9 "2025-05-06T20:34:47Z")

</div>

The difference is in the accumulating parameter.

Basic folds are nice when there is no extra state to talk about, e.g. collapsing a data structure to a single simple value (or a list), or purging `Nothing`s. The definitions are obvious and the implementation is efficient.

Threading product type updates over a list fold on the other hand might as well just be performed imperatively, its functional counterpart has no redeeming qualities. Folding here introduces a state blob, which then makes the code harder to read because this blob has to be reassembled on every step. Not to mention the reassembly is inefficient garbage-wise and trivial to screw up strictness-wise.

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 9:23pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/10 "2025-05-06T21:23:58Z")

</div>

> [@TeofilC](#):
>
> that you are making code more polymorphic, which means that whoever is reading your code needs to run a typechecker in their head to figure out which traversal is actually being called

Aha! `foldl'` already has this problem, right? Because you don’t know what `Foldable` you’re iterating over nor the element type nor container type, without running the type checker in your head.

Or maybe those ones are OK, but there’s a particular issue with `Monad` type parameters? In which case `foldM` has this problem too. Perhaps we should have `foldEither`, `foldIO`, etc, so make that clearer? In the `extend` example in the article, the `foldM` was over `Either`. Perhaps using this would have been clearer:

```haskell
foldEither :: (b -> a -> Either e b) -> b -> [a] -> Either e b

```

There’s another possibility: type applications. In the original `extend` we could have written

```haskell
foldM @[] @(Either Conflict)

```

Then there’s no ambiguity. But then we could equally have written this in the final `extend`:

```haskell
for_ @[] @(StateT PPreAssignment (Either Conflict)) newactives

```

Then there’s no need to run a type checker in your head.

Does this resolve your concern?

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [May 6, 2025, 9:31pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/11 "2025-05-06T21:31:13Z")

</div>

> [@tomjaguarpaw](#):
>
> `foldM @[] @(Either Conflict)`

> [@tomjaguarpaw](#):
>
> `for_ @[] @(StateT PPreAssignment (Either Conflict)) newactives`

This solves the problem of having to run the type checker in your mind, but it reveals another one: the `for_` version is much more verbose.

I’m still on the fence which to prefer.

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 9:40pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/12 "2025-05-06T21:40:42Z")

</div>

> [@jaror](#):
>
> the `for_` version is much more verbose

Yeah, but only because `foldM` is already a “shorthand”. If reducing verbosity is my goal then I can flip type parameters and write

```haskell
myFor_ @(StateT _ _) 

```

and this _already_ specifies the type as precisely as `foldM`. It only gets more precise as I fill in the holes.

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [May 6, 2025, 9:49pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/13 "2025-05-06T21:49:23Z")

</div>

> [@tomjaguarpaw](#):
>
> and this _already_ specifies the type as precisely as `foldM`.

But it is still 4x as long.

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 9:57pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/14 "2025-05-06T21:57:18Z")

</div>

How about

```haskell
f @(S _ _)

```

That’s about as good as I can do!

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [May 6, 2025, 9:59pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/15 "2025-05-06T21:59:56Z")

</div>

That’s nice and compact, but now you’ve given up readability (compared to `foldM`).

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 6, 2025, 10:14pm UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/16 "2025-05-06T22:14:15Z")

</div>

Sure, for people who already know what `foldM` means (or at least “fold”). For anyone who doesn’t I think they’re both equally unreadable 🙂

---

<div class="post-metadata">

**Author:** ![darkxero](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@darkxero](https://discourse.haskell.org/u/darkxero)\
**Post date:** [May 7, 2025, 2:04am UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/17 "2025-05-07T02:04:42Z")

</div>

It’s a fun exercise. I prefer the rule of least power (prefer the least generic thing until needed).

I wanted to compare the current [mapMaybe](https://hackage.haskell.org/package/ghc-internal-9.1201.0/docs/src/GHC.Internal.Data.Maybe.html#mapMaybe) with

```haskell
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
mapMaybe f as =
  toList $
    for_ as $ \a -> do
      for_ (f a) $ \b ->
        yield b

```

but it doesn’t compile?

```haskell
      Expected: Streaming.Internal.Stream (Data.Functor.Of.Of b) m0 b
        Actual: Streaming.Internal.Stream (Data.Functor.Of.Of b) m0 ()

```

Can I get a minimal example? Maybe I’m using the wrong for\_?

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 7, 2025, 11:19am UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/18 "2025-05-07T11:19:41Z")

</div>

I don’t understand the error, but you can find all the code in the post here:

> <https://github.com/tomjaguarpaw/H2/blob/163fac1437342368f5aa3d99168bec79e77a8c03/code/fold.hs>

and you can load it in GHCi with something like

```plaintext
cabal repl -z -b streaming 
...
ghci> :l code/fold.hs 

```

---

<div class="post-metadata">

**Author:** ![TeofilC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/teofilc/32/3987_2.png) [@TeofilC](https://discourse.haskell.org/u/TeofilC)\
**Post date:** [May 7, 2025, 11:24am UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/19 "2025-05-07T11:24:55Z")

</div>

> [@tomjaguarpaw](#):
>
> Aha! `foldl'` already has this problem, right? Because you don’t know what `Foldable` you’re iterating over nor the element type nor container type, without running the type checker in your head.

Sure, but there are monomorphic versions of `foldl'`, and that’s what I often use in practice.  
FWIW using the polymorphic version of `foldl'` isn’t too bad because they are relatively easy to reason about. They should all be the same as doing `toList` first and then doing `foldl'`. It’s the sort of thing that I’ve committed to memory, so I don’t struggle to reason about it. But my memory is limited, and my ability remember potentially surprising instance definitions is especially limited.

> [@tomjaguarpaw](#):
>
> Does this resolve your concern?

There are two steps that I have to do to understand this code.

1. I need to figure out the types involved, which `TypeApplications` resolve
2. I need to figure out the meanings of the typeclass instances.

For monad transformers that can be tricky! In this example I need to see how `StateT` and `Either` interact. I would need to potentially open up two or more definitions and see how all of that code interacts. This is all doable and maybe in the grand scheme of things not that difficult, but this is taking up brain-space and energy that could be used for something else.

Of course the converse – lots and lots of monomorphic functions – is also a problem. You can sometimes use polymorphism to solve an N\*M issue very effectively.

It’s all a matter of balancing trade-offs, but in general going for something more monomorphic is often the right call as a rule of thumb.

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 7, 2025, 11:53am UTC](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024/20 "2025-05-07T11:53:59Z")

</div>

> [@TeofilC](#):
>
> there are monomorphic versions of `foldl'`, and that’s what I often use in practice.

Monomorphic in the container type presumably, not monomorphic in the element or state types. Would it be useful to you to have a fully monomorphic `foldlListIntElementsBoolState'`? If not, why not? Do you have some rule you can use to tell you where to draw the line?

> [@TeofilC](#):
>
> my memory is limited, and my ability remember potentially surprising instance definitions is especially limited.

Interesting, because I have the same reasoning but reach the opposite conclusion. It’s exactly because my memory is limited that I’d rather remember what a small number of composable abstractions like `StateT`, `EitherT` and `Stream` do than a somewhat larger number of non-composable abstractions like `foldl`, `foldl'`, `mapAccumL`, `loopM`, etc. do. With the former I can reason my way to building programs by combining them. With the latter I can’t.

(In a sense it’s even easier for me since I use `bluefin`, not `streaming` and `transformers`. All the effects already play nicely together by default, without causing any confusion.)

> In this example I need to see how `StateT` and `Either` interact. I would need to potentially open up two or more definitions and see how all of that code interacts.

I suppose so, but have you never wondered how the `m` parameter interacts with the threaded state in `foldM`? If not, why would you wonder about the former but not the latter?

> [@TeofilC](#):
>
> in general going for something more monomorphic is often the right call

Yes, I agree.

[Next page](https://discourse.haskell.org/t/scrap-your-iteration-combinators/12024.md?page=2)
