Skip to content

Instantly share code, notes, and snippets.

@jakebromberg
jakebromberg / AlmostIncreasingSequence.swift
Last active January 9, 2019 17:53 — forked from jakehawken/AlmostIncreasingSequence.swift
For the "almostIncreasingSequence" problem on CodeFights:
extension Sequence where Element: Comparable {
func isAlmostIncreasingSequence() -> Bool {
var foundMisplacedElement = false
for (a, b) in zip(self, self.dropFirst()) where a >= b {
guard !foundMisplacedElement else { return false }
foundMisplacedElement = true
}
@jakebromberg
jakebromberg / Array+EachCons.swift
Last active May 31, 2022 17:30 — forked from khanlou/Array+EachCons.swift
each_cons from Ruby in Swift as a function on Sequence
extension Collection {
func eachConsecutive(_ size: Int) -> Array<SubSequence> {
let droppedIndices = indices.dropFirst(size - 1)
return zip(indices, droppedIndices)
.map { return self[$0...$1] }
}
}
// One note before we start: if the inhabitants of Value are a closed set,
// then making Value an enum is going to be a clearer model than using a
// protocol like this. I'm assuming you need to be able to retroactively add
// new members of Value.
// Can't use Equatable directly because of its Self requirement.
protocol HeteroEquatable {
func isEqualTo(_ value: HeteroEquatable) -> Bool
}
@jakebromberg
jakebromberg / AVAssetTrim.swift
Last active October 4, 2023 23:57 — forked from acj/TrimVideo.swift
Trim video using AVFoundation in Swift
import AVFoundation
import Foundation
extension FileManager {
func removeFileIfNecessary(at url: URL) throws {
guard fileExists(atPath: url.path) else {
return
}
do {
import CoreLocation
extension CLLocation {
public static func +(lhs: CLLocation, rhs: CLLocation) -> CLLocation {
let summedLat = lhs.coordinate.latitude + rhs.coordinate.latitude
let summedLong = lhs.coordinate.longitude + rhs.coordinate.longitude
return CLLocation(latitude: summedLat, longitude: summedLong)
}
public static func /(lhs: CLLocation, rhs: Double) -> CLLocation {