Created
June 18, 2017 12:12
-
-
Save carlossless/0b2117461283ea93b99e71afbfcc45c2 to your computer and use it in GitHub Desktop.
A little Swift 4.0 script to build uncurry functions for https://github.com/carlossless/Uncurry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
var chars = "ABCDEFGHIJKLMNOPQRSTU" | |
let charStrings = chars.characters | |
.map{ String($0) } | |
func buildFunc(charStrings: [String], index: Int) -> String { | |
let lowerCaseChars = charStrings.map { $0.lowercased() } | |
let upperCaseChars = charStrings.map { $0.uppercased() } | |
let genericParams = upperCaseChars | |
.joined(separator: ", ") | |
let headParams = upperCaseChars[..<(upperCaseChars.count - 1)] | |
let headLabels = lowerCaseChars[..<(lowerCaseChars.count - 1)] | |
let params = headParams | |
.map { "(\($0))" } | |
.joined(separator: " -> ") | |
let lastParam = genericParams.last! | |
let resultParams = headParams | |
.joined(separator: ", ") | |
let closureSignatureParams = zip(headLabels, headParams) | |
.map { "\($0.0): \($0.1)" } | |
.joined(separator: ", ") | |
let closureInvocations = headLabels | |
.map { "(\($0))" } | |
.joined(separator: "") | |
return """ | |
public func uncurry\(index)<\(genericParams)>(_ function: @escaping \(params) -> \(lastParam)) -> (\(resultParams)) -> \(lastParam) { | |
return { (\(closureSignatureParams)) -> \(lastParam) in function\(closureInvocations) } | |
} | |
""" | |
} | |
let range = 2..<chars.characters.count | |
let functions = range.map { i in | |
return buildFunc(charStrings: Array(charStrings[..<i]), index: i - 1) | |
} | |
print(functions.joined(separator: "\n\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment