[nem-en] [proposal] foldr1/foldl1

Kamil Skalski kamil.skalski at gmail.com
Sun Jul 1 10:37:43 CEST 2007


Very nice functions indeed. It would be also nice to have a general
extension methods operating on IEnumerable [T] doing the same things,
so the functionality won't be limited to lists (which should have
their own specialized implementation as you provided).

Will you commit the changes or prepare a patch?

2007/7/1, Elifant <elifantu at mail.ru>:
> Hello all.
>
> I propose to add functions like Haskell's foldr1 and foldl1 to standard
> library. They are very convenient
> when result has the same type and element type has no "zero" value with
> respect to the applied operation.
>
> Example:
> Within macro call I have a list of expressions (with type PExpr) "e1,
> e2, ..., eN" and I need to get expression
> "((e1 + e2) + ...) + eN". FoldLeft can't be used directly, since <[]>
> can't be used as a starting value.
> With FoldLeft: def sum = FoldLeft(exprs.Tail, exprs.Head, (el, er) => <[
> $el + $er ]>)
> With FoldLeft1: def sum = FoldLeft1(exprs, (el, er) => <[ $el + $er ]>)
>
> Don't know whether these functions must have a different name or just be
> an overload of FoldLeft and FoldRight.
> Reference implementation:
>
> def FoldLeft1 ['a'] (l: list['a], f: 'a * 'a -> 'a): 'a
>         FoldLeft(l.Tail, l.Head, f)
>
> // This one may be implemented more efficiently, I think...
> def FoldRight1 ['a'] (l: list['a], f: 'a * 'a -> 'a): 'a
>         def (rest, last) = DivideLast(l)
>         FoldRight(rest, last, f)
>
>
> _______________________________________________
> https://nemerle.org/mailman/listinfo/devel-en
>


-- 
Kamil Skalski
http://nazgul.omega.pl



More information about the devel-en mailing list