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
| URLSession.shared | |
| .dataTaskPublisher(for: url) | |
| .map(\.data) | |
| .decode(type: MoviesPage.self, decoder: JSONDecoder()) |
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
| // Obj-C | |
| - (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *) | |
| urlcompletionHandler:(void (^)( | |
| NSData * _Nullable data, | |
| NSURLResponse * _Nullable response, | |
| NSError * _Nullable error) | |
| )completionHandler; | |
| // Swift | |
| func dataTask( |
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
| public extension URLSession { | |
| typealias DataTaskResult = Result<(data: Data, response: URLResponse), Error> | |
| typealias DataTaskResultCompletion = (DataTaskResult) -> Void | |
| func dataTask( | |
| with url: URL, | |
| completion: @escaping DataTaskResultCompletion | |
| ) -> URLSessionDataTask { | |
| dataTask(with: url) { (data, response, error) in | |
| if let error = error { |
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
| public extension Result where Success == (data: Data, response: URLResponse) { | |
| func map<T>(_ keyPath: KeyPath<Success, T>) -> Result<T, Failure> { | |
| switch self { | |
| case .success(let response): | |
| return .success(response[keyPath: keyPath]) | |
| case .failure(let error): | |
| return .failure(error) | |
| } | |
| } | |
| } |
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
| public protocol DecodableDataDecoder { | |
| func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable | |
| } | |
| extension JSONDecoder: DecodableDataDecoder {} | |
| extension PropertyListDecoder: DecodableDataDecoder {} | |
| public extension Result where Success == Data { | |
| func decode<T: Decodable>(_ type: T.Type, using decoder: DecodableDataDecoder) -> Result<T, Error> { | |
| do { |
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
| func geNowPlaying(completion: @escaping (Result<MoviesPage, Error>) -> Void) { | |
| URLSession.shared | |
| .dataTask(with: url) { result in | |
| completion( | |
| result | |
| .map(\.data) | |
| .decode(MoviesPage.self, using: JSONDecoder()) | |
| ) | |
| }.resume() | |
| } |
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
| func string(from int: Int) -> String { | |
| String(int) | |
| } |
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
| func compose<A, B, C>( | |
| f: @escaping (A) -> B, // f: A -> B | |
| g: @escaping (B) -> C // g: B -> C | |
| ) -> (A) -> C { // h: A -> C = g • f | |
| return { a in | |
| g(f(a)) // g • f | |
| } | |
| } | |
| // ... then |
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
| infix operator <> : AdditionPrecedence | |
| public protocol Semigroup { | |
| static func <> (left: Self, right: Self) -> Self | |
| } |
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 String: Semigroup { | |
| public static func <> (left: String, right: String) -> String { | |
| left + right | |
| } | |
| } |