Skip to content

Instantly share code, notes, and snippets.

@nathggns
nathggns / README.md
Last active January 26, 2017 12:11
Fibonacci Generator in LMC

LMC Fibonacci Generator

Screenshot

You can run this program on any LMC emulator, such as http://peterhigginson.co.uk/LMC/

LMC, which stands for Little Man Computer is a model of a computer, used to teach students how CPUs work. Read More.

Lines 1-2

@erica
erica / gist:999dabc1a2d176bce3ec
Last active February 11, 2017 01:40
Composed character count
extension String {
var composedCount : Int {
var count = 0
enumerateSubstringsInRange(startIndex..<endIndex, options: .ByComposedCharacterSequences) {_ in count++}
return count
}
}
@CodaFi
CodaFi / Sections.swift
Last active August 29, 2015 14:10
Operator sections for the Swift STL.
/// Sectional Sale
prefix operator >> {}
postfix operator >> {}
public prefix func >>(rhs: UInt16) -> UInt16 -> UInt16 {
return { lhs in lhs >> rhs }
}
public postfix func >>(lhs: UInt16) -> UInt16 -> UInt16 {
@bjhomer
bjhomer / currentTrack.swift
Last active March 20, 2024 02:21
Using ScriptingBridge from Swift.
#! /usr/bin/swift
import ScriptingBridge
@objc protocol iTunesTrack {
optional var name: String {get}
optional var album: String {get}
}
@objc protocol iTunesApplication {
@Antol
Antol / APAspectFitImageView.h
Created October 16, 2014 21:29
UIImageView with aspect fit working with autolayout
//
// APAspectFitImageView.h
// autolayout
//
// Created by Antol Peshkov on 16.10.14.
// Copyright (c) 2014 Antol Peshkov. All rights reserved.
//
#import <UIKit/UIKit.h>
@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
@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)
}
@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))
@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").
@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)