Skip to content

Instantly share code, notes, and snippets.

View ncfavier's full-sized avatar
🌑

Naïm Favier ncfavier

🌑
View GitHub Profile
@ncfavier
ncfavier / classical.gen
Last active November 10, 2017 17:22
Classical music title generator for http://orteil.dashnet.org/randomgen
$include http://orteil.dashnet.org/randomgen/gens/names.txt
$name : Classical piece title generator
$author : Naïm Favier
$description : Generates a random classical music title.
$picture : http://i.imgur.com/YG6jDoC.png
$amount : 1
$button : Compose!
$form
@ncfavier
ncfavier / 2048.py
Last active February 10, 2018 04:57
Text-based 2048 clone
#!/usr/bin/env python3
from random import randint
size = 4
score = 0
grid = [[0 for i in range(size)] for i in range(size)]
def display(grid):
print()
for i in range(size):
@ncfavier
ncfavier / gaston.S
Last active June 4, 2019 20:40
GASton serves index.html on port 8080.
# to compile:
# gcc gaston.S -nostdlib -no-pie
#define SYS_WRITE 1
#define SYS_OPEN 2
#define SYS_CLOSE 3
#define SYS_LSEEK 8
#define SYS_SENDFILE 40
#define SYS_SOCKET 41
#define SYS_ACCEPT 43
@ncfavier
ncfavier / snake.py
Last active March 14, 2018 03:17
Python curses snake game
#!/usr/bin/env python3
import curses
import time
import random
SNAKE_COLOR, FOOD_COLOR = 1, 2
UP, RIGHT, DOWN, LEFT = (0, -1), (1, 0), (0, 1), (-1, 0)
INITIAL_LENGTH = 10
INITIAL_SPEED = 15
SPEED_INCREMENT = 0.5
@ncfavier
ncfavier / znc2weechat.bash
Last active October 31, 2019 15:30
Convert ZNC logs to WeeChat logs (in French)
#!/usr/bin/env bash
channel=$1
while read -rd ' ' time && read -rd ' ' event && IFS= read -r message; do
time=${time#[} time=${time%]}
case $event in
'***')
if [[ $message =~ ^(.*)' is now known as '(.*)$ ]]; then
event='*'
@ncfavier
ncfavier / traversoids.md
Last active November 16, 2021 13:03
Summary of Haskell's traversal functions
traverse   :: (Traversable t, Applicative f) => (a -> f b) -> t a        -> f (t b)
traverse_  :: (Foldable    t, Applicative f) => (a -> f b) -> t a        -> f ()
for        :: (Traversable t, Applicative f) => t a        -> (a -> f b) -> f (t b)
for_       :: (Foldable    t, Applicative f) => t a        -> (a -> f b) -> f ()
sequenceA  :: (Traversable t, Applicative f) => t (f a)                  -> f (t a)
sequenceA_ :: (Foldable    t, Applicative f) => t (f a)                  -> f ()
mapM       :: (Traversable t, Monad       m) => (a -> m b) -> t a        -> m (t b)
mapM_      :: (Foldable    t, Monad       m) => (a -> m b) -> t a        -> m ()
forM       :: (Traversable t, Monad       m) => t a        -> (a -> m b) -> m (t b)
@ncfavier
ncfavier / ExistentialMaps.hs
Created July 25, 2020 18:25
Stupid utilisation of impredicative polymorphism to merge the key lists of two maps with different value types using `on`. Do not try at home.
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ImpredicativeTypes #-} -- HIGHLY EXPERIMENTAL
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Function
-- Say we want to define this function:
mergeKeys :: Map k a -> Map k b -> [k]
{- The obvious implementation is of course
@ncfavier
ncfavier / bridge.sh
Last active August 18, 2020 22:59
Syncplay<->IRC bridge
#!/usr/bin/env bash
# dependencies: bash 4+, jq
. ./config.sh
: "${verbose:=0}"
: "${syncplay_host:=syncplay.pl}"
: "${syncplay_port:=8999}"
: "${syncplay_nick:=syncplaybridge}"
: "${syncplay_room:?"can't be empty"}"
: "${irc_host:=chat.freenode.net}"
@ncfavier
ncfavier / day1.nix
Last active December 2, 2020 18:14
Advent of Code 2020 in Nix
#!/usr/bin/env -S nix-instantiate --eval --strict
# real 0m4.143s
with (builtins.getFlake "nixos").lib;
let
tails = l: if l == [] then [] else [ l ] ++ tails (tail l);
sum = foldl' (x: y: x + y) 0;
product = foldl' (x: y: x * y) 1;
lines = s: splitString "\n" (removeSuffix "\n" s);
nums = map toInt (lines (builtins.readFile ./input1));
@ncfavier
ncfavier / SevenTreesInOne.hs
Last active December 25, 2021 16:11
A very explicit bijection from Andreas Blass' "Seven Trees in One" https://arxiv.org/pdf/math/9405205.pdf
{-# LANGUAGE LambdaCase #-}
module SevenTreesInOne where
import Test.QuickCheck
infixl :*
data T = O | T :* T
deriving (Eq, Ord, Show)
type T7 = (T, T, T, T, T, T, T)