Skip to content

Instantly share code, notes, and snippets.

Michael Eisel michaeleisel

Block or report user

Report or block michaeleisel

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View a.swift
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, +)
}
}
View a.swift
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
}
View a.swift
public extension Sequence {
public func mySequence() -> AnySequence<Element> {
return AnySequence { () -> AnyIterator<Element> in
var iterator = self.makeIterator()
return AnyIterator {
return iterator.next()
}
}
}
}
View a.swift
func checkIteratorState<T: Sequence, T.Element: Equatable>(sequence: T) {
var iterator1 = sequence.makeIterator()
var iterator2 = sequence.makeIterator()
// test equality
}
View gist:2124d7050e87f0d00a672fc6856935eb
extension Equatable where Self: Sequence {
public static func == (lhs: Sequence<Element>, rhs: Sequence<Element>) -> Bool {
return elementsEqual(lhs, rhs)
}
}
View a.swift
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
View a.swift
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
})
}
}
View a.swift
extension Array {
func sortedWith<T: Comparable>(_ comparableTransform: (Array.Element) -> T) -> [Array.Element] {
return sorted(by: { (a, b) -> Bool in
return comparableTransform(a) > comparableTransform(b)
})
}
}
View gist:0f19b0a2d1fbe2a71b039b9e508fa163
extension Array where Element: Hashable {
func uniqueElements() -> [Element] {
return uniqueElements(by: { $0 })
}
func uniqueElements<T: Hashable>(by uniqueValue: (Element) -> T) -> [Element] {
var uniqueElements: [Array.Element] = []
var set: Set<T> = Set()
for element in self {
let value = uniqueValue(element)
View a.swift
extension Array {
func sortedWith<T: Comparable>(_ comparableTransform: (Array.Element) -> T) -> [Array.Element] {
return sorted(by: { (a, b) -> Bool in
return comparableTransform(a) > comparableTransform(b)
})
}
}
let carts: [Card] = []
You can’t perform that action at this time.