Skip to content

Instantly share code, notes, and snippets.

@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>
struct S {
S(int) {}
S(const S&) = delete;
};
int main() {
auto a = S { 5 }; // no go
S s {5}; // ok
return 0;
}
@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
-- 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 / TestableIO.hs
Last active August 10, 2021 18:25
Tiny example showing how to force your Haskell code to be 100% deterministic under unit tests
{-# LANGUAGE FlexibleInstances #-}
module Main where
import Control.Applicative ((<$>))
import Control.Monad.State.Lazy as S
class Monad m => World m where
writeLine :: String -> m ()
instance World IO where
@andyfriesen
andyfriesen / oops.cpp
Last active August 29, 2015 14:03
It would be nice if this generated a compiler warning
#include <stdio.h>
enum Enumeration {
ENUM = 0,
};
struct S final {
explicit S(unsigned short id)
: S(ENUM, value)
{ }
template<class T,class...>class C{C<T*const,T,C>a;C<T,C>b;};C<int>c;
(defun revert-all-buffers ()
"Refreshes all open buffers from their respective files."
(interactive)
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (and (buffer-file-name) (not (buffer-modified-p)))
(revert-buffer t t t) )))
(message "Refreshed open files.") )
@andyfriesen
andyfriesen / lolphp.php
Created February 7, 2014 01:38
PHP and function indirection
<?php
class C {
public function call_private_function($f) {
return $f();
}
private function run() {
echo "run!\n";
}
@andyfriesen
andyfriesen / lolphp.php
Created February 7, 2014 00:53
PHP, __call and access protection.
<?php
class B {
public function __call($fn, $args) {
$c = array($this, 'hello');
$c();
}
private function hello() {
echo "hello B\n";