Skip to content

Instantly share code, notes, and snippets.

import Criterion.Main
prswap [] = []
prswap (a:b:xs) = b:a:prswap xs
prswap' [] = []
prswap' list = reverse (take 2 list) ++ prswap (drop 2 list)
main = defaultMain [ bgroup "prswap" [ bench "prswap [1..1000]" $ whnf prswap [1..1000]
, bench "prswap [1..10000]" $ whnf prswap [1..10000]
module KleisliComp where
import Control.Arrow
import Data.Foldable
import Data.Monoid
import Data.Monoid.Endomorphism
-- After explicitly writing the KleisliEndo instance, I realized that it could
-- be built using existing infrastructure, namely the Kleisli category instance
-- and the Monoid instance for category endomorphism composition.
@cleichner
cleichner / wrapM.hs
Last active August 29, 2015 14:02
wrapM
wrapM :: Monad m => (a -> b) -> (a -> m b)
wrapM = (return .)
-- same as wrapM f = \x -> return (f x)
@cleichner
cleichner / dijkstra.py
Created February 10, 2012 18:17
Dijkstra's Algorithm in Python
from heapq import heapify, heappop
from binascii import a2b_hex
from collections import defaultdict
from struct import unpack
from sys import argv, exit
def parse_byte(bstring):
return unpack('>B', a2b_hex(bstring))[0]
def dijkstra(graph, source):
@cleichner
cleichner / conway.py
Created March 19, 2012 16:04
Succinct Game of Life
# Jack Diederich wrote the advance function
# original code from http://pyvideo.org/video/880/stop-writing-classes
import collections
import curses
import gettext
import random
import signal
import sys
import time
@cleichner
cleichner / 16x16 maze
Created July 27, 2012 18:41
Maze Parsing
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| _ _ _ _ | _ _ _ _ _ |
| |_|_ |_|_ _| _|_ _ _| |
| |_ _|_ | |_| |_ _ _| |_|
|_| |_ _ _|_ _ _| |_ _|
| | |_ _ | |_|_|_ | _|_ |
| _| | |_|_ _| |_ _ | |
| | | _| |_| |_ _ _| | |
| |_| |_|_ _ | |_ _|_ _|_ | |
|_| _| _ _ _| | | | _ _ | |_|
@cleichner
cleichner / LICENSE
Last active December 20, 2015 06:29
I re-wrote a toy Java program in Haskell and messed with the style ... a lot.
Copyright 2014 Chas Leichner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@cleichner
cleichner / butts
Last active December 22, 2015 04:18
Command-line utility to display Lady's "Twerk" video.
#!/usr/bin/env python
'''
The MIT License (MIT)
Copyright (c) 2013 Chas Leichner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@cleichner
cleichner / null.py
Created October 27, 2013 00:09
This eats nearly everything that it interacts with.
'''
Null module:
Responds to all actions and requests for members with a null object.
For example, this code will execute the debugging tools in development, but
won't do anything in production.
if environment == development:
import ExpensiveDebugging
else:
@cleichner
cleichner / cleichner.hs
Last active January 2, 2016 20:59
The laziest high-performance bf compiler ever.
import Control.Monad
import Data.List
data BF a = Add a
| Move a
| StartLoop
| EndLoop
| Write
| Read
| Zero