Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rayfix
rayfix / ctor.cpp
Created April 8, 2015 23:29
How to construct C++11
//
// main.cpp
// forward
//
// Created by Ray Fix on 4/8/15.
// Copyright (c) 2015 Pelfunc, Inc. All rights reserved.
//
#include <iostream>
@rayfix
rayfix / subscriptions_spec.rb
Created April 22, 2015 01:37
Multiple expect invocations inside a single test example
require 'active_support/all'
class Subscription
def initialize(created_at:)
@created_at = created_at
end
attr_reader :created_at
def months_subscribed
((Date.today - created_at.to_date) / 30).round
struct TaggedThing<Tag, Thing> {
var value: Thing
}
protocol A {} // Phantom Type
protocol B {} // Phantom Type
var a = TaggedThing<A, Int>(value: 1)
var b = TaggedThing<B, Int>(value: 2)
@rayfix
rayfix / gist:ea2595e5b1e123e849ec
Created July 1, 2015 15:44
A UIView that conforms
import UIKit
// Rather than a generic type with constraints on being a UIView and conforming to Extra
// you could do something like this.
protocol UIViewConvertable {
var uiview: UIView { get }
}
extension UIView : UIViewConvertable {
var uiview: UIView { return self }
}
@rayfix
rayfix / ShrödingerEquationSolver.swift
Last active November 26, 2015 02:41
Schrödinger Equation Solver: Harmonic Oscillator
//: # Harmonic Oscillator Schrödinger Equation
//:
//: Numerically Solve for Shrödinger's Equation using the
//: Numerov algorithm. This example inspired by Quantum
//: Physics for Dummies, Steven Holzner, John Wiley & Sons, Inc. 2013.
//:
//: Had to increase the delta (step size) and reduce the maximum allowed
//: error to make it run in a reasonable amount of time in the playground.
//: (This obviously could be solved by switching to [more] compiled code.)
//:
@rayfix
rayfix / ShrödingerEquationSolver2.swift
Created November 29, 2015 04:14
ShrödingerEquationSolver2
//: # Harmonic Oscillator Schrödinger Equation
//: ## Version 2
//:
//: Numerically Solve for Shrödinger's Equation using the
//: [Numerov method](https://en.wikipedia.org/wiki/Numerov%27s_method).
//: This example inspired by Quantum
//: Physics for Dummies, Steven Holzner, Wiley 2013.
//:
//: This version is thousands of times faster and more
//: accurate than my more direct port of the original. It starts with
@rayfix
rayfix / loop.swift
Created December 4, 2015 21:45
a simple loop
var i = 0
while (i < 10) {
print(i)
i += 1
}
struct MultiArray<Element> {
private var data: [Element] // the actual data
private var counts: [Int] // the size of each dimension
private var offsets: [Int] // multipliers for accessing each dimension quickly
private func computeIndex(indices: [Int]) -> Int {
precondition(indices.count == counts.count)
for (index, count) in zip(indices, counts) {
precondition(index >= 0 && index < count)
//
// Blurring.swift
// TransitionDemo
//
// Created by Ray Fix on 1/7/16.
//
import UIKit
protocol Blurring {}
@rayfix
rayfix / frequencies.swift
Created January 10, 2016 02:23
Generic frequencies method for SequenceTypes
//: # Generic frequencies for SequenceType
//:
//: Based on a tweet from Airspeed Velocity @airspeedswift
//: describing a custom subscript operation that takes
//: an initial value if the key is not found.
import Foundation
extension Dictionary {
subscript(key: Key, or initial: Value) -> Value {