Skip to content

Instantly share code, notes, and snippets.

View file.js
let thing: ?string = ... // `thing` is a nullable string
if (thing) {
// Within this `if`, `thing` is of type `string`. No longer nullable.
// Outside the `if`, `thing` is still a nullable string.
if (!thing) return
// Since we returned if `thing` is null, `thing` is now of type `string`.
View file.ts
class Class1 {
prop: number
class Class2 {
prop: number
let a: Class1 = new Class1()
a = new Class2()
View gist:0978209efef7774393e0
import Cocoa
class GeneralThing<S> {
var stuff: S?
func doIt() {
if let s = stuff {
echo "*** Removing saved user…"
rm -rf "${HOME}/Library/Application Support/GitHub for Mac/usersByServer.plist"
echo "*** Removing keychain items…"
security -q delete-internet-password -s
security -q delete-generic-password -l 'GitHub for Mac —'
View gist:cecd44aab46f6f40ed0e
func renderForm(state: State) -> Element<State> {
let incButton = Button(title: "Increment", fn: { mapCount($0, inc) })
|> sizeToFit
|> offset(0, 40)
let decButton = Button(title: "Decrement", fn: { mapCount($0, dec) })
|> sizeToFit
let count = Label<State>(text: "\(state.count)")
|> sizeToFit
View gist:130d86b38e02a8717ba5
Swift: Wanted: Generalized chaining
Swift's Optional chaining is super cool.
It'd be really really cool if we could generalize that (monads!) to Haskell-like do-syntax.
Steps to Reproduce:
View gist:1a3d6af1a81e86fb6322
class F<A> {
protocol Functor {
typealias A
typealias B
typealias FA = F<A>
typealias FB = F<B>
func fmap(a: FA, fn: (A -> B)) -> FB
View gist:6673597
RACSignal *a = [b flattenMap:(id x) {
return [RACSignal error:[NSError error...]];