Skip to content

Instantly share code, notes, and snippets.

View mbrandonw's full-sized avatar

Brandon Williams mbrandonw

View GitHub Profile
@mbrandonw
mbrandonw / gist:c5bc1a84f4c749a87b0f
Created June 7, 2014 19:00
Generic function composition in Swift
@infix func * <A,B,C> (g: B -> C, f: A -> B) -> (A) -> C {
return {(x: A) -> C in
return g(f(x))
}
}
var f = {(x: Int) -> Float in
return Float(x * x) / 2.0
}
@mbrandonw
mbrandonw / gist:ba93c363f67291c2b5ec
Last active November 22, 2021 22:04
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> {
@mbrandonw
mbrandonw / gist:535295308a89da6a50fd
Last active August 29, 2015 14:02
`compact` function in Swift
func compact <A> (xs: [A?]) -> [A] {
return xs.reduce([]) { accum, x in
if let x = x {
return accum + [x]
}
return accum
}
}
var xs: [Int?] = [1, 2, nil, 3]
@mbrandonw
mbrandonw / gist:df260f19872a9c3c74e8
Created June 11, 2014 14:28
Uninitialized string
vWelcome to Swift! Type :help for assistance.
a 1> var x:String
x: String = {
core = {
_baseAddress = Builtin.RawPointer = 0x0000000000000000
_countAndFlags = 0
_owner = None
}
}
2> "test \(x)"
@mbrandonw
mbrandonw / gist:5318952dc53ac7be0b95
Last active August 29, 2015 14:04
Overloading Swift functions with only return type.
func f (x: Int) -> Int {
return 1 + x + x*x
}
func f (x: Int) -> String {
return "result = \(f(x) as Int)"
}
// awesome!
var x: Int = f(2) // 7
@mbrandonw
mbrandonw / gist:7f46100a6254425ab37f
Last active August 29, 2015 14:04
Monadic units with function overloading.
func unit <A> (x: A) -> A? {
return Optional.Some(x)
}
func unit <A> (x: A) -> [A] {
return [x]
}
// hypothetical examples
func unit <A> (x: A) -> Tree<A> {
@mbrandonw
mbrandonw / 1-Functor-and-Monad.md
Last active June 4, 2022 02:12
Swift Functor and Monad

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. However, it is very fragile (i.e. easy to crash the compiler).

For example, instance methods of fmap will run fine, but attempting to use a globally defined fmap that acts on Functor types will cause a crash. Similarly for bind. Unfortunately this means we cannot define the nice infix operator versions of these functions.

@mbrandonw
mbrandonw / gist:ece3afb7597f2ceb544e
Created October 18, 2014 14:05
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> {
@mbrandonw
mbrandonw / readme.md
Created October 26, 2014 23:47
Project Euler #5 Playground

Copy and paste this into a playground to play around.

import Foundation
/**
Num typeclass
*/
protocol Num {
func + (lhs: Self, rhs: Self) -> Self
func - (lhs: Self, rhs: Self) -> Self
func * (lhs: Self, rhs: Self) -> Self
func / (lhs: Self, rhs: Self) -> Self