Skip to content

Instantly share code, notes, and snippets.

@Villane
Created July 12, 2011 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Villane/1079151 to your computer and use it in GitHub Desktop.
Save Villane/1079151 to your computer and use it in GitHub Desktop.
Klang: Function Passing
// apply the function 'f' twice to the parameter 'n'
def twice(n: Int, f: (Int) -> Int) = f(f(n))
// add one to the parameter
def addOne(n: Int) = n + 1
// return an initial value and a function
def getValueAndFunction(): (Int, myFunc: (Int) -> Int) = (1, addOne)
// run twice(...) with the initial value and function from getValueAndFunction()
// [] is currently used when passing a tuple directly as function parameters
def main() = twice[ getValueAndFunction() ]
@Villane
Copy link
Author

Villane commented Jul 12, 2011

BTW, ScalaTest is awesome. My tests look like this:

test("Can return a function in a tuple and pass the tuple as a parameter list") {
"""
  def twice(n: Int, f: (Int) -> Int) = f(f(n))

  def addOne(n: Int) = n + 1

  def getValueAndFunction(): (Int, myFunc: (Int) -> Int) = (1, addOne)

  def main() = twice[ getValueAndFunction() ]
""" should compile
}

test("Wrong number of arguments to function") {
"""
  extern fun(a: Int, b: Int): Int
  def main() = fun(1)
""" should
  failWith("Wrong number of arguments to 'fun': 2 expected, 1 found", (3, 23));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment