Skip to content

Instantly share code, notes, and snippets.

@nonowarn
nonowarn / ffi_qsort.hs
Created October 25, 2009 08:21
Haskell <-> C using Function Pointer
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign
import Foreign.C.Types
import Control.Applicative
foreign import ccall "stdlib.h qsort"
c_qsort :: Ptr a
-> CSize
-> CSize
/* 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
@nonowarn
nonowarn / P.th
Created December 24, 2009 02:42
StringIO but in Haskell (on GHC-6.12)
=
GHC 6.12 の Unicode I/O
Yusaku Hashimoto
<nonowarn@gmail.com>
-
自己紹介
-
京都の組み込み系専門学生の
皮をかぶった Haskeller
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 () {
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"