Skip to content

Instantly share code, notes, and snippets.

View sntran's full-sized avatar

Sơn Trần-Nguyễn sntran

View GitHub Profile
defmodule XmlNode do
require Record
Record.defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
def from_string(xml_string, options \\ [quiet: true]) do
{doc, []} =
xml_string
|> :binary.bin_to_list
|> :xmerl_scan.string(options)
@sntran
sntran / physics.js
Last active August 29, 2015 14:07
Physics System with makr.js and p2.js
function Physics(bodyConfs, shapeConfs) {
this.body = bodyConfs;
this.shape = shapeConfs;
}
function PhysicsSystem(gravity) {
makr.IteratingSystem.call(this);
this.registerComponent(ComponentRegister.get(Position));
this.registerComponent(ComponentRegister.get(Rotation));
this.registerComponent(ComponentRegister.get(Physics));
@sntran
sntran / Interesting Erlang Repos.md
Created December 29, 2013 22:45
Interesting Erlang Repos From time to time, I came across some interesting repos when researching on Erlang. They are interesting in many ways, i.e., their purpose, their choice of libraries, and their way of handling the task.
#!/usr/local/bin/python2.7
#!/usr/local/bin/python2.7
# (Minimal) Perfect Hash Functions Generator (key, value) value in this code is the key counter during reading but can be any number
# implementing the MOS Algorithm II CACM92 , and Amjad M Daoud Thesis 1993 at VT;
# based on Steve Hanof implementation http://stevehanov.ca/blog/index.php?id=119.
# Download as http://iswsa.acm.org/mphf/mphf.py
@sntran
sntran / Cactus Kev's Poker Hand Evaluator.md
Last active December 20, 2023 19:14
Cactus Kev's Poker Hand Evaluator (taken from http://suffe.cool/poker/evaluator.html)

A while ago, I decided to take a shot at writing a poker hand evaluator in the programming language "C". There are already numerous evaluators out there, but I had an idea for an algorithm that might be faster than anything already out there. The basic concept is to write a routine that would take a five card poker hand and return it's overall "value". This is extremely valuable in any poker-related software, since the code will constantly be comparing various player's hand with each other to determine the "winner". Here is my concept on how I thought I could write a fast evaluator.


Okay, before we start digging into my algorithm, please read this first. I usually get one or two emails every month from somebody interested in my algorithm or poker code. And they typically ask me if I happen to have a seven-card poker evaluator. The answer is yes. I did indeed write a seven-card hand evaluator a few years after writing the five-card one. It used a completely new algorithm, completely unrelated to my five-

@sntran
sntran / poker.erl
Created November 26, 2013 23:25
Peter Norvig's 5-card Poker hand evaluator, ported to Erlang.
%%%
%%% Peter Norvig's 5-card Poker hand evaluator, ported to Erlang.
%%%
-module(poker).
-export([hand_rank/1, sort_hands/1, winners/1]).
%% Return a list of the ranks, sorted with higher first.
%% Hand is a list of 2-char strings, i.e. ["6H","3D","AS","TH","JC"].
card_ranks(Hand) ->
@sntran
sntran / New Entry.md
Last active December 27, 2015 06:49
New Entry

Ec ido metr decilitro, iz celo semajntago via. Jh volu punktokomo ies, nk plus stil neni hop, igi in tempo renkonten komplemento. Nura nenia centimetro vir co, tc pri loka stop konsonanto, go okej' predikato por. Plena daralbajdo fi veo, povi panjo tet ok, is pli vatto komparado komplemento. Ut alial altmontaro tri, nul uj falsa nekutima.

Gv ing falsa siatempe interogativo. Mili grado monatonomo ek san, jes liternomo difiniteco substantiva as, oni pere poezio frazparto en. Tio kelke samtempe ne, nf tet vira kaŭzo, an eks liva rilativo. Ehe disde aliom gentonomo at, meze alternativa hej in, hoj ng tiuj tempopunkto memkompreneble. Pra nu hago nette, unu titolo tagnokto prepozicio ba. Ador hebrea duobla anc ha, dev bo foren grupa ometr, al ore olog geto. Ali il alio miriametro suplemento.

Hot kontraŭa malebligi ul. Dura getto infra sep ki, cent lingvonomo tutampleksa avo ve, oz plej afro ont. Hura ometr mf fin. Geedzo ipsilono bo ien. Oni krome estiel subtraho tc. Tio ne apuda subjunkcio, en multe kr

@sntran
sntran / Caching and Pagination.md
Last active December 25, 2015 03:49
Caching and Pagination When implementing the cache system for [Gists.IO](http://github.com/sntran/gists.io), we hit a wall with pagination. Mostly due to our lack of knowledge on caching, and the difference between our definition and GitHub's one on pagination. So let this entry be the rubber duck.

Lets talk about cache

Initially, we cache only individual gist using the key {:gist, gist_id, username} in which the first element of the tuple is the atom :gist to act as the tag, then the gist_id and the username of the owner of that gist.

When fetching for gists of a particular user, we perform a match on keys with pattern {:gist, :'$1', username} to retrieve all the cached gists by that username. All went well, or so we thought.

Lets talk about pagination

GitHub's API for fetching Gists will return a Link header when there are more results than the default per_page query, which is 30. The header has the following format when fetching gists for myself:

@sntran
sntran / Gists.IO - Just Write It.md
Last active December 24, 2015 20:29
Gists.IO - Just Write It A simple Gist\-based blogging platform for those of us hacker to jolt down our thoughts without concerning about server setup.

As developer/hacker, we are all caught up in our projects, but some times we want to write. Write about our thought process, our ideas, our instructions to do certain thing, etc...

But we don't have time to set up a blog server. Other services are there but not many of them target us, the hackers. We want something simple, with ability to embed code, and not other fancy stuffs.

So we made this. What you can do are:

  • Author (create/edit/delete) an entry using Markdown
  • Attach code files and embed them inline
  • Revert back to previous version of the entry.
  • Use Emoji 👍
@sntran
sntran / index.html
Created September 20, 2013 13:32 — forked from roman01la/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="viewport"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/three.js/r58/three.min.js"></script>
<script src="//threejs.org/examples/js/controls/OrbitControls.js"></script>