Skip to content

Instantly share code, notes, and snippets.

View Ben-G's full-sized avatar
💭
💻

Benjamin Encz Ben-G

💭
💻
View GitHub Profile
@Ben-G
Ben-G / FunctionFunctor.swift
Created April 27, 2016 14:27
Function as Functor in Swift
func map<A,B,C>(f1: (B) -> C, f2: (A) -> B) -> (A) -> C {
return { f1(f2($0)) }
}
func addOne(i: Int) -> Int {
return i + 1
}
func convertToString(i: Int) -> String {
@Ben-G
Ben-G / NoDynamicConstraint.swift
Created April 13, 2016 05:08
Can't dynamically check for fulfillment of type constraints
//: Playground - noun: a place where people can play
import Cocoa
let a: Int? = 3
let b: Int? = 3
public func equateThem <T: Equatable>(lhs : Optional<T>, rhs : Optional<T>) -> Bool {
return lhs! == rhs!
}
@Ben-G
Ben-G / ConstrainedExtensionsIssue.swift
Created April 12, 2016 05:23
Issue with constrained extensions in Swift 2.2
struct StructTest {}
class ClassTest {}
class WillBeConstrained<T> {}
// Error: type 'T' constrained to non-protocol type 'StructTest'
extension WillBeConstrained where T: StructTest {
}
@Ben-G
Ben-G / ComposeTypes.Swift
Last active March 5, 2016 17:49
Compose Types
// Example of a thing I cannot do with types in Swift
struct Operation<Input, Output> {
// would also be nice to have a list of operations that yield different types (and be able to dynamically inspect the generic type)
var operations: [Operation<>] = []
// This can be accomplished by using type erause
// would be nice to be able to leave a type hole here
var nextOperation: Operation<Output, _> // second generic argument can be any type from the perspective of this specific type
// only important aspect is that input of next operation matches output of this operation
@Ben-G
Ben-G / NonEmptyCollection.swift
Last active February 25, 2016 01:07
Type that forces a collection to not be empty
public struct NonEmptyCollection<T: CollectionType> {
let collection: T
init?(collection: T) {
if !collection.isEmpty {
self.collection = collection
} else {
return nil
}
@Ben-G
Ben-G / UnwrapAll.swift
Last active February 11, 2016 04:37
Generic Function That Unwraps Optional of Arbitrary Depth
// Benjamin Encz
// Note: Proof of Concept, you should definitely not use this!
import Cocoa
public protocol OptionalType {
init()
func unwrap() -> Any
}
@Ben-G
Ben-G / ForwardIndexType.swift
Created February 3, 2016 16:55
"Infinite" loop with ForwardIndexType and `distanceTo`
struct AIndex: ForwardIndexType {
let index: Int
func successor() -> AIndex {
return AIndex(index: self.index + 1)
}
}
func == (lhs: AIndex, rhs: AIndex) -> Bool {
return lhs.index == rhs.index
@Ben-G
Ben-G / NotCrash.swift
Last active January 24, 2016 04:21
Does not crash compiler
public protocol X {
func _ok(a: Any)
}
public protocol StoreSubscriber: X {
typealias StoreSubscriberStateType
typealias AppStateType
func newState(state: StoreSubscriberStateType)
@Ben-G
Ben-G / SwiftFlowRx.swift
Created January 10, 2016 18:00
Swift Flow + RxSwift
class RxObserver<T>: StoreSubscriber {
var state: Variable<T?> = Variable(nil)
private var store: MainStore
init(store: MainStore) {
self.store = store
self.store.subscribe(self)
}
class Parent {
var child: Child? {
didSet {
child.callback = { [weak self] in
delegateCallback()
}
}
}