Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
willmurphyscode / hash_on_mutated_array.rb
Created March 26, 2017 22:08
Demonstration that mutating an array in Ruby changes it's hash value
def does_it_change
mutable_array = [1,2]
hash = {}
hash[mutable_array] = 'foo'
puts hash[mutable_array].inspect # prints "foo"
mutable_array[1] = 3
puts hash[mutable_array].inspect # prints "nil"
end
does_it_change
@willmurphyscode
willmurphyscode / HashOnMutatedArray.cs
Last active March 26, 2017 22:37
Simple demonstration that mutating an array in C# doesn't change its hash function, in contrast with Ruby.
using System;
using System.Collections.Generic;
namespace array_compare
{
class Program
{
static void DoesItChange()
{
Dictionary<int[], string> hash = new Dictionary<int[], string>();
@willmurphyscode
willmurphyscode / node.rs
Created June 24, 2017 13:30
Simple node struct for making a tree
use helpers;
use std::fmt;
#[derive(Debug)]
pub struct Node {
pub left_child: Option<Box<Node>>,
pub right_child: Option<Box<Node>>,
pub has_fruit: bool,
}
extern crate rand;
mod node;
mod helpers;
fn recursive_count_fruit(node: Option<Box<node::Node>>) -> i32 {
match node {
Some(val) => {
let our_fruit = if val.has_fruit { 1 } else { 0 };
let unboxed = *val;
@willmurphyscode
willmurphyscode / destructure-copyable-vs-uncopyable.rs
Created July 10, 2017 11:09
This is shows the difference in Move behavior between a boxed struct whose members can be copied and a box struct whose members can't be copied.
// THANKS TO https://rustbyexample.com/std/box.html
// for the code that was modified into this example
struct Point {
x: f64,
y: f64,
}
struct Rectangle {
p1: Point,
@willmurphyscode
willmurphyscode / CaseInsensitiveDictionary.cs
Created July 20, 2017 11:50
A Dictionary<String, T> that ignores the case of its keys
using System;
using System.Collections.Generic;
namespace collections
{
public class CaseInsensitiveDictionary<T>
{
private Dictionary<String, T> _dict;
public CaseInsensitiveDictionary()
{
@willmurphyscode
willmurphyscode / case_insensitive_hash.rb
Last active July 20, 2017 11:57
A Hash that will ignore the casing of string keys, and treat string and symbol keys synonymously.
class CaseInsensitiveHash
def initialize
@hsh = {}
end
def get(key)
raise ArgumentError unless key.respond_to?(:to_s)
@hsh[key.to_s.downcase.to_sym]
end
@willmurphyscode
willmurphyscode / Menu.cs
Created August 9, 2017 11:28
A simple C# class that implements IEnumerable<T>
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
namespace collections
{
public class Menu : IEnumerable<MenuItem>
{
private List<MenuItem> items;
@willmurphyscode
willmurphyscode / menu.rb
Created August 9, 2017 11:37
A simple class in Ruby that includes Enumerable
class Menu
include Enumerable
def initialize(items)
@items = items
end
def each
@items.map { |item| yield item }
end
@willmurphyscode
willmurphyscode / ruby-cosine-fixedpoint.rb
Created September 13, 2017 11:08
Calculate the fixed point of cosine in Ruby.
TOLERANCE = 0.00000000000000000001
def tolerance_equals?(a, b)
((a - b) * (a - b)) < TOLERANCE
end
def recursive_cosine_fixedpoint(a)
return a if tolerance_equals?(a, Math.cos(a))
recursive_cosine_fixedpoint(Math.cos(a))
end