Skip to content

Instantly share code, notes, and snippets.

View martintrojer's full-sized avatar
🕺
My hovercraft is full of eels

Martin Trojer martintrojer

🕺
My hovercraft is full of eels
View GitHub Profile
@martintrojer
martintrojer / destruct.clj
Created January 28, 2012 13:13
scheme-clojure
(let [[fst & rst] (parse "(+ 1 1)")]
[fst rst])
--> [:+ (1.0 1.0)]
@martintrojer
martintrojer / aleph-async.clj
Created January 28, 2012 16:41
async-clojure
(async
(for [i (range 10 80)]
(http-request
{:method :get
:url (format "http://fssnip.net/%d" i)}))))
@martintrojer
martintrojer / match-step1.fs
Created January 28, 2012 16:48
fsharp-parsing
let extractField = function
// int32 version
| TAtomic(t) :: TString(n) :: rest ->
Field(n, AtomicType(t)), rest
// alias version
| TString(alias) :: TString(n) :: rest ->
Field(n, AliasType(alias)), rest
| _ ->
failwith "syntax error"
@martintrojer
martintrojer / fib.clj
Created January 28, 2012 16:57
poems
(defn fib [n]
(loop [back1 1, back2 0, n n]
(cond
(= n 0) 0
(= n 1) (+ back1 back2)
:else (recur back2 (+ back1 back2) (- n 1)))))
@martintrojer
martintrojer / cond-example.clj
Created January 28, 2012 17:05
scheme-clojure-embedded
;Scheme
(cond ((> 3 2) 'greater)
((< 3 2) 'less)
(else equal))
;Clojure
(cond (> 3 2) :greater
(< 3 2) :less)
:else :equal)
@martintrojer
martintrojer / eval-apply.fs
Created January 28, 2012 17:18
scheme-fsharp
let rec eval (env:Map<string,expression> list) expr =
match expr with
| NullExpr -> failwith "invalid interpreter state"
| Combination([]) -> failwith "invalid combination"
| Combination(h::t) ->
match eval env h with
| (nenv, Procedure(f)) -> apply f t env
| (nenv, Function(args, code)) ->
let newenv =
try List.fold2 bindArg (expandEnv nenv) args t
@martintrojer
martintrojer / code.il
Last active September 30, 2015 01:08
tail-calls
IL_0000: nop
IL_0001: newobj instance void Program/b@12::.ctor()
IL_0006: ldarg.0
IL_0007: ldc.i4.1
IL_0008: add
IL_0009: tail.
IL_000b: call int32 Program::a(class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<int32, int32="">, int32)
IL_0010: ret
@martintrojer
martintrojer / bytecode-src.html
Created January 29, 2012 09:43
javascript rant
<script type="text/il-bc">
public static void addInt();
Code:
0: bipush 6
2: istore_0
3: bipush 17
5: istore_1
6: iload_0
7: iload_1
8: iadd
@martintrojer
martintrojer / example1.c
Created January 29, 2012 10:22
symbolic execution
int* func(int x; int y) {
int* p=0;
int s=x*y;
if (s!=0)
p=malloc(s);
else if(y==0)
p=malloc(x);
return p;
}
@martintrojer
martintrojer / mandelbrot-ascii.clj
Last active September 30, 2015 23:18
Mandelbrot set
(defn c+ [[re1 im1] [re2 im2]] [(+ re1 re2) (+ im1 im2)])
(defn c* [[re1 im1] [re2 im2]]
[(- (* re1 re2) (* im1 im2)) (+ (* re1 im2) (* im1 re2))])
(defn c2 [c] (c* c c))
(defn |c| [[re im]] (Math/sqrt (+ (* re re) (* im im))))
(defn get-mandel-set [im-range re-range max-iter]
(for [im im-range
re re-range
:let [c [re im]]]