Skip to content

Instantly share code, notes, and snippets.

// Before realizing that NSFastGenerator exists...
func enumerate(c: NSFastEnumeration) -> SequenceOf<AnyObject> {
return SequenceOf<AnyObject> { () -> GeneratorOf<AnyObject> in
let bufSize = 16
var objects = UnsafeMutablePointer<AnyObject?>.alloc(bufSize)
var state = NSFastEnumerationState()
var currentCount = 0
@hsavit1
hsavit1 / CustomCell.swift
Created December 12, 2015 02:55 — forked from jasdev/CustomCell.swift
An approach to safer UITableViewCell and UICollectionViewCell reuse
class CustomCell: UITableViewCell, Reusable {
class var reuseIdentifier: String {
return "customCell"
}
}
class SupaHotCustomCell: CustomCell {
override class var reuseIdentifier: String {
return "supaHotCustomCell"
}
@hsavit1
hsavit1 / gist:753cdcbaf94b30fabb50
Created December 12, 2015 06:28 — forked from mbrandonw/gist:ba93c363f67291c2b5ec
First attempt at Functor protocol in Swift
protocol Functor {
func fmap <A, B> (Self<A>, A -> B) -> Self<B>
// ^ ERROR: Cannot specialize non-generic type '`Self`'
}
enum Maybe<A> : Functor {
case Nothing
case Just(A)
func fmap<B>(x: Maybe<A>, f: A -> B) -> Maybe<B> {

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
@hsavit1
hsavit1 / transducer-type.swift
Created December 12, 2015 06:31 — forked from mbrandonw/transducer-type.swift
How to get a transducer type without higher kinds
import Foundation
// A bunch of convenience things
func const <A, B> (b: B) -> A -> B {
return { _ in b }
}
func repeat <A> (n: Int) -> A -> [A] {
return { a in
return map(Array(1...n), const(a))
}
@hsavit1
hsavit1 / monad.swift
Created December 12, 2015 06:35 — forked from cobbal/monad.swift
Monad protocol in swift
protocol Monad {
typealias F
typealias U
class func bind<M : Monad where M.U == U>(Self, F -> M) -> M
class func `return`(F) -> Self
}
extension Array : Monad {
typealias F = T
@hsavit1
hsavit1 / AppDelegate.swift
Created December 13, 2015 16:43 — forked from chriseidhof/AppDelegate.swift
Functional Swift Talk
import UIKit
struct Screen<A> {
let run: (A -> ()) -> UIViewController
}
struct Step<A> {
let build: (navigationController: UINavigationController, callback: A -> ()) -> UIViewController
}
@hsavit1
hsavit1 / transducers.swift
Created December 13, 2015 20:15 — forked from rjchatfield/transducers.swift
Transducers & Reducers in Swift 2
//-- This is an introduction to Transducers in Swift, inspired by
// Talk: https://www.youtube.com/watch?v=estNbh2TF3E
// Code: https://github.com/mbrandonw/learn-transducers-playground
// Updated with Swift 2
/**
* Define a few test methods
*/
/// REDUCER
func append <A> (xs: [A], x: A) -> [A] { return xs + [x] }
@hsavit1
hsavit1 / array.swift
Created December 14, 2015 01:55 — forked from mikeash/array.swift
class ArrayImpl<T> {
var space: Int
var count: Int
var ptr: UnsafeMutablePointer<T>
init(count: Int = 0, ptr: UnsafeMutablePointer<T> = nil) {
self.count = count
self.space = count
@hsavit1
hsavit1 / gist:a08c7c1fe1decc47fc7c
Created December 14, 2015 02:36 — forked from mbrandonw/gist:ece3afb7597f2ceb544e
Proof of De Morgan's law in Swift
// ~(P ∨ Q) = ~P ∧ ~Q
enum Nothing {}
struct Not <A> {
let not: A -> Nothing
init (_ not: A -> Nothing) { self.not = not }
}
struct And <A, B> {