Skip to content

Instantly share code, notes, and snippets.

-- sort function, optimized for lists
-- TODO - profiling
sortBy compare = head . head . dropWhile (isn't unsafeCoerce . drop 1) . group . iterate bubble
where
bubble [] = []
bubble [x] = [x]
bubble (x:y:ys) =
if compare x y == GT
then y:(bubble (x:ys))
else
@andyfriesen
andyfriesen / mono.bat
Created August 30, 2014 19:31
I feel retarded for writing this: On Windows, you can invoke a CLR executable directly, but on Linux and OSX, you have to launch with the mono executable. This is a "batch" file that is also a shell script that invokes a CLR executable correctly on whatever OS you are on. It's useful if, for instance, your IDE specifies a post-build action that …
echo off
echo ; set +v # > NUL
echo ; function GOTO { true; } # > NUL
GOTO WIN
# On Unix, we need to explicitly invoke CLR executables with mono
/usr/bin/env mono $@
exit 0
:WIN
struct S {
S(int) {}
S(const S&) = delete;
};
int main() {
auto a = S { 5 }; // no go
S s {5}; // ok
return 0;
}
@andyfriesen
andyfriesen / trie.rs
Last active August 29, 2015 14:08
I made a simple trie in Rust
use std::str::CharRange;
struct El<T> {
ch : char,
children : Trie<T>
}
pub struct Trie<T> {
elements : Vec<El<T>>,
data : Option<T>
@andyfriesen
andyfriesen / irrefutable.hs
Created November 10, 2014 06:20
ghc (7.8.1) accepts this program, even if you specify -Wall -Werror. :(
x :: String
Just (Just (x:[])) = Just (Just [])
main :: IO ()
main = putStrLn x
@andyfriesen
andyfriesen / colourize.sh
Last active August 29, 2015 14:09
Use Pygments to colourize code in the clipboard
#!/bin/bash
set -ex
if [ `uname` == 'Linux' ]; then
PASTE=xclip\ -o
OPEN=xdg-open
else
# Assume OSX
PASTE=pbpaste
{-# LANGUAGE RecordWildCards #-}
module Promise where
import Control.Monad
import Data.IORef
import Data.Maybe
data Status
= Pending
@andyfriesen
andyfriesen / Main.js
Created April 27, 2015 04:24
Hello World, as produced by Haste
// This object will hold all exports.
var Haste = {};
/* Thunk
Creates a thunk representing the given closure.
If the non-updatable flag is undefined, the thunk is updatable.
*/
function T(f, nu) {
this.f = f;
if(nu === undefined) {
use std::ops::{Add, Sub};
#[derive(Copy, Debug)]
pub struct Rect<T> {
pub left: T,
pub top: T,
pub right: T,
pub bottom: T
}
@andyfriesen
andyfriesen / hurbl.md
Created July 12, 2016 00:45
A quick example of why STM is amazing

Say we want to spin off threads every so often, but we also need the ability to wait until they all go away before exiting the application.

In Haskell, we can do this naive thing:

main = do
    count <- newTVarIO 0
    
 let spawnThread action = do