Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
nixpulvis / brainfuck-test.rs
Created May 29, 2017 05:42
Compare the speed of various BF implementations in Rust.
#![feature(test)]
extern crate test;
extern crate brainfuck;
extern crate bf;
use test::Bencher;
const HELLO_WORLD: &'static str = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
@nixpulvis
nixpulvis / seali.rb
Last active December 16, 2016 00:31
CLIs with subcommands that don't suck.
# NOTE: This is a WIP, please pardon our dust.
#
# Example:
#
# git --namespace=foobar clone https://github.com/nixpulvis/seali
# #<Git @args=[], @flags={namespace: "foobar"}, @sub=#<Clone @args=["https://github.com/nixpulvis/seali"], @flags=[], @sub=nil>>
class Seali
class << self
attr_accessor :subs
@nixpulvis
nixpulvis / first_or_create.rb
Last active July 26, 2016 16:42
Retrying a first or create when two happen at the same time.
# Simple helper function for retrying a first or create when two happen
# at the same time and one violates the database unique index constraint.
def first_or_create(scope, attributes = {})
begin
scope.first_or_create(attributes)
rescue ActiveRecord::RecordNotUnique
retry
end
end
@nixpulvis
nixpulvis / debug_expr.rs
Created April 20, 2016 18:36
debug! an expression and it's syntax.
#[macro_export]
macro_rules! debug_expr {
($expr:expr) => {{
let value = $expr;
debug!("`{}` = {:?}", stringify!($expr), value);
value
}};
}
use std::ops::Deref;
use serde::{Serialize, Serializer, Deserialize, Deserializer, Error};
use serde_json::value::Value;
use super::*;
/// An integer.
///
/// This should ideally work for all sized integers, but currently does not.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Integer(pub i64);
//! ```cargo
//! [dependencies]
//! serde = { git = "https://github.com/nixpulvis/serde.git" }
//! serde_json = { git = "https://github.com/nixpulvis/json.git" }
//! ```
extern crate serde;
extern crate serde_json as json;
use serde::de;
// NOTE:
//
// Matthias's code below and inline.
//
// (define (<-card c1 c2)
// (match-define (card f1 t1) c1)
// (match-define (card f2 t2) c2)
// (or (<-trait t1 t2) (and (equal? t1 t2) (< f1 f2))))
// (define (<-card c1 c2)
@nixpulvis
nixpulvis / dfa.rs
Last active September 14, 2016 23:04
impl Node {
fn new(state: State) -> Node {
Rc::new(RefCell::new(state))
}
fn then(self, state: State) {
let arrow = Arrow::Then(self.clone());
state.arrows.push(arrow);
Node::new(state)
}
#![feature(plugin_registrar, rustc_private)]
extern crate syntax;
extern crate rustc;
extern crate rustc_plugin;
extern crate itertools;
extern crate serde;
extern crate serde_json;
use syntax::codemap::Span;
#[derive(Debug)]
struct Recur(u32);
impl Recur {
pub fn call(n: u32) -> u32 {
let r = Recur(n);
let r = r.recur();
r.0
}