O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Instance Methods Are Curried Functions

One cool thing to note is that instance methods are actually curried functions in Swift.

The basic idea behind currying is that a function can be partially applied, meaning that some of its parameter values can be specified (bound) before the function is called. Partial function application yields a new function.

So given that I have a class:

 class MyHelloWorldClass {
 
  func helloWithName(name: String) -> String {
  return "hello, \(name)"
  }
 }

I can create a variable that points to the class’s helloWithName function:

 let helloWithNameFunc = MyHelloWorldClass.helloWithName
 // MyHelloWorldClass -> (String) -> String

My new helloWithNameFunc is of type MyHelloWorldClass -> (String) -> String ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required