The folds are in this order for a more specific reason.
The foldr function forms the catamorphism for [] such that passing the
constructors produces an identity function.
foldr (:) [] = id
* The same can be said for maybe and Maybe:
maybe Nothing Just = id
* either and Either:
either Left Right = id
* if/else and Boolean:
Just kidding. It is generally accepted that these arguments are around
the wrong way.
I'd do it this way for Scala:
def ifelse[F[_]: Functor, X](t: => X, f: => X): F[Boolean] => F[X]
PS: The type inferencer doesn't get foldl (+) wrong -- it's just
defaulting. Turn it off.
On May 3, 2:22 pm, Daniel Spiewak <
djspie...@gmail.com> wrote:
> I think there are some deeper issues with point-free in Scala that come from
> Scala's fundamental nature as an object-functional language. I think that
> no signature better exemplifies these issues than the foldl/foldLeft
> function/method:
>
> Prelude> :t foldl
> foldl :: (a -> b -> a) -> a -> [b] -> a
>
> trait TraversableOnce[+A] {
> …
> def foldLeft[B](b: B)(f: (B, A) => B): B
>
> }
>
> Notice what is going on here. In Haskell, the order of parameters follows
> from least- to most-specific:
>
> - reduce function
> - seed value
> - list
>
> In Scala, we do the opposite:
>
> - collection
> - seed value
> - reduce function
>
> Put another way: in Scala, all of the parameter ordering is backwards! Take
> a look around some of the other functions in the standard library and
> compare them to their Haskell/SML equivalents (map is a good one). Scala is
> very consistently inverted.
>
> This follows almost immediately from the fact that the method receiver is
> effectively (and literally) the first argument of the function. Method
> receivers are, by definition, the most specific parameter, and thus we have
> our ordering reversed. Scala exacerbates this slightly by giving trailing,
> curried function parameters a special syntax (obviating the need for
> parentheses). Not that this special syntax is a bad thing per se, but it
> does cause some major problems for point-free composability.
>
> The issue is this: how do you compose together a progressively more
> specialized version of a function when the *first* parameter you specify
> must always be the least general? Consider the following Haskell snippet:
>
> Prelude> let sum = foldl (+)
> Prelude> :t sum
> sum :: Integer -> [Integer] -> Integer
>
> Ignoring the fact that the type inferencer gets this wrong… Haskell makes
> it very easy to build up partial functions like this. I can take this sum
> function and then further specialize it against any seed value, to be used
> on any collection. However, if we try to do that with Scala, we *
> immediately* get stuck specializing against the collection value, without
> ever getting the chance to leave it generic.
>
> Oh, and this whole situation is made a hundred times worse by the fact that
> Scala doesn't have rank-2 types, so we can't just get a value for
> TraversableOnce#foldLeft which we can then pass to some utility function
> which might reverse the parameter order.
>
> In short, there are a lot of reasons Scala isn't particularly amenable to
> point free that have nothing to do with tupled vs curried formal parameters
> in the "default" (as in: customarily applied) syntax. Having worked with
> Scala for quite a while now and consistently *tried* to compose using
> point-free combinators, I'm pretty certain that the reason I outlined
> (parameter ordering) is the biggest stumbling block in this area.
>
> Daniel
>
> P.S. I know this doesn't *exactly* address your main point, but I think it's
> important to see that the syntax isn't really the main issue here. It's all
> about the fundamental semantics of the underlying language paradigm, and
> that paradigm is not particularly amenable to point-free.
>
> >
http://pchiusano.blogspot.com/2009/08/scala-success-story-commercial-...
> > Functions aren't curried by default, and I find working with curried
> > functions kind of cumbersome in Scala. There are also some language warts
> > that make it pretty much impossible to program in a point-free style
> > (namely, the somewhat arbitrary distinction between methods, defined using
> > def, and function values which implement one of the FunctionN traits). This
> > might not be so terrible if you didn't have to fully annotate parameter
> > types in method definitions, but you do, and it can get kind of annoying.
> > Again, we are still much better off than we are in Java, but it's possible
> > to do better (for instance, Haskell).
>
> >
http://scala-programming-language.1934581.n4.nabble.com/Point-free-td...