Skip to content

Instantly share code, notes, and snippets.

extension Sequence {
func sorted<T: Comparable>(on propertyAccessor: (Iterator.Element) -> T) -> Array<Iterator.Element> {
return self
.lazy
.map({ (element: $0, sortingProperty: propertyAccessor($0)) })
.sorted(by: { $0.sortingProperty < $1.sortingProperty })
.map({ $0.element })
}
}
extension Sequence {
func eachPair() -> Zip2Sequence<Self, DropFirstSequence<Self>> {
return zip(self, self.dropFirst())
}
}
extension Collection {
func indexed() -> [(index: Index, element: Element)] {
return zip(indices, self).map({ (index: $0, element: $1) })
extension Character: Strideable {
public typealias Stride = Int
public func distance(to other: Character) -> Character.Stride {
guard let first = self.unicodeScalars.first else {
fatalError()
}
guard let other = other.unicodeScalars.first else {
fatalError()
}
extension Sequence {
func smallest(_ n: Int, usingTest areInIncreasingOrder: (Element, Element) -> Bool) -> [Element] {
var result = self.prefix(n).sorted(by: areInIncreasingOrder)
for e in self.dropFirst(n) {
if let insertionIndex = result.firstIndex(where: { areInIncreasingOrder(e, $0) }) {
result.insert(e, at: insertionIndex)
result.removeLast()
}
package amal.global.amal
import java.util.concurrent.atomic.AtomicReference
import kotlin.concurrent.thread
class Promise<T> private constructor(state: State<T>) {
data class Callback<T>(
val onFulfilled: (T) -> Unit,
val onRejected: (kotlin.Error) -> Unit
import Foundation
public enum Method: String {
case GET
case POST
case PUT
case PATCH
case DELETE
}
extension Sequence where Element: Equatable {
func allElementsEqual() -> Bool {
var iterator = makeIterator()
guard let first = iterator.next() else { return true }
while let next = iterator.next() {
if first != next { return false }
}
return true
}
}
import Foundation
struct LineIterator: IteratorProtocol {
let scanner: Scanner
init(string: String) {
scanner = Scanner(string: string)
scanner.charactersToBeSkipped = .whitespaces
}
@khanlou
khanlou / ArchiveCurrent.applescript
Last active February 2, 2018 13:05
Notes.applescripts
tell application "Notes"
move front note to folder "Archive"
end tell
import Foundation
import CoreGraphics
func / (lhs: Int, rhs: CGFloat) -> CGFloat {
return CGFloat(lhs)/rhs
}
func / (lhs: CGFloat, rhs: Int) -> CGFloat {
return lhs/CGFloat(rhs)
}