Skip to content

Instantly share code, notes, and snippets.

View jmont's full-sized avatar

Juan Carlos Montemayor Elosua jmont

View GitHub Profile
@jmont
jmont / gist:9003277
Created February 14, 2014 15:43
NSCoding mock implementation
- (id)xxx_decoder:(NSCoder *)decoder decodeObjectOfClass:(Class)class forKey:(id)key {
id obj = [decoder decodeObjectForKey:key];
if (![obj isKindOfClass:class]) {
return nil;
}
return obj;
}
@jmont
jmont / maybe.m
Last active March 30, 2016 16:27
Swift Monads
// Swift Monads -- Maybe
// Juan C. Montemayor (@norsemelon)
// This operator can be used to chain Optional types like so:
// optionalVal >>= f1 >>= f2
// where f1 and f2 have type `Any -> Any?`
//
// If a value is ever nil, the chain short-circuits and will result in nil.
// This is a much neater way to do this than using the if syntax specified in
// the Swift iBook.
@jmont
jmont / curriedfuncs.m
Last active August 29, 2015 14:02
Curried Functions in Swift
// Curried Functions in Swift
// Juan C. Montemayor (@norsemelon)
// Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
// Syntax: func name (x1: t1)(x2: t2) -> t { ... }
// Note: it has a paren pair for every variable, as opposed to `(x1: t1, x2: t2)`
func addTwoNumbers(a: Int)(b: Int) -> Int {
return a + b
}
@jmont
jmont / funccomp.m
Created June 4, 2014 19:41
[WIP] Function Composition in Swift
operator infix -%- { associativity left }
func -%-<TypeA, TypeB, TypeC> (f: TypeB -> TypeC, g: TypeA -> TypeB) -> (TypeA -> TypeC) {
return ({ x in f(g(x)) })
}
func add3(x:Int) -> Int {
return x + 3;
}
func add7(x: Int) -> Int {

Keybase proof

I hereby claim:

  • I am jmont on github.
  • I am jmont (https://keybase.io/jmont) on keybase.
  • I have a public key whose fingerprint is 9416 F219 A1B6 A6E5 0AA8 B371 793F 0B31 DB85 519F

To claim this, I am signing this object:

@jmont
jmont / upserting_spread_sequelize.js
Last active May 6, 2016 18:41
Upserting using the spread function and Sequelize
// The spread function!
Promise.resolve([thing1, thing2, thing3]).spread((thing1, thing2, thing3) => { /* Do something! */ })
// Upserting a sample Movie model
import Models from 'db/models/index'
updateMovie (movie, params) {
return this.updateMovieObject(movie, params).spread(this.upsertTagsForMovie).spread(this.updateMovieInfo).spread((movie, params) => movie)
@jmont
jmont / upsert.js
Created May 4, 2016 21:51
Upserting models using the spread function
// The spread function!
Promise.resolve([thing1, thing2, thing3]).spread((thing1, thing2, thing3) => { /* Do something! */ })
// Upserting a sample Movie model
import Models from 'db/models/index'
updateMovie (movie, params) {
return this.updateMovieWParams(movie, params).spread(this.upsertTagsForMovie).spread(this.updateMovieInfo).spread((movie, params) => movie)