← Back to context

Comment by munchler

14 hours ago

> I wouldn't say "rarely", unless you have a whole host of other higher order functions at your disposal for more special cases than map and fold

That is my point. Modern functional languages do have a host of higher-order functions for exactly this sort of thing. For example, here is F#'s `Seq` module, for working with lazy sequences: https://fsharp.github.io/fsharp-core-docs/reference/fsharp-c...

> There are many cases, where you don't want to fold or map over the whole data structure and want to exit early with a result already. Writing tail recursive functions is still very common.

I think this can usually be handled more concisely by combining higher-order functions instead. For example, if you want to fold over a partial data structure, you can use `filter` to select only the elements you want, and then fold over that subset. Or, if you want to exit early from a map, you can use `takeWhile` and only map over what's left.

Real-world functional programming is usually about combining these built-in tools effectively, rather than writing new tools from scratch.

While you are right about being able to combine higher-order functions in the way you describe, I might not find your examples compelling, because they require 2 passes over the data.

On the other hand one could argue, that one pass of those is checking some condition (index below some number, or element followed by element that satisfies a predicate, or similar things), and the other pass is then blindly applying some other function, without having to check the condition again. Maybe that is in the end equal in performance.

  • > I might not find your examples compelling, because they require 2 passes over the data.

    Are these the examples?

    >> For example, if you want to fold over a partial data structure, you can use `filter` to select only the elements you want, and then fold over that subset. Or, if you want to exit early from a map, you can use `takeWhile` and only map over what's left.

    These are done in a single pass. And if they weren't, I'd stop using them.

  • Only one pass over the data is made when using lazy evaluation, such as the Seq module in F#. For folds, you can also put the filter directly inside the accumulator function to avoid a second pass.