Skip to content

Instantly share code, notes, and snippets.

View lambda-fairy's full-sized avatar

Chris Wong lambda-fairy

View GitHub Profile
@lambda-fairy
lambda-fairy / keybase.md
Created April 8, 2015 03:43
Keybase proof

Keybase proof

I hereby claim:

  • I am lfairy on github.
  • I am lfairy (https://keybase.io/lfairy) on keybase.
  • I have a public key whose fingerprint is 0658 29A6 73B2 9A01 6E68 EC42 4AFA FC07 E9D2 794C

To claim this, I am signing this object:

-- | Find the triangle number T(n)
triangle :: Integer -> Integer
triangle n = (n * (1 + n)) `div` 2
-- | Find the greatest k where T(k) <= n
invTriangle :: Integer -> Integer
invTriangle n = (intSqrt (1 + 8*n) - 1) `div` 2
-- | Find the greatest k where k^2 <= n
intSqrt :: Integer -> Integer
@lambda-fairy
lambda-fairy / fibonacci.py
Last active August 29, 2015 14:16
Fibonacci in logarithmic time
from collections import namedtuple
class Transform(namedtuple('Transform', 'p q')):
"""
The value ``Transform(p, q)`` represents the matrix
``[[p+q, q], [q, p]]``.
"""
def __mul__(self, other):
"""Compose two transformations end-to-end."""
@lambda-fairy
lambda-fairy / dedup.rs
Last active January 31, 2019 23:22
.dedup() on iterators
use std::iter::Peekable;
trait IteratorDedup: Iterator + Sized {
fn dedup(self) -> Dedup<Self> where Self::Item: PartialEq {
Dedup {
inner: self.peekable(),
emit: true,
}
}
}
@lambda-fairy
lambda-fairy / benches.md
Created January 29, 2015 08:00
to_string() benchmarks

Before:

running 4 tests
test empty_str_to_string ... bench:        32 ns/iter (+/- 3)
test i32_to_string       ... bench:        81 ns/iter (+/- 1)
test long_str_to_string  ... bench:        93 ns/iter (+/- 1)
test short_str_to_string ... bench:        75 ns/iter (+/- 0)

test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured
@lambda-fairy
lambda-fairy / git-retime.sh
Last active August 29, 2015 14:13
git-retime: edit the timestamp of the HEAD commit
#!/bin/sh
set -e
USAGE=''
SUBDIRECTORY_OK=1
. "$(git --exec-path)/git-sh-setup"
if [ $# -gt 0 ]
@lambda-fairy
lambda-fairy / hircine.hs
Last active August 29, 2015 14:12
Hircine API
-- Module is a Category and Profunctor and Monoid
data Module a b
~ (b -> IO ()) -> Codensity IO (a -> IO ())
-- Action is a Functor and Monad
data Action b r
~ ReaderT (b -> IO ()) IO r
~ (b -> IO ()) -> IO r
connect :: String -> Int -> Module Message [Command] -> IO a
@lambda-fairy
lambda-fairy / gist:a36817c892d5bf672c6e
Created September 20, 2014 08:27
Encode OGG Vorbis using gstreamer
gst-launch-1.0 filesrc location=music.wav ! decodebin ! audioconvert ! vorbisenc ! oggmux ! filesink location=music.ogg
@lambda-fairy
lambda-fairy / PropCheck.hs
Created August 14, 2014 05:17
Fancy (classical) propositional logic checker
{-# LANGUAGE TypeFamilies #-}
import Control.Applicative
import Data.Functor.Compose
------------------------------------------------------------------------
-- Preliminaries
(-->) :: Bool -> Bool -> Bool
@lambda-fairy
lambda-fairy / CaseInsensitive.hs
Created July 31, 2014 01:49
Data.CaseInsensitive and (Co)Traversable
traverse :: (FoldCase s1, FoldCase s2, Functor f) => (s1 -> f s2) -> CI s1 -> f (CI s2)
traverse f = fmap CI.mk . f . CI.original
cotraverse :: (FoldCase s1, FoldCase s2, Functor f) => (f s1 -> s2) -> f (CI s1) -> CI s2
cotraverse f = CI.mk . f . fmap CI.original
-- Examples
concat :: [CI ByteString] -> CI ByteString
concat = cotraverse B.concat