This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct StructTest {} | |
class ClassTest {} | |
class WillBeConstrained<T> {} | |
// Error: type 'T' constrained to non-protocol type 'StructTest' | |
extension WillBeConstrained where T: StructTest { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
struct Person { | |
let name: String | |
let age: Int | |
} | |
struct Car { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)!) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Term { | |
let offset: Int | |
let value: String | |
} | |
let term = Term(offset: 0, value: "") | |
let output = generateEquatable(term) | |
print(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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] |