Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A little Swift 4.0 script to build uncurry functions for https://github.com/carlossless/Uncurry
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
You can’t perform that action at this time.