Starting from a simple case of "fold" (I used (+) but can be anything else):
Prelude.foldl (+) 0 [10,20,30]
is it possible apply an inline transformation similar to (that doesn't work):
Prelude.foldl ((+) . (\x -> read x :: Int)) 0 ["10","20","30"]
In case not, is there an alternative to fold, to apply a generic function and inline transformation (apart from using specific functions like 'sum', 'max' etc)?
Best How To :
read lambda applies to the first argument and the first argument to the function given to
foldl is the accumulator. Those two arguments are the opposite for
foldr. So, expanded, it looks like this:
foldl (\acc element -> (read acc :: Int) + element) 0 ["10", "20", "30"]
acc is an
Int, this doesn't work.
So, with this information in hand, you can do this with
foldr since it has the opposite argument order:
foldr ((+) . (read :: String -> Int)) 0 ["10","20","30"]
If you want an inline
foldl version, you can use
flip to achieve this.
You could also use
map first (everything else being equal, this would be the solution I'd prefer):
foldl (+) 0 $ map (read :: String -> Int) ["10","20","30"]
Also, you probably want
foldl' instead of