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:
@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)
@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.
@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>
import UIKit
struct Monoid<A> {
let zero: A
let append: (A, A) -> A
}
let additive = Monoid(zero: 0, append: +)
let multiplicative = Monoid(zero: 1, append: *)
/// An operator is given by a list of arities, where each element indicates the number of
/// variables bound by the operator at that position and the length of the list determines the
/// number of variables the operator accepts. The full generalization of arity is called
/// the operator's "valence".
///
/// For example, if I have a little calculator language with let-bindings, its operators
/// would look like this:
///
///
/// enum CalcOps : Operator {
@mayoff
mayoff / main.m
Created May 31, 2017 15:43
adding objc_boxable to CoreGraphics structs
@import Foundation;
@import CoreGraphics;
typedef struct __attribute__((objc_boxable)) CGPoint CGPoint;
typedef struct __attribute__((objc_boxable)) CGSize CGSize;
typedef struct __attribute__((objc_boxable)) CGRect CGRect;
typedef struct __attribute__((objc_boxable)) CGVector CGVector;
int main(int argc, const char * argv[]) {
@autoreleasepool {
@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
}
}
@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

@brentsimmons
brentsimmons / gist:2080595ac8a6f41711af
Last active August 29, 2015 14:25
Swift init from unknown class
import Cocoa
protocol Thing {
var x: String {get}
init(s: String)
}
class Foo: Thing {
let x: String