Skip to content

Instantly share code, notes, and snippets.

View mklbtz's full-sized avatar

Michael Bates mklbtz

View GitHub Profile
@mklbtz
mklbtz / any_enumerable.rb
Last active September 25, 2017 16:38
A quick way to make enumerable objects from an arbitrary block
class AnyEnumerable
include Enumerable
def initialize(&each_impl)
@each_impl = each_impl
end
def each(&block)
@each_impl.call(&block)
end
/// Useful for maintaining a strong reference to
/// something which would otherwise be deinitialized.
public final class Token<A> {
public let id: A
private let dispose: (A) -> Void
/// Create a new token
///
/// - Parameters:
/// - id: A unique identifier for this token.
@mklbtz
mklbtz / Collection+random.swift
Last active November 3, 2018 17:33
Implements "random" properties for collections and ranges
import Darwin
extension RandomAccessCollection {
// Fails if collection is larger than UInt32.max
var randomIndex: Index {
guard !isEmpty else { return endIndex }
let fullDistance = UInt32(distance(from: startIndex, to: endIndex))
let randomDistance = IndexDistance(arc4random_uniform(fullDistance))
return index(startIndex, offsetBy: randomDistance)
}
@mklbtz
mklbtz / Identifier.swift
Created September 2, 2017 21:52
A type-safe String identifier for objects
/// Useful as a type-safe String identifier for objects, records, etc.
/// Generic type A is the type of the object being "identified".
public struct Identifier<A>: Equatable, Hashable, Comparable {
public let string: String
public init(_ string: String) {
self.string = string
}
public var hashValue: Int {
@mklbtz
mklbtz / Algebra.swift
Last active September 3, 2017 19:38
A Number-like type that can represent algebraic expressions
// A Number-like type that can represent algebraic expressions.
public enum Algebra {
case scalar(Double)
case variable(name: String, () -> Double)
indirect case add(Algebra, Algebra)
indirect case subtract(Algebra, Algebra)
indirect case multiply(Algebra, by: Algebra)
indirect case divide(Algebra, by: Algebra)
indirect case magnitude(Algebra)
indirect case raise(Algebra, toPower: Int)
@mklbtz
mklbtz / algebra.rb
Last active September 2, 2017 20:57
Ruby Enums plus example
# A Numeric-like class that can represent algebraic expressions.
# Uses typecases to define what operations are representable.
class Algebra
include Enum
# Represents number literals
typecase :scalar, value: Numeric do
def to_s
value.to_s
end
@mklbtz
mklbtz / method_reflection.rb
Last active April 12, 2017 20:03
Helper methods for inspecting your methods
class Object
# List Methods defined directly in the receiver's class.
def my_methods
methods_from(self.class)
end
# List Methods with names matching the regex.
def methods_matching(regex)
methods_where { |meth| regex.match(meth.name.to_s) }
end
@mklbtz
mklbtz / OpenSource.rb
Last active April 16, 2017 22:46
A method for opening the `source_location` of a method or proc in your preferred editor
# A method for opening the `source_location`
# of a method or proc in your preferred editor.
# Depends on the presence of $EDITOR in your environment.
module OpenSource
def open_source
return false unless
loc = source_location and
loc.first != '(irb)'
path = loc.join(':') # append the line number to the file path.
system("eval \"$EDITOR #{path}\"")
@mklbtz
mklbtz / auto_rake.rb
Last active February 22, 2017 16:43
Extend a module with AutoRake for convenient rake task calling.
module AutoRake
require 'rake'
Rails.application.load_tasks if Rake::Task.tasks.empty?
def const_missing(const)
mod = Module.new
mod.extend AutoRake
const_set(const, mod)
end
@mklbtz
mklbtz / option_set.rb
Last active May 1, 2017 15:37
Porting Apple's OptionSets to Ruby
require 'active_support'
module OptionSet
extend ActiveSupport::Concern
included do
attr_reader :value
include Comparable
include Enumerable