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
func fix<T, U>(_ f: @escaping (T, @escaping (T) -> U) -> U) -> (T) -> U { | |
func impl(_ p: T) -> U { | |
return f(p, fix(f)) | |
} | |
return impl | |
} |
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
// params — массив, в котором true соответствует параметру, который останется в результирующей функции | |
// а false — параметр, который необходимо частично применить | |
func makeApply(params: [Bool], throws isThrow: Bool) -> String { | |
let isShortForm = params.prefix(while: { $0 }).count == params.filter { $0 }.count | |
var result = "func papply<" | |
result += (1...params.count).map { "T\($0)" }.joined(separator: ", ") | |
result += ", TResult>(_ f: @escaping (" | |
result += (1...params.count).map { "T\($0)" }.joined(separator: ", ") | |
result += isThrow ? ") throws -> TResult, " : ") -> TResult, " | |
result += params.enumerated().filter { $0.element }.map { $0.offset + 1 }.map { (isShortForm ? "_ " : "") + "p\($0): T\($0)" }.joined(separator: ", ") |
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
final class Counter { | |
var count = 0 | |
} | |
func predicate(value: Int, counter: Counter) -> Bool { | |
if value > 0 { | |
counter.count += 1 | |
return true | |
} | |
return false |
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
func papply<T1, T2, T3, TResult>(_ f: @escaping (T1, T2, T3) throws -> TResult, p3: T3) -> (T1, T2) throws -> TResult { | |
func apply(p1: T1, p2: T2) throws -> TResult { | |
return try f(p1, p2, p3) | |
} | |
return apply(p1:p2:) | |
} |
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
func makeUncurry(paramsCount: Int, throws isThrow: Bool) -> String { | |
var result = "func uncurry<T" | |
result += (0..<paramsCount).map { ", T\($0 + 1)" }.joined() | |
result += ", TResult>(_ f: @escaping (T) -> (" | |
result += (0..<paramsCount).map { "T\($0 + 1)" }.joined(separator: ", ") | |
result += ") " | |
if isThrow { result += "throws " } | |
result += "-> TResult) -> (T" | |
result += (0..<paramsCount).map { ", T\($0 + 1)" }.joined() | |
result += ") " |
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
func uncurry<T, TResult>(_ f: @escaping (T) -> () -> TResult) -> (T) -> TResult { | |
func impl(this: T) -> TResult { | |
return f(this)() | |
} | |
return impl(this:) | |
} |
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
func makeWeakify(paramsCount: Int) -> String { | |
var result = "private struct WeakWrapper\(paramsCount)<T: AnyObject" | |
result += (0..<paramsCount).map { ", T\($0 + 1)" }.joined() | |
result += "> {\n" | |
result += "\(tab)init(_ p: T, _ f: @escaping (T) -> (" | |
result += (0..<paramsCount).map { "T\($0 + 1)" }.joined(separator: ", ") | |
result += ") -> Void) {\n" | |
result += "\(tab)\(tab)self.p = p\n" | |
result += "\(tab)\(tab)self.f = f\n" | |
result += "\(tab)}\n\n" |
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
private struct WeakWrapper0<T: AnyObject> { | |
init(_ p: T, _ f: @escaping (T) -> () -> Void) { | |
self.p = p | |
self.f = f | |
} | |
func invoke() -> Void { | |
guard let p = self.p else { return } | |
f(p)() | |
} |
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
@interface Javascroopt: NSObject | |
@property (nonatomic, strong) int (^addSomething)(int); | |
@property (nonatomic, strong) void (^printSomething)(NSString *); | |
@end | |
@implementation Javascroopt | |
@end |
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
class Javascript { | |
init(dep1: Int, dep2: Int) { | |
some = { p in p + dep1 } | |
any = { v in print("\(dep2) is \(v)") } | |
} | |
let some: (Int) -> Int | |
let any: (String) -> Void | |
} |
NewerOlder