Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
chriseidhof / type-constraints.swift
Last active August 29, 2015 14:02
Swift Constraints on extension?
enum Sum<L,R> {
case Left(@auto_closure () -> L)
case Right(@auto_closure () -> R)
}
protocol GShow {
func gshow() -> String
}
extension String : GShow {
enum PageType {
case Markdown
case HTML
case XML
}
extension PageType : DebugPrintable {
var debugDescription : String {
switch self {
case .Markdown: return "Markdown"
@chriseidhof
chriseidhof / Result.m
Created September 11, 2014 16:35
nilResultAfterTimeOut
id nilResultAfterTimeOut(int64_t timeoutInNanoSeconds, ResultBlock block) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_time_t timeoutTime = dispatch_time(DISPATCH_TIME_NOW, timeoutInNanoSeconds);
__block id result = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
result = block();
dispatch_semaphore_signal(semaphore);
});
@chriseidhof
chriseidhof / async.swift
Last active August 29, 2015 14:08
Parallel API
enum Either<A,B> {
case Left(A)
case Right(B)
}
// Not sure about which cases this should have...
enum Async<A> {
case Delayed(() -> Async<A>)
case Result(A)
case Failure(NSError)
@chriseidhof
chriseidhof / :(
Created November 4, 2014 10:41 — forked from kostiakoval/:(
import Foundation
import ImageIO
infix operator >>= { associativity left }
func >>=<A,B>(l: A?, r: A -> B?) -> B? {
if let x = l {
return r(x)
}
return nil
import Foundation
// A bunch of convenience things
func const <A, B> (b: B) -> A -> B {
return { _ in b }
}
func repeat <A> (n: Int) -> A -> [A] {
return { a in
return map(Array(1...n), const(a))
}
  • One

    var x = hello
    World
    Test
//
// main.swift
// MCE
//
// Created by Chris Eidhof on 05/02/15.
// Copyright (c) 2015 Chris Eidhof. All rights reserved.
//
import Foundation