Skip to content

Instantly share code, notes, and snippets.

View jbpotonnier's full-sized avatar

Jean-Baptiste Potonnier jbpotonnier

View GitHub Profile
@jbpotonnier
jbpotonnier / find_by_key.py
Created June 15, 2012 19:22
Find recursively by key
def find(d, key):
if key in d:
yield d[key]
for c in d.get('children', []):
for e in find(c, key): yield e
@jbpotonnier
jbpotonnier / switch_dv_fr
Created July 18, 2012 09:09
Switch between dvorak and fr keyboard. I map it using my window manager
#!/bin/bash
kbd=`setxkbmap -query | grep layout | awk '{print $2}'`
if [ 'fr' = $kbd ]
then
notify-send 'Keyboard is now Dvorak'
setxkbmap dvorak en -option compose:ralt
else
notify-send 'Keyboard is now French'
@jbpotonnier
jbpotonnier / fizzbuzz.hs
Created August 3, 2012 21:12
My fizz/buzz in Haskell and its direct translation in Python (inspired from http://www.free-variable.org/2009/07/were-not-in-kansas-anymore-toto/ ).
fizz = cycle ["", "", "fizz"]
buzz = cycle ["", "", "", "", "buzz"]
numbers = map show [1..]
combine "" "" number = number
combine "fizz" "buzz" _ = "Fizz buzz!"
combine "fizz" _ _ = "Fizz!"
combine _ "buzz" _ = "Buzz!"
fizzbuzz = zipWith3 combine fizz buzz numbers
@jbpotonnier
jbpotonnier / currency.hs
Created August 13, 2012 20:32
I try to understand phantom types in Haskell. In main, try eur 12 <+> usd 23 and see the compilation error, saying that you cannot add euros and US dollars. Neat ;)
data EUR
data USD
data Amount a = Amount Float deriving Show
eur :: Float -> Amount EUR
eur a = Amount a
usd :: Float -> Amount USD
usd a = Amount a
@jbpotonnier
jbpotonnier / morse.hs
Created October 16, 2012 21:08
Morse code base in Haskell
mport Sound.Tomato.Speakers
sound freq duration = withSpeakers sampleRate 128 $ \s -> playSamples s sound
where
sampleRate = 22050
dt = 1 / sampleRate -- time in seconds of a single sample
sound = take (ceiling $ duration / dt)
$ map (0.3*) $ sine freq
sine freq = [sin (2*pi*freq*dt*fromIntegral t) | t <- [0..]]
@jbpotonnier
jbpotonnier / numeric_maze.hs
Created October 26, 2012 23:28
Solving http://www.rubyquiz.com/quiz60.html with the power of list monad
import Control.Monad (guard)
double :: Int -> Int
double = (2 *)
add_two :: Int -> Int
add_two = (2 +)
halve :: Int -> Int
halve n = n `div` 2
@jbpotonnier
jbpotonnier / posfix.ml
Last active October 12, 2015 05:18
Postfix evaluation
let push : float list -> string -> float list =
fun stack input -> match stack, input with
| (b::a::rest), "+" -> a +. b::rest
| (b::a::rest), "-" -> a -. b::rest
| (b::a::rest), "*" -> a *. b::rest
| (b::a::rest), "/" -> a /. b::rest
| rest, number -> (float_of_string number)::rest
let polish_eval : string -> float =
fun input ->
@jbpotonnier
jbpotonnier / Main.hs
Last active December 10, 2015 19:18
EventSource streaming using Haskell and redis brpop. Use redis-cli to push some data in the "val" list and see it appear in your browser. It is not that hard... finally. There are some events not appearing at the beginning of the sequence, I don't know why, after that it works well.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative
import Snap.Core
import Snap.Util.FileServe
import Snap.Http.Server
import Data.Monoid (mconcat)
import Blaze.ByteString.Builder.Char8
import Blaze.ByteString.Builder
@jbpotonnier
jbpotonnier / override.js
Last active December 14, 2015 01:38
Override method in JavaScript
var create = function (proto) {
var F = function () {};
F.prototype = proto;
return new F();
};
var Point = {};
Point.initialize = function (x, y) {
this.x = x;
this.y = y;
@jbpotonnier
jbpotonnier / accessor.hs
Last active December 18, 2015 02:08
Access json data using path. Functional programming to the rescue :P
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as Text
import Data.Text (Text)
import Data.Aeson (encode, decode, Value, Value(..))
import qualified Data.HashMap.Strict as HashMap
import Data.Vector ((!))
path :: Text -> Value -> Value