Swift should support currying/partial application for default infix operators

Originator:kurz
Number:rdar://17729052 Date Originated:18-Jul-2014 08:49 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 6 Beta 3
Classification:Enhancement Reproducible:Always
 
Summary:
It should be easy to partially apply an operator(-function) like '*' or '+'

Steps to Reproduce:
Write the following code in Swift:

let addTwo: Int -> Int = (+)(2)
let timesTwo: Int -> Int = (*)(2)

let a = addTwo(3)
let b = timesTwo(3)

let listA = [2,3,5,10].map(addTwo)
let listB = [2,3,5,10].map(timesTwo)

Expected Results:
The code compiles and creates partially applied versions of the predefined operator functions

Actual Results:
Compiler error:
Playground execution failed: error: <EXPR>:16:27: error: 'UInt8' is not convertible to 'Int -> Int'
let addTwo: Int -> Int = (+)(2)

Comments

It would be nice if Swift had better built-in support for partial application and currying. FWIW, here are some little functions I've written to sorta do this:

// fix(x, op) binds the first argument to x
func fix<T0, T1, T>(t0: T0, f: (T0, T1) -> T) -> T1 -> T {
    return { t1 in f(t0, t1) }
}

// fix(op, x) binds the last argument to x
func fix<T0, T1, T>(f: (T0, T1) -> T, t1: T1) -> T0 -> T {
    return { t0 in f(t0, t1) }
}

// Examples

let addOne = fix(1, +)

addOne(2)                                                        // 3
addOne(9)                                                        // 10

let lessThanFour = fix(<, 4)

lessThanFour(3)                                                  // true
lessThanFour(5)                                                  // false
By kristopherdjohnson at July 24, 2014, 2:26 p.m. (reply...)

Do you have expected results and actual results reversed here?

I follow this radar until expected results.

By GriotSpeak at July 19, 2014, 4:16 a.m. (reply...)

got the expected and actual the wrong way round. Fixed that now.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!