Skip to content

Instantly share code, notes, and snippets.

View megakorre's full-sized avatar

Patrik Kårlin megakorre

View GitHub Profile

Keybase proof

I hereby claim:

  • I am megakorre on github.
  • I am megakorre (https://keybase.io/megakorre) on keybase.
  • I have a public key ASAuD9d2Yv2fzydlqsmDvq-Jz9Psh2qoPwFCji9_DFG0mAo

To claim this, I am signing this object:

package main
import (
"bufio"
"io"
"net"
"os"
)
func echoLoop(c chan string,broadcast chan string, con net.Conn) {
@megakorre
megakorre / let-map.clj
Created August 21, 2012 09:23
let-map
(defmacro let-map [& bindings]
`(let ~(into [] bindings)
~(reduce
(fn [p [n _]]
(if (= \- (first (name n)))
p
(assoc p (keyword (name n)) n)))
{}
(partition 2 bindings))))
enum expr {
val(int),
plus(&expr, &expr),
minus(&expr, &expr)
}
mod enum_mod {
export run;
enum expr {
val(int),
plus(&expr, &expr),
minus(&expr, &expr)
}
fn eval(e: &expr) -> int {
@megakorre
megakorre / rust-poly.md
Created August 8, 2012 21:47
blog: rust pollymorpism

EDIT! full code can be found here gist

So I played around with rust a bit now. And I thought I might share some random stuff.

So say you wanted to represent a expression tree with plus and minus nodes and a node for values. One way to do this would be to use rust's enum's.

In rust enums are like haskell's union types so you can specify the different values to be constructors carrying data.

test.rs:16:19: 16:22 error: mismatched types: expected `fn@(&&<V6>) -> <V8>` but found `extern fn(&int) -> &int` (expected argument mode ++ but found &&)
test.rs:16 let v = mapper(inc, add);
^~~
test.rs:16:24: 16:27 error: mismatched types: expected `fn@(&&<V7>, &&<V8>) -> <V7>` but found `extern fn(&int, &int) -> &int` (expected argument mode ++ but found &&)
test.rs:16 let v = mapper(inc, add);
^~~
error: aborting due to 2 previous errors
@megakorre
megakorre / func-interface.clj
Created July 18, 2012 18:43
functional interface implementation in clojure
(defmacro fi
[interface args & code]
(let [interface-type (.getMapping *ns* interface)
methods (-> (.getMethods interface-type)
seq)
method-sym (.getName (first methods))]
(when-not (= (count methods) 1)
@megakorre
megakorre / gist:2889294
Created June 7, 2012 15:06
aggr a macro for reduce comprihension*
(defn compile-aggr [binding code]
(let [[scoped expression & r] binding]
(when (and scoped (not expression))
(throw (new Exception "aggr needs a even number of parameters")))
(if scoped
`(reduce
(fn [a# b#] (let [~scoped [a# b#]] ~(compile-aggr r code)))
~expression)
`(do ~@code))))
@megakorre
megakorre / gist:2474043
Created April 23, 2012 21:41
console log clojurescript
(.log js/console "Hello, World")