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
infix operator >>> { precedence 50 associativity left } | |
public func >>> <A,B,C>(left: A throws -> B, right: B throws -> C) -> (A throws -> C) { | |
return { (parameter: A) throws -> C in | |
return try right(left(parameter)) | |
} | |
} | |
let data = ... |
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 combine <A,B,C>(first: A throws -> B, _ second: B throws -> C) -> (A throws -> C) { | |
return { (parameter: A) throws -> C in | |
let finalResult = try second(first(parameter)) | |
return finalResult | |
} | |
} | |
let data = //... the NSData representing the json | |
let parseAndValidate = combine(parseJSONFromNSData, validateJSONResponse) |
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 unmarshalJSON<T : Unmarshallable>(type: T.Type)(json: JSON) throws -> T { ... } | |
let handleResponse = combine(combine(parseJSONFromNSData, validateJSONResponse), unmarshalJSON(Model.self)) |
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
// Function might throw errors like this: | |
func parseJSONFromNSData(data: NSData) throws -> JSON { | |
// Try to parse but fails ... | |
throw JSONParseError.InvalideJSON | |
} | |
func handleResponse<T : Unmarshallable>(data: NSDate) -> T { | |
let data = ("{'somejson':true'}" as NSString).dataUsingEncoding(NSUTF8StringEncoding)! | |
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
enum JSONParseError : ErrorType { | |
case InvalideJSON | |
case UnexpectedCharacter(line: Int, column: Int) | |
} | |
enum ServerResponseError : ErrorType { | |
case InvalidResponse | |
case ErrorResponse(message: 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
func parseJSONFromNSData(data: NSData) -> JSON { ... } | |
func validateJSONResponse(json: JSON) -> (JSON, NSError?) { ... } | |
func unmarshalJSON<T : JSONUnmarshallable>(json: JSON, type: T.Type) -> T { ... } |
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
// | |
// LimitOperator.swift | |
// | |
// Created by Alexander Ney on 22/06/2014. | |
// Copyright (c) 2014 Alexander Ney. All rights reserved. | |
// | |
// Lower limit operator | |
operator infix |- {associativity right} |
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
// | |
// OptionalOperators.swift | |
// | |
// Created by Alexander Ney on 22/06/2014. | |
// Copyright (c) 2014 Alexander Ney. All rights reserved. | |
// | |
// Null Coalescing Operator |
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
// | |
// String+RegularExpression.swift | |
// | |
// Created by Alexander Ney on 15/06/2014. | |
// Copyright (c) 2014 Alexander Ney. All rights reserved. | |
// | |
import Foundation | |