emmanueltouzery / prelude-ts

Functional programming, immutable collections and FP constructs for typescript and javascript
ISC License
377 stars 21 forks source link

Allow fold to use a different type on the accumulator #56

Closed sporto closed 2 years ago

sporto commented 2 years ago

Vector.fold and similar have a signature like:

fold(zero: T, fn: (v1: T, v2: T): T): T

Many times you want to fold into a different type This is common in FP langs. e.g. https://package.elm-lang.org/packages/elm/core/latest/List#foldl

I'm missing an alternative function?

Consider changing this to something like, where the accumulator can be something different.

fold(zero: U, fn: (v1: T, v2: U): U): U
emmanueltouzery commented 2 years ago

I believe you're looking for foldLeft? See the foldable interface: http://emmanueltouzery.github.io/prelude.ts/latest/apidoc/interfaces/foldable.html

emmanueltouzery commented 2 years ago

closing for now, I believe I answered. If that's not the case, please reopen.

sporto commented 2 years ago

Yes, it seems so, thanks