1

Possible Duplicate:
Applying an argument list to curried function using foldLeft in Scala

Consider:

val f = (i: Int, j: Int, k: Int, l: Int) => i + j + k + l

Because one can do this:

f.curried.apply(1).apply(2).apply(3).apply(4)

It's easy to fall into the trap of trying this:

List(1, 2, 3, 4).foldLeft(f.curried) { (fs, e) => fs.apply(e) }

However, the B parameter in the fold changes type once one applies an argument to it. In this example, the first iteration would change from a (Int) => (Int) => (Int) => (Int) => Int to a (Int) => (Int) => (Int) => Int.

Question: How to solve this?

Community
  • 1
  • 1
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
  • 1
    I don't think this makes sense. In `(fs, e) => fs.apply(e)` you are actually using a different type for `fs` on every iteration and it's a different `apply` function. I don't see how it could work. – Gabe Jan 09 '12 at 16:29
  • 1
    I think the problem is that it's not entirely clear what is being looked for here. The problem suggested in the title - folding curried functions - is never going to work because of the problem you identify. So what's the underlying problem? Applying a list of parameters to a function? – Submonoid Jan 09 '12 at 16:34
  • 7
    This was discussed in [this question](http://stackoverflow.com/questions/7606587/applying-an-argument-list-to-curried-function-using-foldleft-in-scala). – hbatista Jan 09 '12 at 16:35
  • This is a duplicate; please, vote close it. – Hugo Sereno Ferreira Jan 10 '12 at 14:50

1 Answers1

2

You should be able to do this with an HList - see http://apocalisp.wordpress.com/2010/07/08/type-level-programming-in-scala-part-6b-hlist%C2%A0folds/ for an example of building fold functions over heterogenous lists.

Edit - hbatista's link in the comment above provides a full solution doing precisely this.

Submonoid
  • 2,809
  • 2
  • 20
  • 25