• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i 'curry' in scala

 
Trilochan Bharadwaj
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

OK, first of all, I am admitting that I am not experienced with Scala at all, After buying Venkat's book, which I must admit is really hot! I tried a few things in first 4-5 chapters I guess (this is more of coding and less of reading ... ), anyways, I was able to intuitively (beauty of Scala?) do anonymous functions and invoke them, however i can't figure out how do i define a lambda expression so as to speak, or something which takes function as an argument or returns a function or both... the whole notation (I decided to not read by skipping chapters, (there's a functions chapter in the book ...), but couldn't resist doing it myself first =) .. I wrote this but it didn't compile:



Any suggestions?

Trilochan
 
Trilochan Bharadwaj
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I searched this forum and got an excellent link:
Functions in Scala
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you were going for, but as you saw in that blog posting, to define a method that takes a function argument you use either one of the following equivalent methods.



You can call it with either a named method, or by an anonymous function:
 
Gabriel Claramunt
Ranch Hand
Posts: 375
Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like currying is something different than what you where trying to accomplish.
Currying is simply the transformation of a function with multiple parameters into another function with only one parameter that returns a function of the remaining parameters.
In Scala, if you want to write curried functions, you just separate the parameters with parenthesis.
If the normal(uncurried) function is:

then, the curried version is:

A while ago, I did a blog entry on currying in Scala vs Haskell
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic