Skip to content

Instantly share code, notes, and snippets.

func unzip2<
S: SequenceType, T, U where S.Generator.Element == (T, U)
>(x: S) -> (AnySequence<T>, AnySequence<U>)
{
return (AnySequence(x.lazy.map { $0.0 }), AnySequence(x.lazy.map { $0.1 }))
}
func unzip2<
C: CollectionType, T, U where C.Generator.Element == (T, U), C.Index: RandomAccessIndexType
>(x: C) -> (AnyRandomAccessCollection<T>, AnyRandomAccessCollection<U>)
@natecook1000
natecook1000 / 0000-expanded-minmax.md
Last active April 17, 2016 12:13
Swift Evolution Proposal: Expanded min/max algorithms

Expanded min/max algorithms

  • Proposal: TBD
  • Author(s): Nate Cook
  • Status: Draft
  • Review manager: TBD

Introduction

This proposal would expand on the min() and max() sequence methods to add methods that return the corresponding index for a collection, efficiently find the minimum and maximum elements or indices at the same time, and provide extension points for sorted collections to provide all these results more efficiently.

Build Swift first using utils/build-script -R, other settings will need different directories below. Add these lines to ~/.bashrc, call with run-test path/to/test:

LLVM_DIR="/path/to/Swift/llvm"
SWIFT_BUILD_DIR="/path/to/Swift/build/Ninja-ReleaseAssert/swift-macosx-x86_64"

run-validation-test() {
    $LLVM_DIR/utils/lit/lit.py -a --param swift_site_config=$SWIFT_BUILD_DIR/validation-test-macosx-x86_64/lit.site.cfg $1
}
@natecook1000
natecook1000 / Swift-3.0-Hierarchy.png
Last active March 19, 2020 20:21
Swift StdLib Type/Protocol Hierarchies
Swift-3.0-Hierarchy.png
@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 / 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 / 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 / 0000-dict.md
Last active March 31, 2017 16:13
Dictionary & Set Improvements

Dictionary & Set Enhancements

  • Proposal: SE-0000
  • Author: Nate Cook
  • Review Manager: TBD
  • Status: Awaiting review

Introduction

This proposal comprises a variety of commonly (and less commonly) suggested improvements to the standard library's Dictionary type, from merging initializers to dictionary-specific filter and mapValues methods. The proposed additions to Dictionary, and the corresponding changes to Set, are detailed in the sections below.

// Additions to make the unsafe pointer APIs more consistent
extension UnsafePointer {
func deallocate(capacity: Int) {
UnsafeMutablePointer(mutating: self).deallocate(capacity: capacity)
}
}
extension UnsafeBufferPointer {
func deallocate() {
/// A sequence of weakly held elements.
///
/// Iterating over this sequence yields the elements that still exist at the
/// moment of iteration. Subsequent iterations may not yield the same elements,
/// as some elements may have been removed from memory.
public struct WeakSequence<Element: AnyObject> : Sequence {
var _contents: [Box] = []
/// A box for weakly holding a class instance.
class Box {