#Every Single Option Under The Sun
- optimization level options
- automatic crashing options
- debug info options
- swift internal options
- swift debug/development internal options
- linker-specific options
- mode options
@dynamicMemberLookup | |
struct CoW<T> { | |
init(_ value: T) { | |
_storage = Storage(value) | |
} | |
public subscript<V>(dynamicMember keyPath: WritableKeyPath<T, V>) -> V { | |
get { value[keyPath: keyPath] } | |
set { value[keyPath: keyPath] = newValue } | |
} | |
var value: T { |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE TupleSections #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE RankNTypes #-} | |
import Control.Applicative | |
import Control.Comonad | |
import Data.Bifunctor |
#Every Single Option Under The Sun
// Playground - noun: a place where people can play | |
public func id<A>(x : A) -> A { | |
return x | |
} | |
public func error<A>(x : String) -> A { | |
assert(false, x) | |
} |
public func unsafeCoerce<A, B>(_ x : A) -> B { | |
return unsafeBitCast(x, to: B.self) | |
} | |
func Y<A>(_ f : @escaping (A) -> A) -> A { | |
return { (x : @escaping (A) -> A) in | |
return f((x(unsafeCoerce(x)))) | |
}({ (x : A) -> A in | |
let fn : (A) -> A = unsafeCoerce(x) | |
return f(fn(x)) |
Copy and paste the swift code below into a playground to experiment.
This is a very close emulation of Functor and Monad typeclasses in swift. As of Swift 1.2 and Xcode 6.3, this is no longer very fragile.
Unfortunately, the compiler cannot verify the types when passing a function to (>>=)
. We have to wrap the function in a closure and call it with an explicit argument to compile.
optionalDoubles >>= squareRoot // doesn't compile
optionalDoubles >>= { squareRoot($0) } // compiles
Copy and paste the swift code below into a playground to experiment.
This is a very close emulation of Functor and Monad typeclasses in swift. However, it is very fragile (i.e. easy to crash the compiler).
For example, instance methods of fmap
will run fine, but attempting to use a globally defined fmap
that acts on Functor
types will cause a crash. Similarly for bind
. Unfortunately this means we cannot define the nice infix operator versions of these functions.
protocol Functor { | |
func fmap <A, B> (Self<A>, A -> B) -> Self<B> | |
// ^ ERROR: Cannot specialize non-generic type '`Self`' | |
} | |
enum Maybe<A> : Functor { | |
case Nothing | |
case Just(A) | |
func fmap<B>(x: Maybe<A>, f: A -> B) -> Maybe<B> { |
// | |
// Yoneda.swift | |
// Basis | |
// | |
// Created by Robert Widmann on 12/11/14. | |
// Copyright (c) 2014 Robert Widmann. All rights reserved. | |
// | |
import Basis |