Skip to content

Instantly share code, notes, and snippets.

View Centril's full-sized avatar
🏠
Working from home

Mazdak Farrokhzad Centril

🏠
Working from home
  • Sweden
View GitHub Profile
import Data.Foldable (foldl')
import Data.List (genericReplicate)
distribute2 :: Integral a => a -> a -> [a]
distribute2 sum nBuckets =
let distHelp nBuckets (remains, tables) i =
let tablesLeft = nBuckets - i
currBucket = (remains + tablesLeft - 1) `div` tablesLeft
in (remains - currBucket, tables ++ [currBucket])
in snd $ foldl' (distHelp nBuckets) (sum, []) [0..nBuckets - 1]
@Centril
Centril / optimizing_fluent.rs
Created July 14, 2016 15:59
Rust: Testing if fluent interfaces are optimized away on releases
#![feature(asm)]
#[inline(never)]
fn noop() {
unsafe {
asm!("NOP");
}
}
struct S { x: usize }
@Centril
Centril / OptArcMutex.rs
Created November 6, 2016 02:11
OptArcMutex, mem::replace_with, mem_modify
use std::sync::{Arc, Mutex, MutexGuard, LockResult, PoisonError};
pub enum OptArcMutex<T> {
Arc(Arc<Mutex<T>>),
Imm(T)
}
pub enum MGuard<'a, T: 'a> {
Real(MutexGuard<'a, T>),
Fake(&'a mut T)
@Centril
Centril / nom.IResult.rs
Created December 22, 2016 21:33
nom::IResult::map_rem
// For debugging purposes:
impl<I, O, E=u32> IResult<I, O, E> {
fn map_rem<R, F: FnOnce(I) -> R>(self, f: F) -> IResult<R, O, E> {
use IResult::*;
match s {
Done(i, o) => Done(f(i), o),
Error(e) => Error(e),
Incomplete(n) => Incomplete(n),
}
@Centril
Centril / str_to_enum.rs
Created December 30, 2016 11:48
str_to_enum.rs
#![feature(try_from)]
use std::str::FromStr;
use std::convert::TryFrom;
macro_rules! str_to_enum {
($typ: path, $($var: ident),+) => {
impl std::str::FromStr for $typ {
type Err = ();
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
@Centril
Centril / stuff.rs
Created January 4, 2017 09:56
fn to_rc<T: ?Sized>(b: Box<T>) -> Rc<T>
#![feature(alloc, heap_api)]
extern crate alloc;
use std::rc::Rc;
use std::mem::{align_of, align_of_val, size_of, size_of_val};
fn to_rc<T: ?Sized>(b: Box<T>) -> Rc<T> {
// super duper undefined behavior!
struct InnerRcBox<T: ?Sized> {
strong: usize,
@Centril
Centril / keybase.md
Created January 9, 2017 08:32
keybase.md

Keybase proof

I hereby claim:

  • I am Centril on github.
  • I am centril (https://keybase.io/centril) on keybase.
  • I have a public key whose fingerprint is 17DC 71EF 7AD7 4032 4B5D 112E 818B 6F39 DE23 A081

To claim this, I am signing this object:

@Centril
Centril / combinators.hs
Created January 13, 2017 21:35
useful higher order combinators in haskell
(.$) :: (t -> b -> c) -> (a -> b) -> t -> a -> c
(.$) f g a = f a . g
infixr 8 .$
-- | '.|': Compose an unary function with a binary function.
-- from: http://hackage.haskell.org/package/pointless-fun-1.1.0.5/docs/Data-Function-Pointless.html
(.|) :: (b -> c) -> (t -> a -> b) -> t -> a -> c
(.|) f g a = f . g a
infixr 7 .|
@Centril
Centril / data.rs
Created September 17, 2017 21:04
Rust: A write up on various pointers for str.
fn main() {
use std::mem::{size_of, size_of_val};
let susize = size_of::<usize>();
// usize is size_t.
// "hello" is in static memory, x is on the stack and like: (char*, usize).
// &'static str is a fat pointer.
// It is not null terminated.
@Centril
Centril / const_trait_system.rs
Created December 4, 2017 18:55
Const Trait System
//******************************************************************************
// "const_trait_system"
//******************************************************************************
/*
Dependency DAG of proposal:
[A, B] -> []
[C] -> [B] and/or [A] -- NOTE: C is moot without B or A.
[D, E] -> [B]