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 / presentation.md
Created June 28, 2014 13:00
MobileOptimized Presentation

[fit] Functional Programming in

[fit] Swift

Chris Eidhof - objc.io - June 28, Minsk



@chriseidhof
chriseidhof / parsing.swift
Last active April 16, 2023 02:38
JSON Parsing in Swift
// This code accompanies a blog post: http://chris.eidhof.nl/posts/json-parsing-in-swift.html
//
// As of Beta5, the >>= operator is already defined, so I changed it to >>>=
import Foundation
let parsedJSON : [String:AnyObject] = [
"stat": "ok",
"blogs": [
@chriseidhof
chriseidhof / main.swift
Created August 17, 2014 17:45
Accelerate-wrapper
//
// main.swift
// Accelerate
//
// Created by Chris Eidhof on 17/08/14.
// Copyright (c) 2014 Chris Eidhof. All rights reserved.
//
import Foundation
import Accelerate
@chriseidhof
chriseidhof / routes.swift
Created August 17, 2014 21:04
Type-safe routes in Swift
//
// main.swift
// Routes
//
// Created by Chris Eidhof on 17/08/14.
// Copyright (c) 2014 Chris Eidhof. All rights reserved.
//
import Foundation
@chriseidhof
chriseidhof / gcd.swift
Created August 28, 2014 01:08
GCD Wrappers
import Foundation
// Executes an array of blocks in parallel, but only returns after they're all done.
func parallel(blocks: [() -> ()]) {
let group = dispatch_group_create()
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
for block in blocks {
dispatch_group_async(group, queue, block)
}
@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 / swift.tex
Created October 8, 2014 13:44
Listings Swift config
\lstdefinelanguage{swift}
{
morekeywords={
func,if,then,else,for,in,while,do,switch,case,default,where,break,continue,fallthrough,return,
typealias,struct,class,enum,protocol,var,func,let,get,set,willSet,didSet,inout,init,deinit,extension,
subscript,prefix,operator,infix,postfix,precedence,associativity,left,right,none,convenience,dynamic,
final,lazy,mutating,nonmutating,optional,override,required,static,unowned,safe,weak,internal,
private,public,is,as,self,unsafe,dynamicType,true,false,nil,Type,Protocol,
},
morecomment=[l]{//}, % l is for line comment