aspose file tools*
The moose likes Scala and the fly likes How do i 'curry' in scala Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Scala
Bookmark "How do i Watch "How do i New topic
Author

How do i 'curry' in scala

Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
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

Joined: Feb 02, 2009
Posts: 100
OK, I searched this forum and got an excellent link:
Functions in Scala
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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:


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

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

Gabriel
Software Surgeon
 
 
subject: How do i 'curry' in scala