By Encore PTL

2014-06-08 14:36:01 8 Comments

I want to make a function that return a curry function like below

func addTwoNumbers(a: Int)(b: Int) -> Int {
    return a + b

addTwoNumbers(4)(b: 6) // Result: 10

var add4 = addTwoNumbers(4)
add4(b: 10) // returns 14     

What is the return type of such function and how can I generate a function like this using a function that take Variadic parameters.

func generateCurry(.../*Variadic parameters*/) -> .../*curry function type*/ {
  return ...//curry function

I want a generic solution and not take only Int as arguments in the parmeter of the generateCurry function

let curried = curry(func(a, b, c) {
  print(a + b + c)
curried(1)(2)(3) //prints 6


@werediver 2017-01-24 09:10:26

In case you want to quickly get the curry function for any number of parameters, it's possible to generate it as shown in this gist.

The code is in Swift 2.2 and generates code for Swift 2.2 (at the moment). It uses simple template-based approach (a possible alternative is constructing an AST followed by code-generation):

func genCurry(n: Int, indent: Indent = .fourSpaces, accessLevel: AccessLevel = .Default, verbose: Bool = false) -> String {

    // ...
    // The bulky park is skipped for clarity.

    return accessLevel.asPrefix + "func curry<\(genericParams)>(f: \(fSig)) -> \(curriedSig(n)) {\n"
        + indent.single + "return \(closure)\n"
        + "}\n"

@Adam Sharp 2014-08-19 03:16:06

You can achieve this pretty easily with closures:

/// Takes a binary function and returns a curried version
func curry<A,B,C>(f: (A, B) -> C) -> A -> B -> C {
    return { a in { b in f(a, b) } }

curry(+)(5)(6) // => 11

let add: Int -> Int -> Int = curry(+)
add(5)(6) // => 11

It would be really nice to be able to do the same thing for functions that take 3, 4 or more arguments, but without duplicating the implementation. The signature of such a function might start something like:

/// Take a function accepting N arguments and return a curried version
func curry<T>(args: T...) -> /* ? */

What would the return type be? It would change based on the input to the function. This definitely isn't possible in Swift at the moment, and I don't think it would be possible at all without some kind of macro system. But even with macros I don't think the compiler would be satisfied unless it knew the length of the list at compile-time.

Having said that, it's really straight-forward to manually overload the currying function with a version that accepts 3, 4, 5 or more parameters:

func curry<A,B,C,D>(f: (A, B, C) -> D) -> A -> B -> C -> D {
    return { a in { b in { c in f(a,b,c) } } }

func curry<A,B,C,D,E>(f: (A, B, C, D) -> E) -> A -> B -> C -> D -> E {
    return { a in { b in { c in { d in f(a,b,c,d) } } } }

// etc.

@k06a 2014-11-11 21:10:18

Awesome answer! Thank you!

@Bryan McLemore 2014-06-08 14:45:19

I'm not sure this is actually going to be possible in the same way it is inside of languages like Python.

The core problem I see to having a single generic solution is the strong typing of the closures/funcs you want to accept.

You could fairly easily create a curry function that worked on a specific or common function signature, but as far as a general purpose curry I don't see a way for it to work. The issue is more than about the types of the arguments (as mentioned in comments) but also with the number of them.

I've written up a simple example of how you could implement a curry function. It works, but I don't see a sane way to have a truly generic one like you can in more loosely typed languages.

func add(a1: Int, a2: Int) -> Int {
    return a1 + a2

func curry(argument: Int, block: (Int, Int) -> Int) -> Int -> Int{
    func curried(arg: Int) -> Int {
        return block(argument, arg)

    return curried

curry(5, add)(6)

@Encore PTL 2014-06-08 14:49:09

We can use generics and make sure all arguments passed are of type T

@Bryan McLemore 2014-06-08 14:52:22

You still run into the fact that a function with different numbers of arguments is a different signature, and thus a different type. The varadic requirement is part of the problem.

@Encore PTL 2014-06-08 15:59:42

If we can solve the issue of passing an array in a Variadic function then we can solve this issue. Is there a way in the language to call Variadic method like performSelector in Objective C or can we bridge to Objective C to make it work?

@Bryan McLemore 2014-06-08 17:52:20

In swift the variadic implementation appears to effectively be an array. I did just try to implement a version of my example that uses Any to some effect. I was able to crash Xcode using it and got a truly impressive compiler error by doing var test: Any = curry(5,add). I'm keep getting back to a generic curry being impossible in swift, or minimally way more effort than it's worth.

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] What is 'Currying'?

20 Answered Questions

[SOLVED] #pragma mark in Swift?

  • 2014-06-03 14:05:56
  • Arbitur
  • 213190 View
  • 907 Score
  • 20 Answer
  • Tags:   swift

3 Answered Questions

9 Answered Questions

[SOLVED] Swift Beta performance: sorting arrays

2 Answered Questions

[SOLVED] Uncurrying curried Function in Swift

2 Answered Questions

[SOLVED] Why is this function curried?

  • 2018-10-21 22:23:11
  • yesar
  • 56 View
  • 0 Score
  • 2 Answer
  • Tags:   sml currying

15 Answered Questions

[SOLVED] JavaScript curry: what are the practical applications?

1 Answered Questions

[SOLVED] Swift: Benefits of Curry Function

  • 2015-02-19 01:40:35
  • user1107173
  • 433 View
  • 2 Score
  • 1 Answer
  • Tags:   swift currying

Sponsored Content