Skip to content

Instantly share code, notes, and snippets.

View token_gist.rs
pub enum Token {
LeftParen,
RightParen,
Operator(Opcode),
Operand(isize)
}
pub enum Opcode {
Add, Subtract, Multiply, Divide,
}
@willmurphyscode
willmurphyscode / rust-cosine-fixedpoint.rs
Created Sep 13, 2017
Calculate the fixed point of cosine in Rust.
View rust-cosine-fixedpoint.rs
const TOLERANCE: f64 = 0.00000000000000000001;
fn tolerance_equals(a: f64, b: f64) -> bool {
((a - b) * (a - b)) < TOLERANCE
}
fn recursive_cosine_fixedpoint(a: f64) -> f64 {
if tolerance_equals(a, a.cos()) {
a
} else {
@willmurphyscode
willmurphyscode / python-cosine-fixedpoint.py
Created Sep 13, 2017
Calculate the fixed point of cosine in Python
View python-cosine-fixedpoint.py
import math
tolerance = 0.00000000000000000001
def tolerance_equals(a, b):
return ((a - b) * (a - b)) < tolerance
def recursive_cosine_fixedpoint(a):
if tolerance_equals(a, math.cos(a)):
return a
@willmurphyscode
willmurphyscode / ruby-cosine-fixedpoint.rb
Created Sep 13, 2017
Calculate the fixed point of cosine in Ruby.
View ruby-cosine-fixedpoint.rb
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
@willmurphyscode
willmurphyscode / menu.rb
Created Aug 9, 2017
A simple class in Ruby that includes Enumerable
View menu.rb
class Menu
include Enumerable
def initialize(items)
@items = items
end
def each
@items.map { |item| yield item }
end
@willmurphyscode
willmurphyscode / Menu.cs
Created Aug 9, 2017
A simple C# class that implements IEnumerable<T>
View Menu.cs
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 / CaseInsensitiveDictionary.cs
Created Jul 20, 2017
A Dictionary<String, T> that ignores the case of its keys
View CaseInsensitiveDictionary.cs
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 Jul 20, 2017
A Hash that will ignore the casing of string keys, and treat string and symbol keys synonymously.
View case_insensitive_hash.rb
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 / destructure-copyable-vs-uncopyable.rs
Created Jul 10, 2017
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.
View destructure-copyable-vs-uncopyable.rs
// 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,
View simple-tree-main.rs
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;