Skip to content

Instantly share code, notes, and snippets.

View jsanders's full-sized avatar

James Sanders jsanders

View GitHub Profile
@jsanders
jsanders / game.scala
Created April 19, 2014 20:33
A simple game from "Introduction to Functional Game Programming using Scala" talk at lambdaconf2014
class IO[A] private (run0: => A) {
def run = run0
def flatMap[B](f: A => IO[B]): IO[B] = {
IO(f(run).run)
}
def map[B](f: A => B): IO[B] = flatMap(a => IO(f(a)))
}
@jsanders
jsanders / keybase.md
Created March 18, 2014 15:24
Keybase

Keybase proof

I hereby claim:

  • I am jsanders on github.
  • I am jsanders (https://keybase.io/jsanders) on keybase.
  • I have a public key whose fingerprint is A954 10AF 97B2 2125 1A92 9C8E CFAA B7C4 D2CF 612A

To claim this, I am signing this object:

@jsanders
jsanders / generate_big_primes.rs
Last active October 30, 2018 22:53
Generate big primes in Rust. This works pretty fast now thanks to https://github.com/jsanders/rust-bignum and https://github.com/jsanders/rust-gmp! I'm still implementing my own mod_exp, but the performance is quite tolerable nonetheless.
extern crate bignum;
extern crate time;
use std::rand::task_rng;
use std::iter::{count,range_step_inclusive};
use std::num::{Zero,One};
use bignum::{BigUint,RandBigInt,ToBigUint};
// Find all prime numbers less than n
fn small_primes(bound: uint) -> ~[uint] {
@jsanders
jsanders / mod_exp.rs
Last active January 4, 2016 21:19
Rust extra::bigint mod_exp
// Modular exponentiation by squaring
fn mod_exp(base: &BigUint, exponent: &BigUint, modulus: &BigUint) -> BigUint {
let (zero, one): (BigUint, BigUint) = (Zero::zero(), One::one());
let mut result = one.clone();
let mut baseAcc = base.clone();
let mut exponentAcc = exponent.clone();
while exponentAcc > zero {
// Accumulate current base if current exponent bit is 1
if (exponent & one) == one {
@jsanders
jsanders / prime_sieve.rs
Last active May 19, 2022 16:41
Rust Sieve of Eratosthenes
// Find all prime numbers less than n
fn small_primes(bound: uint) -> ~[uint] {
// num is considered prime as long as primes[num] is true
// Start with all evens besides 2 filtered out
let mut primes = std::vec::from_fn(bound+1, |num| num == 2 || num & 1 != 0);
// Start at 3 and step by 2 because we've already filtered multiples of 2
for num in count(3u, 2).filter(|&num| primes[num]).take_while(|&num| num * num <= bound) {
// Mark prime num's multiples non-prime
// We can start at num^2 because smaller non-primes have already been eliminated
@jsanders
jsanders / explain_analyze.rb
Created December 18, 2013 22:56
Run EXPLAIN ANALYZE on all select queries and log the results. Definitely don't use this if performance matters...
if Rails.env.development?
require 'active_record/connection_adapters/postgresql_adapter'
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def __explain_analyze(sql, command, *args)
meth = "#{command}_without_explain_analyze".to_sym
if /\A\s*SELECT/i.match(sql)
newsql = "EXPLAIN ANALYZE #{sql}"
plan = send(meth, newsql, *args).map { |row| row['QUERY PLAN'] }.join("\n")
Rails.logger.debug("\e[1m\e[31mQUERY PLAN FOR: #{sql.strip};\n#{plan}\e[0m")
@jsanders
jsanders / glut_hello_world.cpp
Last active December 25, 2015 03:09
GLUT hello world implementations in c++ (for reference) and rust (as an attempt). The rust version seems close to working, but it hangs after the window comes up...
// Compile with `g++ -framework GLUT glut_hello_world.cpp`
#include <GLUT/glut.h>
void display(void) { }
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
@jsanders
jsanders / flip.rs
Last active December 24, 2015 10:38
Seeing if I can make a rust implementation of `flip` that is reminiscent of Haskell's (http://hackage.haskell.org/package/base-4.6.0.1/docs/Prelude.html#v:flip)
fn flip<T>(f: ~fn(a: T, b: T)) -> ~fn(a: T, b: T) {
|a, b| { f(b, a) }
}
fn hello_world(hello: &str, world: &str) {
println!("{:s}, {:s}!", hello, world)
}
#[test]
fn test_flip() {
@jsanders
jsanders / redis.rs
Last active December 24, 2015 10:29
Start of redis client implementation in rust
use std::rt::io::{Reader, Writer};
use std::rt::io::net::ip::{Ipv4Addr,SocketAddr};
use std::rt::io::net::tcp::TcpStream;
use std::str;
trait ToUintSafe {
fn to_uint_safe(&self) -> Option<uint>;
}
impl ToUintSafe for int {
@jsanders
jsanders / invmod.rb
Created September 27, 2013 20:57
Modular inverse function, with obligatory EGCD implementation.
# Extended Euclidean GCD algorithm
# Outputs k, u, and v such that ua + vb = k where k is the gcd of a and b
def egcd(a, b)
u_a, v_a, u_b, v_b = [ 1, 0, 0, 1 ]
while a != 0
q = b / a
a, b = [ b - q*a, a ]
u_a, v_a, u_b, v_b = [ u_b - q*u_a, v_b - q*v_a, u_a, v_a ]
# Each time, `u_a*a' + v_a*b' = a` and `u_b*a' + v_b*b' = b`
end