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
@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 / 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 }
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 / test-indexed-state-monad.hs
Last active May 23, 2022 16:45
Testing indexed state monad transformers
--------------------------------------------------------------------------------
-- Copyright : (C) 2008 Edward Kmett, 2016 Mazdak Farrokhzad
-- License : BSD-style
--------------------------------------------------------------------------------
{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances,
UndecidableInstances, MultiParamTypeClasses #-}
import Control.Applicative(Alternative)
import Control.Monad.Identity
import Control.Monad.State
@Centril
Centril / search.groovy
Last active December 10, 2015 06:07
Fuzzy predicate logic searching with relevance, HOF style.
interface SearchResult<T> {
T getResult();
double getRelevance();
// Just for being able to "clone" with other relevance...
SearchResult<? extends T> withRelevance( double relevance );
}
interface SearchCriteria<T> {
SearchResult<? extends T> apply( SearchResult<? extends T> result );
@Centril
Centril / generativeFlatMap.groovy
Created October 25, 2015 22:49
generative flat map, groovy
List.metaClass.foldn = { init, closure ->
def l = delegate.clone()
def acc = init
def it = l.listIterator()
while ( it.hasNext() ) {
def e = it.next()
it.remove()
closure( acc, e, it )
}
return acc
@Centril
Centril / philips-tv-unknown-sources
Created March 26, 2015 17:54
how to enable unknown sources in a Philips TV (android)
1. Download [terminal emulator] from playstore.
2. Open [terminal emulator] and execute the following: `am start --user 0 -n com.android.settings/.SecuritySettings`. This will launch the Security Settings which Philips has hidden.
3. Scroll down and: Enable unknown sources.
4. Profit.
<!-- references -->
[terminal emulator]: https://play.google.com/store/apps/details?id=jackpal.androidterm
@Centril
Centril / mousemovement.js
Created February 28, 2015 23:49
mousemovement.js
// Dumping old code as gists...
(function($) {
$.mouseMovement = {
stack: [],
clearInterval: 1000
};
var stack = $.mouseMovement.stack;
@Centril
Centril / README.md
Created February 28, 2015 23:46
Old jQuery hack

Applied to jQuery 1.7 dev. This was just an idea i made long ago, dumping for keepsake.

@Centril
Centril / same.rs
Created February 23, 2015 20:27
Trait & struct have the same name for a method
fn main() {
let b = B;
(&b as &A).a();
}
trait A {
fn a( &self );
}
struct B;