Skip to content

Instantly share code, notes, and snippets.

@pierric
pierric / iterator_split.rs
Created February 2, 2016 06:02
Split over an iterator in Rust
struct Split<I, P> {
iter: I,
pred: P,
}
impl<I, P> Iterator for Split<I,P> where I: Iterator, P: Fn(&I::Item) -> bool {
type Item = Vec<I::Item>;
fn next(&mut self) -> Option<Vec<I::Item>> {
let ref p = self.pred;
let not_p = |x: &I::Item| !p(x);
@pierric
pierric / viterbi.hs
Last active February 14, 2016 07:19
The viterbi algorithm for HMM in Haskell language
data Tag = TagStart | TagStop | Tag Int deriving (Eq, Show)
type Word = String
class CorpusModel m where
tags :: m -> [Tag]
tag_name :: m -> Tag -> String
tag_count_u :: m -> Tag -> Int
tag_count_b :: m -> (Tag, Tag) -> Int
tag_count_t :: m -> (Tag, Tag, Tag) -> Int
word_tag_count :: m -> Word -> Tag -> Int
@pierric
pierric / viterbi.rs
Last active February 14, 2016 07:19
The viterbi algorithm for HMM in Rust language
pub trait CorpusModel {
fn get_tags(&self) -> Range<u32>;
fn get_word_tag_count(&self, word: &String, tag: u32) -> u32;
fn get_tag_count_u(&self, tag: u32) -> u32;
fn get_tag_count_b(&self, tag1: u32, tag2: u32) -> u32;
fn get_tag_count_t(&self, tag1: u32, tag2: u32, tag3: u32) -> u32;
fn get_tag_name(&self, tag: u32) -> &String;
}
pub fn viterbi<M:CorpusModel>(model: &M, sentence: &Vec<String>) -> Vec<u32> {
@pierric
pierric / cyk.hs
Created March 1, 2016 13:15
CYK algorithm for PCFG
type Word = String
type NonTerminal = B.ByteString
data UnaryRule = NonTerminal :- Word deriving (Eq, Show)
data BinaryRule = NonTerminal := (NonTerminal, NonTerminal) deriving (Eq, Show)
instance Hashable UnaryRule where
hashWithSalt s (n :- w) = s `hashWithSalt` n `hashWithSalt` w
instance Hashable BinaryRule where
hashWithSalt s (n := (j,k)) = s `hashWithSalt` n `hashWithSalt` j `hashWithSalt` k
@pierric
pierric / ghc-pipeline.hs
Created June 18, 2016 20:38
Use GHC as a library, compiling to CMM
--
-- Call the PROG as:
-- <PROG> -package-db=<PATH-TO-PKGCONF>
--
-- <PATH-TO-PKGCONF> can be found by the following command:
-- ghc --print-global-package-db
--
module Main where
import GHC
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
{--
-- collect the results of expressions as a tuple,
-- e1 >>= \a ->
-- e2 >>= \b ->
-- return (a,b)
@pierric
pierric / llvm-update-alternatives
Last active December 6, 2016 11:23 — forked from jc00ke/llvm-update-alternatives
LLVM & clang alternatives
#!/usr/bin/env sh
sudo update-alternatives --install \
/usr/bin/llvm-config llvm-config /usr/bin/llvm-config-3.7 200 \
--slave /usr/bin/llvm-ar llvm-ar /usr/bin/llvm-ar-3.7 \
--slave /usr/bin/llvm-as llvm-as /usr/bin/llvm-as-3.7 \
--slave /usr/bin/llvm-bcanalyzer llvm-bcanalyzer /usr/bin/llvm-bcanalyzer-3.7 \
--slave /usr/bin/llvm-cov llvm-cov /usr/bin/llvm-cov-3.7 \
--slave /usr/bin/llvm-diff llvm-diff /usr/bin/llvm-diff-3.7 \
--slave /usr/bin/llvm-dis llvm-dis /usr/bin/llvm-dis-3.7 \
@pierric
pierric / conj_constraints.hs
Created March 26, 2017 15:06
Convert a list of constraints to one
{-# LANGUAGE ConstraintKinds, DataKinds, TypeOperators, TypeFamilies #-}
import GHC.Exts
type A a = Num a ': Eq a ': Ord a ': '[]
type family ListOfConstraints (a :: [Constraint]) :: Constraint where
ListOfConstraints '[] = ()
ListOfConstraints (c ': cs) = (c, ListOfConstraints cs)
@pierric
pierric / atari-rl.md
Last active June 27, 2017 13:55
Run atari-rl on windows
  • Install MSYS2 (http://www.msys2.org/)

    • pacman -S base-devel mingw-w64-x86_64-gcc mingw-w64-x86_64-cmake
  • Install Anaconda

  • Env

    • conda create --name atari-rl python=3.5
    • activate atari-rl
    • PATH=%PATH%:D:\msys64\mingw64\bin;D:\msys64\usr\bin
      • note, I installed msys64 in D:\msys64
@pierric
pierric / Makefile
Last active July 17, 2017 10:27
A minimal caffe app - mnist
CAFFEDIR=../caffe/cppbuild/linux-x86_64
OPENCVDIR=../opencv/cppbuild/linux-x86_64
INCDIR=${CAFFEDIR}/include
LINKOPTS=-L${CAFFEDIR}/lib -L${CAFFEDIR}/caffe-rc3/build/lib -lboost_system -lboost_thread -lgflags -lglog -lleveldb -llmdb -lopenblas -lprotobuf -lsnappy -lcaffe -lboost_filesystem -pthread -lpthread -L${OPENCVDIR}/lib -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc
train: train.cpp
g++ -o train -DCPU_ONLY -I${INCDIR} train.cpp ${LINKOPTS}
run: train
LD_LIBRARY_PATH=${CAFFEDIR}/lib:${OPENCVDIR}/lib ./train