Skip to content

Instantly share code, notes, and snippets.

@stephantabor
stephantabor / bb.js
Last active January 6, 2024 04:18
Bluebird .each vs .mapSeries vs .map
var Promise = require('bluebird');
var funcs = Promise.resolve([500, 100, 400, 200].map((n) => makeWait(n)));
funcs
.each(iterator) // logs: 500, 100, 400, 200
.then(console.log) // logs: [ [Function], [Function], [Function], [Function] ]
funcs
.mapSeries(iterator) // logs: 500, 100, 400, 200
const touchStart = Rx.Observable.fromEvent(window, 'touchstart').timestamp();
const touchMove = Rx.Observable.fromEvent(window, 'touchmmove');
const touchEnd = Rx.Observable.fromEvent(window, 'touchend').timestamp();
const touchAndHold = touchStart
.flatMap(() => touchEnd.takeUntil(touchMove), (x, y) => { start: x.timestamp, end: y.timestamp })
.filter(ts => (ts.end - ts.start) / 1000 > 2);
const subscription = touchAndHold.subscribe(
() => console.log('touch and hold!')
@davbeck
davbeck / assets.rb
Last active January 18, 2023 01:35
Automatic Enum-based Xcassets in Swift
#!/usr/bin/env ruby
require "active_support"
def assetCatalogsAtPath(path)
results = []
contents = Dir.entries(path)
@rnapier
rnapier / gist:dbffbf54274a880a6ac7
Last active July 12, 2016 01:29
More exploration of guard/try and crazy operator idea
// This is pretty clean, but I often dislike chains of temporary variables
// They tend to lead to little bugs when you use the wrong one. Swift's warnings
// and 'let' reduce problems, though.
func pagesFromOpenSearchData(data: NSData) throws -> [Page] {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
guard let array = json as? [JSON] else { throw JSONError.BadArray(json) }
guard case let value = array[1] where array.count >= 2 else { throw JSONError.OutOfRange }
guard let titles = value as? [String] else { throw JSONError.BadStringList(json) }
@justinwoo
justinwoo / using-rxjs-instead-of-flux-with-react.md
Last active October 21, 2023 10:16
Using RxJS instead of Flux with React to organize data flow

Reposted from Qiita

For almost a year now, I've been using this "flux" architecture to organize my React applications and to work on other people's projects, and its popularity has grown quite a lot, to the point where it shows up on job listings for React and a lot of people get confused about what it is.

Why I'm tired of using and teaching flux

There are a billion explainations on the internet, so I'll skip explaining the parts. Instead, let's cut to the chase -- the main parts I hate about flux are the Dispatcher and the Store's own updating mechanism.

If you use a setup similar to the examples in facebook/flux, and you use flux.Dispatcher, you probably have this kind of flow:

Copy and paste the swift code below into a playground to experiment.

This is a very close emulation of Functor and Monad typeclasses in swift. As of Swift 1.2 and Xcode 6.3, this is no longer very fragile.

Unfortunately, the compiler cannot verify the types when passing a function to (>>=). We have to wrap the function in a closure and call it with an explicit argument to compile.

optionalDoubles >>= squareRoot // doesn't compile
optionalDoubles >>= { squareRoot($0) } // compiles
@JaviLorbada
JaviLorbada / FRP iOS Learning resources.md
Last active April 8, 2024 18:07
The best FRP iOS resources.

Videos

@steipete
steipete / workaround.m
Created January 6, 2015 22:14
If you're implementing child view controllers and want automaticallyAdjustsScrollViewInsets to work...
// This ensures that the automaticallyAdjustsScrollViewInsets magic works
// On our newly added view controller as well.
// This triggers _layoutViewController which then triggers
// _computeAndApplyScrollContentInsetDeltaForViewController:
// which finally updates our content inset of the scroll view (if any)
// rdar://19053416
[self.navigationController.view setNeedsLayout];
// Some global place
class CollisionMapping
{
enum Layer : Uint32 {
Terrain,
Actors,
}
// Maps individual collision types (bird, pipe etc) to a smaller set of
// more generic types to make them more manageable
@lucamarrocco
lucamarrocco / app.swift
Last active January 30, 2024 11:00
swift osx application without nib
import Cocoa
class WindowController: NSWindowController {
}
class AppDelegate: NSObject {
var mainWindow: NSWindow?
var mainController: NSWindowController?
}