Skip to content

Instantly share code, notes, and snippets.

View RuiAAPeres's full-sized avatar
💭
🏔🏃‍♂️

Rui Peres RuiAAPeres

💭
🏔🏃‍♂️
View GitHub Profile
func something() (int,int) {
return 1,2
}
func sum(lhs : Int)(rhs : Int) -> Int {
return lhs + rhs
}
let sum2 = sum (2)
let pair1 = sum2(rhs: 1)
let pair2 = sum2(rhs: 2)
let pair3 = sum2(rhs: 3)
let pair4 = sum2(rhs: 4)
//The magic method
-(id (^)(id,...)) getSelector:(SEL) aSelector forTarget:(id) target withFirstArgument:(id) firstArgument {
id (^blockName)(id,...) = ^id(id values,...) {
id cenas = firstArgument;
SEL theSelector;
NSMethodSignature *aSignature;
NSInvocation *anInvocation;
func curry<T,U,V>(f:(T,U)->V) -> T -> U -> V
{
return {x in { y in f(x,y)}}
}
extension Array {
var decompose : (T,[T])? {
return (count > 0) ? (self[0], Array(self[1..<count])) : nil
}
}
@RuiAAPeres
RuiAAPeres / gist:0b317796f810839fe706
Last active April 13, 2016 03:29
Monads in Swift -
public final class Box<T>
{
let value : T
public init(_ value : T) {
self.value = value
}
}
func flatten<T>(box : Box<Box <T>>) -> Box <T> {
import Foundation
import ReactiveCocoa
final class PriorityAction<T> {
let identifier : String
let action : Action<Void, [T], Error>
init(identifier: String, action: Action<Void, [T], Error>) {
protocol Foo {
typealias T: Hashable
var foos: [T] { get }
}
struct Bar<T: Equatable>: Foo {
let foos: [T]
init(foos: [T]) {
func fetchData(request: NSURLRequest) -> SignalProducer<[Foo], Error> {
return foosFromServer(request).flatMapError {_ in //network failed deal with it}
.flatMapLatest { parseFoos($0).flatMapError {_ in //parse failed deal with it} }
.flatMapLatest { persistFoos($0).flatMapError {_ in //persist failed deal with it} }
.flatMapError { error in loadFoosFromCache().mapError {_ in error} }
}
1. The work should only be started when we spend more than 0.5s in the ViewController
2. Check if there is data in the cache
2.1 Success: Show it and start an internet request if the cache is outdated
2.1.1 Success: Show the data
2.1.2 Failed: Silently fail
2.2 Fail: Try to get data from the internet
2.2.1 Success: Show it
2.2.2 Fail: Show the error
3. Every 5 minutes start internet request check, if cache is outdated && ViewController is visible
3.1 Success: Show the data
class TableDataSource: UITableViewDataSource {
let viewModels: [ViewModel]
init(viewModels: [ViewModel]) {
self.viewModels = viewModels
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {