Skip to content

Instantly share code, notes, and snippets.

View AlexanderNey's full-sized avatar

Alexander Ney AlexanderNey

  • Atlassian
  • Sydney, Australia
View GitHub Profile
@AlexanderNey
AlexanderNey / article-function-composing-5.swift
Last active June 15, 2016 12:54
ARTICLE Function Composing Swift 2.0 - 5
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 = ...
@AlexanderNey
AlexanderNey / article-function-composing-4.swift
Last active August 29, 2015 14:23
ARTICLE Function Composing Swift 2.0 - 3
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)
@AlexanderNey
AlexanderNey / article-function-composing-4.swift
Last active August 29, 2015 14:23
ARTICLE Function Composing Swift 2.0 - 4
func unmarshalJSON<T : Unmarshallable>(type: T.Type)(json: JSON) throws -> T { ... }
let handleResponse = combine(combine(parseJSONFromNSData, validateJSONResponse), unmarshalJSON(Model.self))
@AlexanderNey
AlexanderNey / article-function-composing-3.swift
Last active June 15, 2016 12:07
ARTICLE Function Composing Swift 2.0 - 3
// 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)!
@AlexanderNey
AlexanderNey / article-function-composing-2.swift
Last active August 29, 2015 14:23
ARTICLE Function Composing Swift 2.0 - 2
enum JSONParseError : ErrorType {
case InvalideJSON
case UnexpectedCharacter(line: Int, column: Int)
}
enum ServerResponseError : ErrorType {
case InvalidResponse
case ErrorResponse(message: String)
}
@AlexanderNey
AlexanderNey / atricle-function-composing-1.swift
Last active June 15, 2016 12:06
ARTICLE Function Composing Swift 2.0 - 1
func parseJSONFromNSData(data: NSData) -> JSON { ... }
func validateJSONResponse(json: JSON) -> (JSON, NSError?) { ... }
func unmarshalJSON<T : JSONUnmarshallable>(json: JSON, type: T.Type) -> T { ... }
@AlexanderNey
AlexanderNey / LimitOperator.swift
Last active August 29, 2015 14:02
lower and upper limiter operator in swift
//
// 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}
@AlexanderNey
AlexanderNey / OptionalOperators.swift
Last active May 22, 2017 17:03
Null Coalescing Operator for Swift
//
// OptionalOperators.swift
//
// Created by Alexander Ney on 22/06/2014.
// Copyright (c) 2014 Alexander Ney. All rights reserved.
//
// Null Coalescing Operator
@AlexanderNey
AlexanderNey / String+RegularExpression.swift
Created June 15, 2014 17:06
Swift String extension to ease working with regular expressions
//
// String+RegularExpression.swift
//
// Created by Alexander Ney on 15/06/2014.
// Copyright (c) 2014 Alexander Ney. All rights reserved.
//
import Foundation