Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / ext.rs
Last active October 24, 2021 23:08
"Extension Methods" in Rust
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::collections::hash_state::HashState;
fn main() {
if let Some(content) = {
let args = std::os::args();
if args.len() == 2 {
Some(args[1].to_string())
} else {
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var propertyValue = GetValidDecimalInput();
var propertyTax = PropertyAssessment(propertyValue) * 0.0064m;
@archer884
archer884 / computus.cs
Created February 18, 2015 23:53
Super-Computus Easter calculator!
using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace Computus
{
class Program
{
static Regex NodePattern = new Regex(@"<pre>.*?</pre>", RegexOptions.IgnoreCase|RegexOptions.Singleline);
@archer884
archer884 / tip.rs
Last active August 29, 2015 14:16
Tip calculator
struct TipResult {
amt: f64,
tip: f64,
}
fn main() {
let args: Vec<_> = std::env::args().collect();
let tip = match &args[..] {
[_, ref amt] => get_tip(amt.parse().ok(), Some(0.15f64)),
@archer884
archer884 / struct_variants.rs
Created April 20, 2015 22:39
Struct enum variants
enum TestEnum {
DataA { f: String, l: String },
DataB { n: String },
}
impl std::fmt::Display for TestEnum {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self {
&TestEnum::DataA { ref f, ref l } => write!(fmt, "{}, {}", l, f),
&TestEnum::DataB { ref n } => fmt.write_str(&n),