Skip to content

Instantly share code, notes, and snippets.

/*****************************************************************************
* Copyright (c) 2011 Kenneth Waters
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
@aras-p
aras-p / preprocessor_fun.h
Last active April 28, 2024 15:25
Things to commit just before leaving your job
// Just before switching jobs:
// Add one of these.
// Preferably into the same commit where you do a large merge.
//
// This started as a tweet with a joke of "C++ pro-tip: #define private public",
// and then it quickly escalated into more and more evil suggestions.
// I've tried to capture interesting suggestions here.
//
// Contributors: @r2d2rigo, @joeldevahl, @msinilo, @_Humus_,
// @YuriyODonnell, @rygorous, @cmuratori, @mike_acton, @grumpygiant,
@matthiasplappert
matthiasplappert / gist:9493050
Last active August 29, 2015 13:57
QuickLook Debugging for `UIView`
@interface UIView (MPAdditions)
@end
@implementation UIView (MPAdditions)
- (id)debugQuickLookObject {
if (self.bounds.size.width < 0.0f || self.bounds.size.height < 0.0f) {
return nil;
}
@CodaFi
CodaFi / Nat.swift
Last active September 24, 2021 19:46
// Playground - noun: a place where people can play
protocol ℕ {
static func construct() -> Self
static var value : UInt { get }
}
struct Zero : ℕ {
static func construct() -> Zero {
return Zero()
@CodaFi
CodaFi / Church.swift
Last active November 15, 2018 20:38
Church Encoding in Swift
/// Playground - noun: a place where people can play
/// Church-Turing clan ain’t nothing to func with.
/// Church encoding is a means of representing data and operators in the lambda
/// calculus. In Swift, this means restricting functions to their fully curried
/// forms; returning blocks wherever possible. Church Encoding of the natural
/// numbers is the most well-known form of encoding, but the lambda calculus is
/// expressive enough to represent booleans, conditionals, pairs, and lists as
/// well. This file is an exploration of all 4 representations mentioned.
@kristopherjohnson
kristopherjohnson / YieldGenerator.swift
Last active June 21, 2019 03:14
Attempt to implement a "yield" for Swift, similar to yield in Python, F#, etc.
import XCTest
import Foundation
// Generates values from a closure that invokes a "yield" function
struct YieldGenerator<T>: Generator {
var yieldedValues = Array<T>()
var index = 0
mutating func yield(value: T) {
yieldedValues.append(value)
@rnapier
rnapier / gist:8eda179689d9d61c2bfb
Last active August 29, 2015 14:04
And autoclosure saves(?) the day for generic recursive enums
// Creating a generic recursive data structure with autoclosure. (READ ALL NOTES; THIS MAY NOT DO WHAT YOU WANT.)
// Closures are reference types, so the size is known (? I think ?)
// Note that this is just because of unimplemented features in the compiler in Beta5
// There's no reason to think this is a long-term requirement.
// IMPORTANT: the closure will run every time you access this value, so if that has
// side effects, this won't work. It's only possible on pure value types.
// But the fact that this works as expected is actually kind of incredible.
// Think about what is required for it to work out the type for NestedList.Elem("first").
@CodaFi
CodaFi / Y.swift
Last active November 29, 2021 18:19
The Y Combinator in Swift
public func unsafeCoerce<A, B>(_ x : A) -> B {
return unsafeBitCast(x, to: B.self)
}
func Y<A>(_ f : @escaping (A) -> A) -> A {
return { (x : @escaping (A) -> A) in
return f((x(unsafeCoerce(x))))
}({ (x : A) -> A in
let fn : (A) -> A = unsafeCoerce(x)
return f(fn(x))
@CodaFi
CodaFi / Continuations.swift
Created October 5, 2014 02:54
A Swift Continuation Monad
// Playground - noun: a place where people can play
public func id<A>(x : A) -> A {
return x
}
public func error<A>(x : String) -> A {
assert(false, x)
}
@andymatuschak
andymatuschak / CollectionViewDataSource.swift
Last active February 12, 2021 09:44
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit