Skip to content

Instantly share code, notes, and snippets.

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

Benjamin Encz Ben-G

💭
💻
View GitHub Profile
//: Playground - noun: a place where people can play
import Foundation
/*
Basic Monad according to definition in: http://stackoverflow.com/questions/27750046/is-a-collection-with-flatmap-a-monad
The requirements for being a monad
- A monad has to be a type constructor F[_] that takes one type argument. For example, F could be List, Function0, Option, etc.
@Ben-G
Ben-G / OptionalMap.swift
Created June 8, 2015 04:08
"Optional chaining for functions" using map and curried functions
var a: String? = "a"
var b: String? = "b"
func twoParamFunction(a: String)(b:String) {
println("\(a)\(b)")
}
a.map(twoParamFunction).map { b.map($0)}
//: Playground - noun: a place where people can play
/*
Compiler Error:
candidate exactly matches
func doSomething() {
^
*/
struct A:B,C {
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
let greeting: String = "hello, world!"
let capitalizedGreeting = greeting.capitalizedString
let testString = "👴🙅HahaśāGœ"
@Ben-G
Ben-G / MulticastDelegate
Created July 13, 2015 23:31
Multicast Delegate Swift Prototype (WIP - currently storing strong references to delegates)
// Multicast delegate prototype. Better solution in Obj-C here: http://arielelkin.github.io/articles/objective-c-multicast-delegate/
// Implementations
class CarListener: Hashable {
var hashValue = Int(arc4random())
func receivedNoise(noiseEmitter: CarListenable) {
@Ben-G
Ben-G / DefaultValuesIssues.swift
Created July 28, 2015 21:57
Edge cases with default values for methods in Swift
//: Playground - noun: a place where people can play
import UIKit
protocol APIClientProtocol {
init()
init(urlSession: NSURLSession)
init(apiKey: String)
init(urlSession: NSURLSession, apiKey: String)
@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)
import Foundation
class User: NSObject {
dynamic var name: String?
}
class Observer: NSObject {
var user: User
init(user: User) {
struct Car: StringLiteralConvertible {
enum CarType: String {
case Mercedes, Porsche, Ford, Unknown
}
var type: CarType
typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
@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!