Skip to content

Instantly share code, notes, and snippets.

Avatar

Brendan Zabarauskas brendanzab

View GitHub Profile
View fmt_int.rs
#[feature(macro_rules)];
use std::fmt;
use std::fmt::parse::FlagAlternate;
use std::fmt::parse::FlagSignAwareZeroPad;
use std::fmt::parse::FlagSignPlus;
use std::fmt::parse::{AlignLeft, AlignRight, AlignUnknown};
use std::num::{cast, zero};
use std::uint;
View boolean.rs
/// Sets that form a boolean algebra.
///
/// These must contain exactly two elements, top and bottom: `{⊤, ⊥}`, and be
/// equipped with three basic operators: `¬`, `∧`, and `∨`, the definitions of
/// which are outlined below.
pub trait Boolean: Eq {
/// The bottom value, `⊥`.
fn bottom() -> Self;
/// The top value, `⊤`.
View checked_math.rs
struct Checked<T>(T);
impl<T: CheckedAdd> Add<Checked<T>, Option<Checked<T>>> for Checked<T> {
fn add(&self, other: &Checked<T>) -> Option<Checked<T>> {
let (&Checked(ref x), &Checked(ref y)) = (self, other);
x.checked_add(y).map(|z| Checked(z))
}
}
impl<T: CheckedSub> Sub<Checked<T>, Option<Checked<T>>> for Checked<T> {
View bitset-macro.rs
#[feature(macro_rules)];
#[allow(non_camel_case_types)];
macro_rules! bitset(
($BitSet:ident: $T:ty {
$($VALUE:ident = $value:expr),+
}) => (
#[deriving(Eq)]
pub struct $BitSet {
priv bits: $T,
View stack_edsl.rs
//! Leveraging tuples to make a statically typed, concatenative EDSL in Rust.
//!
//! I'm not sure how practical it really is – Rust's syntax can make it a little
//! hard to read, but it's fun to play around with it. The neat thing is how
//! little copying occurs. Everything is moved in and out by-value in a pipeline.
//!
//! Thanks goes to [Tekmo on reddit](http://www.reddit.com/r/programming/
//! comments/1zzom4/using_functionlength_to_implement_a_stack_language/cfyibsr)
//! for the idea.
//!
View dynamic_stack_lang.rs
extern crate collections;
use std::fmt;
use std::io;
use collections::HashMap;
fn main() {
let mut stdin = io::stdin();
let mut env = (~[], words::default());
loop {
View arith.hs
evalBigStep :: Tm -> Maybe Tm
evalBigStep (TmIf t1 t2 t3) =
evalBigStep t1 >>= \b1 -> case b1 of
TmTrue -> evalBigStep t2 -- (B-IfTrue)
TmFalse -> evalBigStep t3 -- (B-IfFalse)
_ -> Nothing
evalBigStep (TmSucc t1) =
evalBigStep t1 >>= \t1' -> case t1' of
nv1 | isNumerical nv1 -> Just (TmSucc nv1) -- (B-Succ)
_ -> Nothing
View lambda-calc.md

Introducing the lambda calculus

The following function:

fn(x) { t }

can be written using this weird notation devised by Church in the 40s:

λx. t
View borrow-example.rs
extern crate serialize;
use serialize::json;
fn get_string<'a>(data: &'a json::Json, key: &~str) -> Option<&'a str> {
match *data {
json::Object(ref map) => {
match map.find(key) {
Some(&json::String(ref s)) => {
Some(s.as_slice())
View lambda_calc.md

Introducing the lambda calculus

λx. t

This could be expressed in a 'hypothetical conventional language' as:

fn(x) { t }