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?

4
  • 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. Commented Jan 9, 2012 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? Commented Jan 9, 2012 at 16:34
  • 7
    This was discussed in this question. Commented Jan 9, 2012 at 16:35
  • This is a duplicate; please, vote close it. Commented Jan 10, 2012 at 14:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.