Skip to content

Instantly share code, notes, and snippets.

let xs = [0,1,2,3,4,5]
let double: (Int) -> Int = { $0 * 2 }
let isEven: (Int) -> Bool = { $0 % 2 == 0 }
// Goal: [0,2,4,6,4,10]. Double all but the last even element.
extension Collection {
func mapExceptLast(matching predicate: (Element) -> Bool, transform: (Element) -> Element) -> [Element] {
var result: [Element] = []
@natecook1000
natecook1000 / joke.swift
Last active October 22, 2017 16:00 — forked from airspeedswift/joke.swift
Multiline Literals
func tellJoke(name: String, character: Character) {
let punchline = String(name.filter { $0 != character })
let n = name.count - punchline.count
let joke = """
Q: Why does \(name) have \(n) \(character)'s in their name?
A: I don't know, why does \(name) have \(n) \(character)'s in their name?
Q: Because otherwise they'd be called \(punchline).
"""
print(joke)
@natecook1000
natecook1000 / SwiftCoRoutine.swift
Created March 15, 2017 16:04 — forked from mzaks/SwiftCoRoutine.swift
A simple coroutine for swift
import Foundation
public class CoRoutine<T> {
private var index = 0
private var sequence : [T]
private var routine : (T)->()
private let step : Int?
private let deltaTime : TimeInterval?
private(set) public var isCanceled : Bool = false
private(set) public var isDone : Bool = false
@natecook1000
natecook1000 / LazyFilteredCollectionToArrayBenchmark.swift
Last active March 14, 2017 17:48 — forked from dabrahams/LazyFilteredCollectionToArrayBenchmark.swift
Measure the cost of pre-counting lazy filtered collections when converting to Array, WIP
import Darwin
import CoreFoundation
// A variable we can use with exit() to ensure that the optimizer
// doesn't remove code we want to time
var undead = 0
// A filtered collection that can be more efficiently counted than the stock one.
struct LazyFilterBidirectionalCollection2<Base : BidirectionalCollection> : Collection {
typealias Impl = LazyFilterBidirectionalCollection<Base>
@natecook1000
natecook1000 / update-swift-dev
Created March 8, 2017 15:35 — forked from ddunbar/update-swift-dev
This is the script I currently use on OS X to get a working "swift-dev.xctoolchain" out of a built Swift. It isn't designed to work for anyone but me, but it should be easy to adapt. I always run this after every build, and then use `TOOLCHAINS=swift-dev swift build` (etc) to use the development compiler.
#!/bin/sh
set -e
if [ -z "${CONFIGURATION}" ]; then
CONFIGURATION=debug
fi
# Create the development toolchain.
rm -rf ~/public/swift-project/build/Ninja-ReleaseAssert/swift-dev.xctoolchain
@natecook1000
natecook1000 / generics_playground.swift
Last active June 2, 2016 23:27 — forked from jessesquires/generics_playground.swift
Swift optional generic parameters?
protocol FactoryAType {
typealias Product
}
protocol FactoryBType {
typealias Product
}
struct CombinedFactory<T: FactoryAType, U: FactoryBType where T.Product == U.Product> {
let factoryA: T
@natecook1000
natecook1000 / modifiedCopy.swift
Last active October 29, 2015 19:24 — forked from anonymous/modifiedCopy.swift
turn foo.xInPlace(arg) into a foo.x(arg)...
func modifiedCopy<Struct, Arg>(var start: Struct, @noescape mutator: (inout Struct) -> Arg -> (), arg: Arg) -> Struct {
mutator(&start)(arg)
return start
}
extension Array {
func arrayByAppending(e: Element) -> Array {
return modifiedCopy(self, mutator: Array.append, arg: e)
}
}
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi enim lacus, ullamcorper in gravida a, semper id dolor. Mauris quis metus id"
extension String {
func split(separator: Character, maxSplit: Int = .max, allowEmptySlices: Bool = false) -> [String] {
return characters.split(separator, maxSplit: maxSplit, allowEmptySlices: allowEmptySlices).map(String.init)
}
}
let words = try string.split(" ")
let counts = words.map { $0.characters.count }
protocol OptionalType {
typealias T
func flatMap<U>(@noescape f: (T) -> U?) -> U?
}
extension Optional : OptionalType { }
extension SequenceType where Generator.Element: OptionalType {
func flatten() -> [Generator.Element.T] {
return self.map { $0.flatMap { $0 } }
// see also https://gist.github.com/griotspeak/8bb4c46611fc90d3043b
func findFirst<S: SequenceType>(seq: S, predicate: S.Generator.Element -> Bool) -> S.Generator.Element? {
for x in seq {
if predicate(x) { return x }
}
return nil
}
func not<T>(predicate: T -> Bool) -> (T -> Bool) {