This file contains hidden or 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
| %{ | |
| def commaSep(array): | |
| return ', '.join(array) | |
| def closure(n, param): | |
| if n == 1: | |
| return '{ p' + str(len(param) - n) + ' in f(' + commaSep(param) + ') } ' | |
| return '{ p' + str(len(param) - n) + ' in ' + closure(n - 1, param) + '} ' |
This file contains hidden or 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
| extension Collection { | |
| func list3() -> [[Element]] { | |
| zip(0..., self).flatMap { (i: Int, e0: Element) -> [[Element]] in | |
| zip((i + 1)..., self[index(startIndex, offsetBy: (i + 1))...]).flatMap { (j: Int, e1: Element) -> [[Element]] in | |
| self[index(startIndex, offsetBy: (j + 1))...].map { (e2: Element) -> [Element] in | |
| [e0, e1, e2] | |
| } | |
| } |
This file contains hidden or 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
| extension Collection { | |
| func list3() -> [[Element]] where Index == Int { | |
| zip(0..., self).flatMap { (i: Int, e0: Element) -> [[Element]] in | |
| zip((i + 1)..., self[(i + 1)...]).flatMap { (j: Int, e1: Element) -> [[Element]] in | |
| self[(j + 1)...].map { (e2: Element) -> [Element] in | |
| [e0, e1, e2] | |
| } | |
| } |
This file contains hidden or 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
| extension Combine.Future { | |
| convenience init<S: Scheduler>(on scheduler: S, _ attemptToFulfill: @escaping (@escaping Future<Output, Failure>.Promise) -> Void) { | |
| self.init { promise in | |
| scheduler.schedule { | |
| attemptToFulfill(promise) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| extension Sequence { | |
| func traverse<T>(_ transform: @escaping (Element) -> AnyPublisher<T, Never>) -> AnyPublisher<[T], Never> { | |
| var index = 0 | |
| return self | |
| .publisher | |
| .flatMap { i -> AnyPublisher<(Int, T), Never> in | |
This file contains hidden or 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
| (0...3) | |
| .publisher | |
| .flatMap { i -> AnyPublisher<Int, Never> in | |
| Future<Int, Never> { promise in | |
| DispatchQueue.global().async { | |
| print("start", i, "----", Thread.current) | |
| sleep(UInt32([0, 2, 3, 5].randomElement()!)) | |
| print("end", i) |
This file contains hidden or 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
| (0...3) | |
| .map { i -> AnyPublisher<Int, Never> in | |
| Future<Int, Never> { promise in | |
| DispatchQueue.global().async { | |
| print("start", i, "----", Thread.current) | |
| sleep(UInt32([0, 2, 3, 5].randomElement()!)) | |
| print("end", i) | |
This file contains hidden or 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
| (0...3) | |
| .map { i -> AnyPublisher<Int, Never> in | |
| Future<Int, Never> { promise in | |
| print("start", i) | |
| sleep(UInt32([0, 2, 3, 5].randomElement()!)) | |
| print("end", 1) | |
| promise(.success(i * 2)) |
This file contains hidden or 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 | |
| extension Array { | |
| func binarySearch(_ f: (Element) -> ComparisonResult) -> Element? { | |
| self[...].binarySearch(f) | |
| } | |
| } | |
| extension ArraySlice { | |
| func binarySearch(_ f: (Element) -> ComparisonResult) -> Element? { | |
This file contains hidden or 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 func contentImage(in rect: CGRect) -> UIImage { | |
| UIGraphicsImageRenderer(bounds: rect) | |
| .image { con in | |
| textField.layer.render(in: con.cgContext) | |
| } | |
| } |