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 / 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 / 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 / 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 / ZipWithKey.swift
Created June 9, 2016 23:04
ZipWithKeySwift
//: Playground - noun: a place where people can play
import UIKit
struct Person {
let name: String
let age: Int
}
struct Car {
@Ben-G
Ben-G / ReSwiftEffectsExperiment.swift
Created June 11, 2016 23:32
ReSwift Allow For Effects - Experiment
//: Playground - noun: a place where people can play
import Cocoa
protocol Action {}
final class Store<State> {
typealias Reducer = (Action, State) -> (State, [Any])
var state: State
@Ben-G
Ben-G / LiftTupleToIOU.swift
Created June 21, 2016 22:57
(Don't actually do this) Lift Tuples to IOUs as Workaround for Swift Bug SR-42
// Don't actually do this...
func lift<A, B, C>(input: (A, B, C)) -> (A!, B!, C!) {
return (Optional(input.0)!, Optional(input.1)!, Optional(input.2)!)
}
@Ben-G
Ben-G / CoreDataStack.swift
Last active August 29, 2016 08:49
CoreDataStack Swift
//
// CoreDataStackInMemory.swift
// TripPlanner
//
// Created by Benjamin Encz on 7/20/15.
// Copyright © 2015 Make School. All rights reserved.
//
// Structure is inspired by: http://martiancraft.com/blog/2015/03/core-data-stack/, Thanks!
@Ben-G
Ben-G / GenerateEquatable.swift
Last active October 13, 2016 18:19
Generate Equatable in terms of member fields
import Foundation
struct Term {
let offset: Int
let value: String
}
let term = Term(offset: 0, value: "")
let output = generateEquatable(term)
print(output)
@Ben-G
Ben-G / LiftToOptional.swift
Last active January 24, 2017 16:52
Function that lifts a function with non-optional input to one with optional input.
// Thanks to @jckarter for pointing that this is just Optional's map implementation...
{ $0.map(f) }
/// Takes a function with non-optional input and non-optional return and lifts it to a function
/// with optional input and optional return.
/// The lifted function will return `nil` iff the input is `nil`, otherwise the input will be
/// applied to the original function which will return a non-`nil` value.
public func liftToOptional<T,U>(_ f: @escaping (T) -> U) -> (T?) -> U? {
return { arg in
guard let arg = arg else { return nil }
@Ben-G
Ben-G / UnsafePointerPlayground.swift
Last active February 17, 2017 17:24
Playing with unsafe Pointers
//: Playground - noun: a place where people can play
import Foundation
let arr = [1,5,7,8]
let pointer = UnsafeMutablePointer<[Int]>.alloc(4)
pointer.initialize(arr)
let x = pointer.memory[3]