问题
I'm having some difficulty using Turtle and only after several minutes staring at incomprehensible error messages realized that I was using wrong fold
function.
https://hackage.haskell.org/package/turtle-1.5.8/docs/Turtle-Shell.html#v:fold https://hackage.haskell.org/package/foldl-1.4.0/docs/src/Control.Foldl.html#fold
Why is there a name collision? I don't believe that's coincidence but I cannot figure it out. Are these inherently different kinds of folds?
To be concrete, I wanted to fold a stream filenames into single name using maximum of modification time.
回答1:
(First of all, the folds we are talking about here are not the foldr foldl foldl' functions from Data.Foldable
, they are from the foldl package, which is conceptually related to the foldl'
function from the Data.Foldable
, as a sort of versatile generalization, but has separate definitions.)
The Fold datatype repesents a strict, stateful operation to perform on an incoming sequence of values. Its Applicative
instance lets you run two operations combined on the same sequence of values. Fold
s have the nice property that they are agnostic about where the values come from.
Each type of source will have its own function to feed in the values. These functions might share the name fold
, but it isn't a problem because the typical recommendation is to import the packages qualified.
The fold function from the foldl package feeds the contents of any Foldable
container. The fold from turtle feeds the results of a Shell
. Streaming libraries like "pipes" have their own adapters.
来源:https://stackoverflow.com/questions/50050325/should-i-use-folds-in-turtle-or-foldl-packages