Skip to content

Instantly share code, notes, and snippets.

require "benchmark"
class Array
def duplicates_reject_uniq
reject { |e| count(e) <= 1 }.uniq
end
def duplicates_uniq_reject
uniq.reject { |e| count(e) <= 1 }
end
(function (win, doc) {
var by = {}, slice = [].slice, type2method = {
id: "getElementById",
className: "getElementsByClassName",
tagName: "getElementsByTagName",
query: "querySelectorAll"
};
for (var type in type2method) if (type2method[type] in doc) (function (type, method) {
by[type] = method.match(/(?:Elements|All)/) ? function () {
/* Copied, Pasted and summarized from ps' source code.
You can use sysctl to get other process' argv.
*/
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define pid_of(pproc) pproc->kp_proc.p_pid
class Proc
def on(g)
f = self
-> a, b { f[g[a], g[b]] }
end
end
class Symbol
def to_func
-> this, *args { this.__send__ self, *args }
instance Fractional Integer where
fromRational x = 0
instance Fractional Int where
fromRational x = 1
a :: Int
a = 0
b :: Fractional a => a
{-# LANGUAGE NoMonomorphismRestriction #-}
data N = A | B | C | D
instance Num N where
fromInteger 0 = C
fromInteger 1 = B
A + B = B
A + C = C
A + D = B
@nonowarn
nonowarn / h.rb
Created May 3, 2010 07:52
javascript-like Hash in Ruby
class H
def initialize(hash) @hash = hash end
def method_missing(name, *args)
if name =~ /([^=]+)=$/
return @hash[$1.intern] = args.first
end
@hash[name] || @hash[name.to_s]
end
end
@nonowarn
nonowarn / TypeEq.hs
Created March 2, 2010 11:16
Equality on Types
-- based on http://okmij.org/ftp/Haskell/de-typechecker.lhs
{-# LANGUAGE IncoherentInstances,UndecidableInstances #-}
data HTrue
data HFalse
instance Show HTrue where show _ = "HTrue"
instance Show HFalse where show _ = "HFalse"
@nonowarn
nonowarn / Points.hs
Created March 2, 2010 00:39
Example of has
import Data.Has
import Control.Applicative
data X = X; data Y = Y; data Z = Z
newtype Point2D = P2 (X :> Int :&: Y :> Int)
deriving (Has (X `Labelled` Int),Has (Y `Labelled` Int))
getXY :: (Knows X Int p, Knows Y Int p) => p -> (Int,Int)
getXY = liftA2 (,) (prjl X) (prjl Y)
@nonowarn
nonowarn / Makefile
Created February 11, 2010 03:47
Brainf**k Interpreter
bf: bf.hs
ghc --make -O2 bf