Skip to content

Instantly share code, notes, and snippets.

View jbpotonnier's full-sized avatar

Jean-Baptiste Potonnier jbpotonnier

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / grep_gist.rb
Created June 14, 2012 23:14
Grep in gist description and display url
require 'net/http'
require 'uri'
require 'json'
def github(*list)
URI('https://api.github.com/' + list.join('/'))
end
def get(uri)
request = Net::HTTP::Get.new uri.request_uri
@jbpotonnier
jbpotonnier / .gitignore
Created June 5, 2012 21:10
Paths in ul-li
bin
local
include
lib
man
.Python
*.pyc
*.swp
@jbpotonnier
jbpotonnier / model.py
Created June 4, 2012 21:50
Very basic json validation (you more likely want something like kwalify or WTForm)
from exceptions import Exception
def is_valid(model, instance):
missing_fields = set(model) - set(instance)
if missing_fields:
raise MissingFields(missing_fields)
missing_types = set(instance) - set(model)
if missing_types:
raise MissingTypes(missing_types)
type_errors = [(k, model[k], type(v)) for (k, v) in instance.items() if type(v) is not model[k]]
@jbpotonnier
jbpotonnier / map.hs
Created June 4, 2012 21:46
map is just fold, or is it the other way around :P Is fold enough to have map, filter, and recursion?
foldlmap :: (a -> b) -> [a] -> [b]
foldlmap f = foldl (\ a b -> a ++ [f b]) []
foldrmap :: (a -> b) -> [a] -> [b]
foldrmap f = foldr (\ a b -> (f a) : b) []
main :: IO ()
main = do