You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, we are specifying the following signature for foldLeft/foldRight, for all ordered collections (array is just an example):
func foldLeft<T, A>(array : [T], base : A, combine : (A, T) -> A) : A
func foldRight<T, A>(array : [T], base : A, combine : (T, A) -> A) : A
(this is the relative base/combine argument order in Haskell and SML)
Notice that the combine functions take the accumulator on the left or right, but the base arguments invariably comes first.
Should we change this to:
func foldLeft<T, A>(array : [T], base : A, combine : (A, T) -> A) : A
func foldRight<T, A>(array : [T], combine : (T, A) -> A, base : A) : A // swap base and combine
(this is the relative base/combine argument order in OCaml)
Informally, this corresponds to the textual order of the unfolded operations:
foldLeft([a0,...an], b, c) = c(.... c(c(b, a0), a1) ..., an)
foldRight([a0,a1,...,an], c, b) = c(a0, c(a1, ... c(an, b)..))
Then it is clearer that b is combined with the first or last element and it might make it easier to remember the distinction between foldLeft and foldRight.
The downside is that swapping from foldLeft to foldRight requires some (more) argument shuffling.
The text was updated successfully, but these errors were encountered:
This is a good point. I would be fine with either of these options with a very slight preference for the current argument order (base, combine), which is consistent with Rust and Scala.
It seems like there is a wide range of different orderings across mainstream languages. JS and Python use combine, base for both left/right due to the base value being optional. However the argument order is usually consistent between the left and right functions.
Currently, we are specifying the following signature for foldLeft/foldRight, for all ordered collections (array is just an example):
(this is the relative base/combine argument order in Haskell and SML)
Notice that the combine functions take the accumulator on the left or right, but the base arguments invariably comes first.
Should we change this to:
(this is the relative base/combine argument order in OCaml)
Informally, this corresponds to the textual order of the unfolded operations:
Then it is clearer that
b
is combined with the first or last element and it might make it easier to remember the distinction betweenfoldLeft
andfoldRight
.The downside is that swapping from
foldLeft
tofoldRight
requires some (more) argument shuffling.The text was updated successfully, but these errors were encountered: