Skip to content

Instantly share code, notes, and snippets.

@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>
@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)
/*****************************************************************************
* 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:
@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 / 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 / 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)
}
@airspeedswift
airspeedswift / MyArray.swift
Last active October 8, 2022 19:52
Array using ManagedBuffer
private class MyArrayBuffer<Element>: ManagedBuffer<Int,Element> {
func clone() -> MyArrayBuffer<Element> {
return self.withUnsafeMutablePointerToElements { elements -> MyArrayBuffer<Element> in
return MyArrayBuffer<Element>.create(self.allocatedElementCount) { newBuf in
newBuf.withUnsafeMutablePointerToElements { newElems->Void in
newElems.initializeFrom(elements, count: self.value)
}
return self.value
@avafloww
avafloww / PhpJava.java
Last active October 16, 2022 18:50
This snippet of code is syntactically valid in both PHP and Java, and produces the same output in both.
/*<?php
//*/public class PhpJava { public static void main(String[] args) { System.out.printf("/*%s",
//\u000A\u002F\u002A
class PhpJava {
static function main() {
echo(//\u000A\u002A\u002F
"Hello World!");
}}
//\u000A\u002F\u002A
PhpJava::main();