Skip to content

Instantly share code, notes, and snippets.

View telescreen's full-sized avatar

Bùi Hồng Hà telescreen

View GitHub Profile
@telescreen
telescreen / pomodoro
Last active May 18, 2023 02:49
A simple script to send notify after a period of time passed
#!/bin/bash
DB=~/.pomodoro.db
if [ ! -f $DB ]; then
sqlite3 $DB "CREATE TABLE timerecord ( record_time text, duration text);"
fi
# Count down $1 number of seconds
countdown() {
start="$(( $(date +%s) + $1))"
@telescreen
telescreen / relf.c
Last active September 11, 2020 02:45
Parse ELF Header
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <elf.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
@telescreen
telescreen / pratt.py
Created August 16, 2020 06:59
Pratt Combinator Parsing using python3
#!/usr/bin/env python3
## Study Pratt parsing for python3
## Ref: http://effbot.org/zone/simple-top-down-parsing.htm
import re
token_pattern = re.compile(r"\s*(?:(\d+)|(\*\*|.))\s*")
def tokenize(program):
@telescreen
telescreen / calc.c
Last active July 16, 2020 15:11
calc.c
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
/* clang -Wall -W -g -std=c11 -Wno-missing-field-initializers -pedantic calc.c -o calc */
typedef enum {
@telescreen
telescreen / tmux.conf
Last active June 8, 2020 02:25
Tmux config
# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-o
bind-key C-o send-prefix
# split panes using | and -
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %
@telescreen
telescreen / sparser.cpp
Last active December 18, 2015 01:59
simple xml parser
/*
* Compile: g++ sparse.cpp
* Run: ./a.out
*
* Test code:
*
* <tag1><tag11>aa</tag11><tag12>bb</tag12></tag><tag21>cc</tag21>
* <tag><tag1>fdfdf</tag1></tag>
* <tag><tag1>fdfdf<tag3>fdf</tag3></tag1></tag>
*
@telescreen
telescreen / 99probs.ml
Last active December 17, 2015 03:59
99 problems ocaml solutions
(* Write a function last : 'a list -> 'a option that returns the last element of a list. *)
let rec last l = match l with
[] -> None
| [x] -> Some x
| x :: l -> last l
(* Find the last but one (last and penultimate) elements of a list. *)
let rec last_two l = match l with
[] -> None
| [x] -> None
@telescreen
telescreen / refstr.cpp
Created February 6, 2013 06:22
Reference counted string (example of handle-body idiom)
#include <iostream>
class StringRep {
friend class String;
private:
StringRep() { *(rep = new char[1]) = '\0'; }
~StringRep() { delete[] rep; }
StringRep(const StringRep& s) { ::strcpy((rep = new char[::strlen(s.rep)+1]), s.rep); }
StringRep& operator=(const StringRep& s) {
if (rep != s.rep) {
-- Given a list (for instance [1,2,3]), return its powerset (a set of subsets)
-- Combinatorics guys: well a subset is a set with/without the first element
-- Hoola!
-- powerset [1,2,3] = [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
powerset :: [a] -> [[a]]
powerset [] = [[]]
powerset (x:xs) = let ss = subset xs in ss ++ (map (\ys -> x:ys) ss)
-- Monad guys: after a few minutes thinking
@telescreen
telescreen / latency.txt
Created June 1, 2012 12:07 — forked from jboner/latency.txt
Latency numbers every programmer should know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
SSD random read 150,000 ns
Read 1 MB sequentially from memory 250,000 ns 0.25 ms
Round trip within same datacenter 500,000 ns 0.5 ms