Skip to content

Instantly share code, notes, and snippets.

;;; based on the discussion at https://groups.google.com/d/msg/netty/mcGswAZ5NeQ/ivG9snRSFy4J
(ns netty4.core
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap]
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption
ChannelHandlerContext ChannelInboundHandlerAdapter ChannelHandler]
[io.netty.handler.logging LogLevel LoggingHandler]
io.netty.buffer.ByteBuf
io.netty.channel.socket.SocketChannel
io.netty.channel.nio.NioEventLoopGroup
@shlomiv
shlomiv / gist:5264147
Last active December 15, 2015 12:59
gloss
(def c (finite-frame (prefix :int16)
[:uuid :int32 (repeated (finite-frame (prefix :int16) (string :utf8)) :prefix :none)]))
(def p (encode c [:uuid 1000 ["ass" "hole"]]))
(let [fp (contiguous p)
s (.limit fp)
arr (.array fp)]
(doseq [i (range s)]
(print (aget arr i) " ")))
(def chopped-codec
(finite-frame
(prefix :int32)
[:uuid :int32 (repeated (finite-frame (prefix :int16) (string :utf8)) :prefix :none)]))
(def response-chopped-codec
(finite-frame
(prefix :int32)
[:uuid :int32 (repeated :int64 :prefix :none)]))
(run
"let count = proc(val) -(0, -(-1, val)) % perform the actual counting on church-numerlas
in let to-num = proc(n) ((n count) 0) % convert from church numerals to num-val
in let next = proc(n) proc(f) proc(x) (f ((n f) x)) % standard church numerals implementations using LC
in let mult = proc(m) proc(n) proc(x) (m (n x)) % multiplication in terms of CN
in let zero = proc(f) proc(x) x % some constants
in let one = proc(f) proc(x) (f x)
in let two = proc(f) proc(x) (f (f x))
in let three = (next two)
in let four = ((mult two) two)
(defroutes app-routes
(GET "/" [] )
(GET ["/shlomi/:id/:name" :id #"[0-9]+" :name #"[a-zA-Z]+"] [id name] (str id name))
(GET "/test" [] (html5 [:head
[:title "YO"]
[:body
[:h1 "Yo!"]
(text-field "test" "whats in here")]]))
(route/resources "/")
(route/not-found "Not Found"))
@shlomiv
shlomiv / first_one_wins_side_effects.clj
Last active December 21, 2015 07:59
this version is useful for side-effects only (like printing)
(defn first-out [& fns]
(let [fs (atom [])
terminate (fn [] (println "cancling..") (doall (map future-cancel @fs)))]
(reset! fs (doall
(map (fn [x] (future-call #((x) (terminate)))) fns )))))
(defn wait-for [n s]
(fn [] (print "start...") (flush) (Thread/sleep n) (print s) (flush)))
(first-out (wait-for 1000 "long") (wait-for 500 "short"))
(defn first-out [& fns]
(let [fs (atom [])
ret (promise)
terminate (fn [x] (println "cancling.." ) (doall (map future-cancel @fs)) (deliver ret x))]
(reset! fs (doall
(map (fn [x] (future-call #(terminate (x)))) fns )))
@ret
))
(defn wait-for [n s]
// load the entire file, and call it fs (all lazy)
val fs = sc.textFile("/data01/fs.txt")
// lets find all lines that contains the string "song", and cache that data source
val songs = fs.filter(x=>x.toLowerCase.contains("song")).cache
// now that we are trying to count, all the previous lazy computations will have to get realized, so this will take about 85
// seconds to complete, but then it will be completly cached.
songs.count
(require '(criterium [core :as q]))
(def ranges [-1 2 5 10 20 40 80 500 1500 Long/MAX_VALUE])
(defn create-partitioner
"returns a function that accepts a number, and returns i if the number is between h(igh) and l(ow)
otherwise returns nil"
[l h i] (fn [v] (if (and (<= v h) (> v l)) i nil) ))
(defn part
@shlomiv
shlomiv / core.clj
Last active June 14, 2017 21:27
a working clojure echo server with netty 4
(ns netty4.core
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap]
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption
ChannelHandlerContext ChannelInboundHandlerAdapter ChannelHandler]
[io.netty.handler.logging LogLevel LoggingHandler]
io.netty.buffer.ByteBuf
io.netty.channel.socket.SocketChannel
io.netty.channel.nio.NioEventLoopGroup
io.netty.channel.socket.nio.NioServerSocketChannel
java.util.concurrent.atomic.AtomicInteger)