This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here, I’ve added a function to Array called `partition`. | |
// In short, the function will partition the Array into a Dictionary based on a function | |
// which takes an Element of the Array and returns its corresponding Key into the Dictionary. | |
extension Array { | |
func partition<Key>(key: (Element)->Key) -> [Key: [Element]] { | |
var groups = [Key: [Element]]() | |
for element in self { | |
let key = key(element) | |
var group = groups[key] ?? [Element]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The MIT License (MIT) | |
# | |
# Copyright (c) 2015 Michael Bates | |
# | |
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# O(nk) | |
class Array | |
def unzip ary | |
return [] if ary.empty? | |
count = ary.first.count | |
(0...count).map { |i| | |
ary.map { |tuple| | |
raise 'inconsistent tuple size' if tuple.count != count | |
tuple[i] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support' | |
module OptionSet | |
extend ActiveSupport::Concern | |
included do | |
attr_reader :value | |
include Comparable | |
include Enumerable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}\"") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 { |
OlderNewer