Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
JadenGeller / Type-Level Assertions.swift
Last active March 9, 2018 03:06
Type-Level Assertions (or, almost-dependent types)
/*
* We've defined "traits" by which we can type an integer that are characteristic of its value.
* These traits can even be subtraits of other traits (like both positive and negative being nonzero).
*
* We can use these traits in the type signatures of functions to indicate what trait will be returned
* as a function of the passed-in traits.
*
* Even cooler, we can specify properties of the traits such that we can runtime-verify the correctness
* of these labels (in case a function was improperly annotated, for example).
*/
@CodaFi
CodaFi / Nat.swift
Last active January 6, 2024 07:23
An encoding of recursion schemes (à "Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire" [Meijer, Fokkinga, Paterson] ~( http://www.eliza.ch/doc/meijer91functional.pdf )) in Swift.
final class Nat<F> : FunctorOf<F> {
let pred : Optional<F>
override init() {
self.pred = nil
}
init(pred : F) {
self.pred = pred
}
@natecook1000
natecook1000 / NSScanner+Swift.swift
Created March 3, 2015 20:13
Swift-friendly NSScanner methods
// NSScanner+Swift.swift
// A set of Swift-idiomatic methods for NSScanner
//
// (c) 2015 Nate Cook, licensed under the MIT license
import Foundation
extension NSScanner {
// MARK: Strings
@sjoerdvisscher
sjoerdvisscher / mutatinglenses.swift
Last active February 24, 2016 23:19
Mutating lenses in Swift using operators with inout arguments
struct Lens<T, A> {
let get: T -> A
let mod: (T, A -> A) -> T
func set(t: T, _ a: A) -> T {
return self.mod(t) { _ in a }
}
}
infix operator >>> {}
func >>><S, T, A>(l: Lens<S, T>, r: Lens<T, A>) -> Lens<S, A> {
@smileyborg
smileyborg / detect_evil_merge.sh
Last active August 16, 2023 04:00
Two scripts that can be used to detect evil merges in Git. See http://stackoverflow.com/questions/27683077
#!/bin/bash
# A shell script to provide a meaningful diff output for a merge commit that can be used to determine whether the merge was evil.
# The script should be run from outside the git repository, with two arguments:
# 1 - the directory of the git repository
# 2 - the SHA for the merge commit to inspect
# The script will output one file:
# - the merge redone fresh without any conflicts resolved, diff'ed to the actual merge
output_file="diff.txt"
@seanwoodward
seanwoodward / NSFetchRequestTypedResults.swift
Last active August 29, 2015 14:11
Typed results when executing NSFetchRequest
func executeFetchRequestT<T:AnyObject>(request:NSFetchRequest, managedObjectContext:NSManagedObjectContext, error: NSErrorPointer = nil) -> [T]? {
var localError: NSError? = nil
if let results:[AnyObject] = managedObjectContext.executeFetchRequest(request, error: &localError) {
if results.count > 0 {
if results[0] is T {
let asT:[T] = results as [T]
return .Some(asT)
}
@natecook1000
natecook1000 / genericVsArray.swift
Created September 17, 2014 19:23
Comparing speed of generic function vs. Slice -> Array conversion
// comparing speed of generic function vs. Slice -> Array conversion
import Foundation
// sum function that takes a Slice<Int>
func sumSlice(numbers: Slice<Int>) -> Int {
return numbers.reduce(0, +)
}
// sum function that takes an Array<Int>
@natecook1000
natecook1000 / Explore.swift
Last active December 8, 2015 15:25 — forked from erica/gist:06d7e44f4f834757dc36
Swift: Reflection dump
// Original by Erica Sadun
// Source: http://ericasadun.com/2014/06/24/swift-reflection-dump/
import UIKit
import Foundation
func typestring(x : Any) -> String
{
if let obj = x as? NSObject {
return NSStringFromClass((x as NSObject).dynamicType)
@mattt
mattt / regex.swift
Created August 11, 2014 18:08
Creating a regular expression object from a String literal
class Regex {
let pattern: String
let options: NSRegularExpressionOptions!
private var matcher: NSRegularExpression {
return NSRegularExpression(pattern: self.pattern, options: nil, error: nil)
}
required init(pattern: String, options: NSRegularExpressionOptions = nil) {
self.pattern = pattern
import Foundation
extension String
{
var length: Int {
get {
return countElements(self)
}
}