Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
// Make a NEWTYPE
#define NT(inner,outer) \
typedef struct \
{ \
inner _nt_field; \
} outer
// Declare a val of NEWTYPE
module ReadBase where
import Numeric
import Maybe
readBase :: Integer -> String -> [(Integer,String)]
readBase base input = readInt base isValid toValue input
where chars = ['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z']
vals = zip chars [0..(fromIntegral base)]
isValid = flip elem chars
@sw17ch
sw17ch / c_sqlite_test.c
Created January 18, 2011 20:27
Test writing a bunch of blobs at once.
/* gcc -Wall -O3 c_sqlite_test.c -lsqlite3 -o c_sqlite */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sqlite3.h>
/* Macros to simplify things. */
#define BEGIN_TRANSACTION() do { int e; char em[1024]; \
@sw17ch
sw17ch / div2.hs
Created March 17, 2011 21:36
Division by two
-- div2.hs
data Nat = Zero | Succ Nat
deriving (Show)
zero = Zero
one = Succ zero
two = Succ one
three = Succ two
four = Succ three
@sw17ch
sw17ch / gist:1232426
Created September 21, 2011 15:46
What I think a Monad is
A monad is a datatype used to describe actions. Wait, what? What do you mean
'describe an action'?!
Well, at least in Haskell, we're not really using actions or statements; we're
using mathematical transformations. But sometimes, we really want to think of
something a 'doing' something to another thing. In those circumstances, due to
Haskell's semantics, we need a sort of mathematical description of what 'doing
something to something else' means.
The mathematical abstraction of this is a monad.
@sw17ch
sw17ch / subdivide.rb
Created October 31, 2011 18:00
Some times I love monkey patching.
class Array
def subdivide(len)
return self if len <= 0 || len >= self.size
from = 0
result = []
while from < self.size
result << self.slice(from, len)
from += len
end
#include <stdio.h>
#include <string.h>
#include <stdint.h>
uint32_t THE_STATUS_REGISTER = 0;
typedef void (*handler) (void);
typedef struct {
handler hdl;
alias orig_method_missing method_missing
def method_missing(sym, *args)
if driver.methods.include?(sym.to_s)
driver.send(sym, *args)
else
orig_method_missing(sym, *args)
end
end
@sw17ch
sw17ch / gist:1497923
Created December 19, 2011 16:47
Sometimes Haskell warnings can be ... complicated.
dist/build/Language/C/Parser/Lexer.hs:522:17:
Warning: Bindings containing unlifted types should use an outermost bang pattern:
(new_s)
= if (offset >=# 0#) && (check ==# ord_c) then
alexIndexInt16OffAddr alex_table offset
else
alexIndexInt16OffAddr alex_deflt s
In the expression:
let
(base) = alexIndexInt32OffAddr alex_base s
@sw17ch
sw17ch / rr_kosher.rb
Created January 9, 2012 17:06
is this kosher?
describe "thing" do
before do
@obj_1 = Object.new
@obj_2 = Object.new
stub(@obj_1).foo { puts "stubbed foo (1)" }
stub(@obj_2).foo { puts "stubbed foo (2)" }
end
it "1 does things" do
mock(@obj_1).foo { puts "mocked foo (1)" }