Skip to content

Instantly share code, notes, and snippets.

@shlomiv
shlomiv / banner_maker.clj
Created February 18, 2015 11:44
example writing antialiased text and an image on top of an existing image
(ns banner-maker.core
(:require [clojure.java.io :refer [as-url input-stream as-file file resource]])
(:import [java.awt Font RenderingHints])
(:gen-class))
(defn register-font [font]
(let [f (Font/createFont Font/TRUETYPE_FONT (input-stream font))]
(.registerFont (java.awt.GraphicsEnvironment/getLocalGraphicsEnvironment) f)
f))
(ns client-test
(:use [lamina.core]
[aleph.tcp]
[aleph.netty.core]
[gloss.core]
[gloss.io])
(:gen-class))
(def chopped-codec
(finite-frame
(defn n-map
"mapping function f on coll xs, using up to n concurrent evaluations.
returns an atom containing unordered results immidietly."
[n f xs]
(let [agents (cycle (map (fn [_] (agent nil)) (range n)))
r (atom [])]
(dorun
(map (fn [a v]
(send-off a
(fn [_ new] (swap! r conj (f new)))
@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)
(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
// 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
(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]
@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"))
(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"))
(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)