[squeak-dev] Re: Collection>>sum implementation

Tony Garnock-Jones tonyg at lshift.net
Tue Aug 12 09:21:21 UTC 2008


Jason Johnson wrote:
> #inject:into: behaves like a right fold (reduction moves from left to
> right) but with the [] on the front of the list instead of the end.

It's actually behaving like a *left* fold. The leftness/rightness of a 
fold is named after the left/right associativity that the combining 
operator is given -- the difference between

   ((([] : 1) : 2) : 3)       -- a left fold, with (:) invoked
                              -- left-associatively

and

   (1 : (2 : (3 : [])))       -- a right fold, with (:) invoked
                              -- right-associatively

In haskell,

   foldr (:) [] =~= id

and

   foldl (flip (:)) [] === reverse

Regards,
   Tony




More information about the Squeak-dev mailing list