Skip to content

Instantly share code, notes, and snippets.

named!(single_token<&[u8], Token>,
alt!(
left_paren |
right_paren |
addition_sign |
subtraction_sign |
multiplication_sign |
division_sign |
operand
)
named!(left_paren<&[u8], Token>,
do_parse!(tag!("(") >> (Token::LeftParen))
);
named!(right_paren<&[u8], Token>,
do_parse!(tag!(")") >> (Token::RightParen))
);
named!(addition_sign<&[u8], Token>,
do_parse!(tag!("+") >> (Token::Operator(Opcode::Add)))
pub enum Token {
LeftParen,
RightParen,
Operator(Opcode),
Operand(isize)
}
pub enum Opcode {
Add, Subtract, Multiply, Divide,
}
@willmurphyscode
willmurphyscode / rust-cosine-fixedpoint.rs
Created September 13, 2017 11:10
Calculate the fixed point of cosine in Rust.
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 September 13, 2017 11:09
Calculate the fixed point of cosine in Python
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 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
@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 / 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 / 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