Last active
August 29, 2015 14:22
-
-
Save griotspeak/7a9444caf1b225070b88 to your computer and use it in GitHub Desktop.
Swift.Array extensions
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
// | |
// Array.swift | |
// TonalKit | |
// | |
// Created by TJ Usiyan on 11/8/14. | |
// Copyright (c) 2014 Buttons and Lights LLC. All rights reserved. | |
// https://gist.github.com/7a9444caf1b225070b88.git | |
public func reduceR<S : CollectionType, U, IndexType : BidirectionalIndexType where S.Index == IndexType>(sequence: S, initial: U, combine: (U, S.Generator.Element) -> U) -> U{ | |
return Array(sequence.reverse()).reduce(initial, combine: combine) | |
} | |
internal func id<T>(x:T) -> T { | |
return x | |
} | |
extension ParsingStringType { | |
internal var headAndTail:(ListType.Generator.Element, ListType)? { | |
return symbols.first.bind { head in | |
let headIndex = self.symbols.startIndex | |
let tailIndex = headIndex.successor() | |
return (head, self.symbols[tailIndex..<self.symbols.endIndex]) | |
} | |
} | |
} | |
extension Array { | |
internal var headAndTail:(T, [T])? { | |
if isEmpty { | |
return nil | |
} else { | |
let headIndex = startIndex | |
let tailIndex = headIndex.successor() | |
return (self[headIndex], Array(self[tailIndex..<endIndex])) | |
} | |
} | |
public func reduce1(combine: (Generator.Element, Generator.Element) -> T) -> T { | |
if let found = headAndTail { | |
return (found.1).reduce(found.0, combine: combine) | |
} else { | |
preconditionFailure("reduce1 requires non empty list") | |
} | |
} | |
} | |
public func reduce1<T: ParsingStringType>(sequence: T, combine: (T.ListType.Generator.Element, T.ListType.Generator.Element) -> T.ListType.Generator.Element) -> T.ListType.Generator.Element { | |
if let found = sequence.headAndTail { | |
return (found.1).reduce(found.0, combine: combine) | |
} else { | |
preconditionFailure("reduce1 requires non empty list") | |
} | |
} | |
infix operator <|> { | |
associativity right | |
precedence 90 | |
} | |
infix operator <|?|> { | |
associativity right | |
precedence 90 | |
} | |
infix operator ++ { | |
associativity right | |
precedence 90 | |
} | |
internal func ++(lhs:String, rhs:String) -> String { | |
return "".join([lhs, rhs]) | |
} | |
internal func ++<T:ExtensibleCollectionType>(lhs:T.Generator.Element, rhs:T) -> T { | |
var list = T() | |
list.append(lhs) | |
return list + rhs | |
} | |
public func ==<T:Equatable, PST:Equatable>(lhs:[(value:T, state:PST)], rhs:[(value:T, state:PST)]) -> Bool { | |
if lhs.count != rhs.count { | |
return false | |
} | |
return Zip2(lhs, rhs).reduce(true) { | |
(isEqual, elements) -> Bool in | |
return isEqual && (elements.0.value == elements.1.value) && (elements.0.state == elements.1.state) | |
} | |
} | |
public func ==<T:Equatable, PST:Equatable>(lhs:[(value:[T], state:PST)], rhs:[(value:[T], state:PST)]) -> Bool { | |
if lhs.count != rhs.count { | |
return false | |
} | |
return Zip2(lhs, rhs).reduce(true) { | |
(isEqual, elements) -> Bool in | |
return isEqual && (elements.0.value == elements.1.value) && (elements.0.state == elements.1.state) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment