Skip to content

Instantly share code, notes, and snippets.

protocol ClientType: Hashable {
var fullName: String { get }
}
extension ClientType {
var hashValue: Int {
return fullName.hashValue
}
}
/*:
Brandon Williams
# Lenses in Swift
*/
/*:
## i.e. Functional getters and setters
//: Playground - noun: a place where people can play
import Cocoa
import Foundation
// WinterBuddies a social network to connect you with people who can help you survive the winter
class ProfileViewController : UIViewController {
let musicService = MusicService()
//
// Signal+Extensions.swift
// Khan Academy
//
// Created by Nacho Soto on 10/1/15.
// Copyright © 2015 Khan Academy. All rights reserved.
//
import ReactiveCocoa
First step is to desugar do notation. In this case we want to translate it to the bind (>>=) operator:
greeter =
name >>= ask
return ("hello, " ++ name ++ "!")
Now that it's desugared (not convinced I've done it correctly) I will translate it to Swift.
I'm relying on the Reader monad implementation in Swiftz (https://github.com/typelift/Swiftz/blob/968075391aedb15ec51ff9d5b7d4921b8fa3a3c6/Swiftz/Reader.swift)
Current code is:
@adamkuipers
adamkuipers / hk.swift
Last active December 8, 2015 19:46
Higher-kinded types encoded as path-dependent types
// Based off of https://gist.github.com/runarorama/33986541f0f1ddf4a3c7
protocol λ {
typealias α
}
struct K<L: λ>: λ {
typealias α = L.α
}
@runarorama
runarorama / gist:33986541f0f1ddf4a3c7
Created May 7, 2015 14:06
Higher-kinded types encoded as path-dependent types
trait λ {
type α
}
trait Functor extends λ {
type α <: λ
def map[A,B](x: α { type α = A })(f: A => B): α { type α = B }
}
@chriseidhof
chriseidhof / TypedNotifications.swift
Created January 26, 2015 14:41
Typed Notifications
import Foundation
class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
}
struct Notification<A> {
let name: String
}
import Prelude
public struct Lens<A, B> {
private let get: A -> B
private let set: (A, B) -> A
public init(get: A -> B, set: (A, B) -> A) {
self.get = get
self.set = set
}
@rjchatfield
rjchatfield / transducers.swift
Last active January 13, 2019 13:41
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] }