Skip to content

Instantly share code, notes, and snippets.

@archer884
archer884 / parallel_prime_spike.rs
Last active August 29, 2015 14:12
Parallel prime finding nonsense
use std::iter::iterate;
use std::num::Float;
use std::sync::Future;
fn main() {
if let Some(n) = read_uint(1) {
let sum_of_primes = {
let block_size = if n > 999 { n / 20 } else { n };
let mut futures = Vec::new();
@archer884
archer884 / expiring_cache.rs
Created December 30, 2014 18:56
Rust cache with expiring items
extern crate chrono;
pub mod expiring_cache {
use chrono::{DateTime, Duration, UTC};
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::hash::Hash;
pub struct ExpiringCache<Key, Value> where Key: Eq + Hash {
expiry: Duration, // cache endurance
@archer884
archer884 / trampoline.rs
Created December 16, 2014 01:10
Clojure-style trampoline using boxing
#![feature(unboxed_closures)]
use Bounce::{Process, Result};
fn main() {
let x: int = from_str(std::os::args()[1].as_slice()).unwrap();
let result = Process(box move || is_even(x)).execute();
println!("{}", if result { "even" } else { "odd" });
}
@archer884
archer884 / trampoline.rs
Last active August 29, 2015 14:11
Trampoline implementation in Rust
#![feature(unboxed_closures)]
fn main() {
let x = from_str::<int>(std::os::args()[1].as_slice()).unwrap();
let n = Bounce::Incomplete(x).execute(|n| {
match n {
n if n < 1 => Bounce::Fault("Value out of range."),
1i => Bounce::Complete(n),
n => Bounce::Incomplete(n - 1i),
}
@archer884
archer884 / sieve_avg_prime.rs
Last active August 29, 2015 14:10
Fetch primes using sieve, average values less than one million
fn main() {
let (cnt, sum) = sieve(1000000).iter()
.fold((0u, 0u), |a, b| {
let (cnt, sum) = a;
(cnt + 1, sum + *b)
});
println!("{}", sum as f32 / cnt as f32);
}
@archer884
archer884 / fib_seq.rs
Last active August 29, 2015 14:10
Rust fibonacci sequence
use std::iter::Unfold;
fn main() {
let fib_seq = Unfold::new((0i64, 1i64), |state| {
// closures pretty much always borrow, so *dereference
// to get value instead of pointer
let (x, y) = *state;
let result = Some(x);
// again, set *value, not reference
@archer884
archer884 / wcount.rs
Created December 1, 2014 22:10
Rust word usage counter
#![feature(phase)]
#[phase(plugin)]
extern crate regex_macros;
extern crate regex;
use regex::Regex;
use std::ascii::AsciiExt;
use std::collections::{HashMap};
use std::collections::hash_map::Entry::{Vacant, Occupied};
use std::io::{BufferedReader, File};
@archer884
archer884 / spellcheck.rs
Created November 29, 2014 23:52
Checks files for spelling errors.
#![feature(phase)]
#[phase(plugin)]
extern crate regex_macros;
extern crate regex;
use regex::Regex;
use std::ascii::AsciiExt;
use std::collections::HashSet;
use std::io::{BufferedReader, File};
use std::io::fs::PathExtensions;
@archer884
archer884 / dice.cs
Last active August 29, 2015 14:10
Dice rolling program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
namespace Dice
{
class Program
{
static void Main(string[] args)
@archer884
archer884 / DateCorrector.cs
Last active August 29, 2015 14:09
Comments for YuEnDee
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace EasyChallenge188_Dates
{
class Program
{