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
// Consider this function: | |
func fetchPost(by postId: Int, completion: @escaping (Post?, Error?) -> Void) { | |
... | |
} | |
// Using our extension to use an observable | |
// fetchPostObservable looks like: fetchPostObservable(postId) -> Observable<Post> | |
let fetchPostObservable = Observable.fromAsync(postService.getPost(postId:completion:)) | |
// Now to fetch a post you just have to call it just like you would call any function which returns an Observable: | |
fecthPostObservable(10).subscribe(onNext: { (post) 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
enum LogLevel: String { | |
case debug | |
case error | |
} | |
func logMessage(level: LogLevel, message: String) { | |
print("[\(level)] \(message)") | |
} | |
// Without Currying |
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 Observable { | |
// MARK: For completionBlock(Element, Error) template methods | |
public static func fromAsync(_ asyncRequest: @escaping (@escaping (Element?, Error?) -> Void) -> Void) -> Observable<Element> { | |
return Observable.create { (o) -> Disposable in | |
asyncRequest { res, error in | |
if let err = error { | |
o.onError(err) | |
return |
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
// We apply curry to the log message func | |
let logCurried = curry(logMessage) | |
// logCurried can be represented like this: logCurried -> (LogLevel) -> (String) -> Void | |
let debug = logCurried(.debug) | |
// debug can be represented like this: debug -> (String) -> Void | |
debug("Log debug") |
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
enum LogLevel: String { | |
case debug | |
case error | |
} | |
func logMessage(level: LogLevel, message: String) { | |
print("[\(level)] \(message)") | |
} |
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 curry<A, B, C>(_ fn: @escaping (A, B) -> C) -> (A) -> (B) -> C { | |
return { (a: A) in | |
return { (b: B) in | |
return fn(a, b) | |
} | |
} | |
} |
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 collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
guard let dummyCell: OABSubCategoryMenuCell = Bundle.main.loadNibNamed("OABSubCategoryMenuCell", owner: self, options: nil)?.first as? OABSubCategoryMenuCell else { | |
return CGSize.zero | |
} | |
dummyCell.setContent(subCat: self.subCategories[indexPath.row], isSelected: false) | |
dummyCell.setNeedsLayout() | |
dummyCell.layoutIfNeeded() | |
let size: CGSize = dummyCell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize) | |
return size | |
} |
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 enum BundleData | |
{ | |
ItemPosition, | |
UserName | |
} | |
public static class Extensions | |
{ | |
public static int GetBundleData(this Intent intent, BundleData data, int defaultValue) | |
=> intent.Extras.GetInt(data.ToString(), defaultValue); |
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
// | |
// THTriangleView.swift | |
// thirsty | |
// | |
// Created by Werck Ayrton on 23/06/2015. | |
// Copyright (c) 2015 Nyu Web Developpement. All rights reserved. | |
// | |
import UIKit |
NewerOlder