Skip to content

Instantly share code, notes, and snippets.

extension Array {
func sortedWith<T: Comparable>(_ comparableTransform: (Array.Element) -> T) -> [Array.Element] {
return sorted(by: { (a, b) -> Bool in
return comparableTransform(a) > comparableTransform(b)
})
}
}
extension Array {
func sortedWith<T: Comparable, U: Comparable>(_ comparableTransform: (Array.Element) -> (T, U)) -> [Array.Element] {
return sorted(by: { (a, b) -> Bool in
let tupleA = comparableTransform(a)
let tupleB = comparableTransform(b)
return tupleA.0 == tupleB.0 && tupleA.1 == tupleB.1
})
}
}
struct ReductionsSequence<Base: Sequence, T>: Sequence, IteratorProtocol {
typealias Element = T
private var iterator: AnyIterator<Base.Element>
private var current: T
private let reducer: (T, Base.Element) -> T
init(sequence: Base, initialValue: T, reducer: @escaping (T, Base.Element) -> T) {
iterator = AnyIterator(sequence.makeIterator())
self.reducer = reducer
extension Equatable where Self: Sequence {
public static func == (lhs: Sequence<Element>, rhs: Sequence<Element>) -> Bool {
return elementsEqual(lhs, rhs)
}
}
func checkIteratorState<T: Sequence, T.Element: Equatable>(sequence: T) {
var iterator1 = sequence.makeIterator()
var iterator2 = sequence.makeIterator()
// test equality
}
public extension Sequence {
public func mySequence() -> AnySequence<Element> {
return AnySequence { () -> AnyIterator<Element> in
var iterator = self.makeIterator()
return AnyIterator {
return iterator.next()
}
}
}
}
postfix operator ~
public postfix func ~<T>(lhs: (T, T)) -> [T] {
return [lhs.0, lhs.1]
}
func go() {
(0, 0)~ // '~' is not a postfix unary operator
}
func f() {
let max = range.product(range).maxWith { (topRow, leftCol) -> (Int) in
return (0..<3).product(0..<3).map { rowOffset, colOffset in // (rowOffset: Int, colOffset: Int) -> (Int) in
return scores[topRow + rowOffset][leftCol + colOffset]
}.reduce(0, +)
}
}
Here's the license (it's the MIT license) that applies to all of my public gists:
Copyright Michael Eisel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERW
//Copyright (c) 2018 Michael Eisel. All rights reserved.
import Foundation
import QuartzCore
// import IkigaJSON
import ZippyJSON
import os
func dataFromFile(_ file: String) -> Data {
return try! String(contentsOfFile: Bundle.main.path(forResource: file, ofType: nil)!).data(using: .utf8)!