Skip to content

Instantly share code, notes, and snippets.

View mjul's full-sized avatar

Martin Jul mjul

View GitHub Profile
(wc -l (grep "foo" (cat "foo.txt")))
(->> (cat "foo.txt") (grep "foo") "wc -l")
@mjul
mjul / core.clj
Created March 28, 2011 14:21
Publish a HTML5 Server-Sent Event to a local Kaazing WebSocket Gateway server via UDP
(ns kaazing-eval.core
(import [java.io File]
[java.net DatagramPacket DatagramSocket InetSocketAddress URI]))
(defn publish [msg]
(let [remote-uri (URI/create "udp://localhost:50505")
remote-addr (InetSocketAddress. (.getHost remote-uri)
(.getPort remote-uri))
socket (DatagramSocket.)]
(let [bytes (.getBytes msg)
@mjul
mjul / KaazingPublishTest.cs
Created March 28, 2011 14:57
Publish a HTML5 Server-Sent Event to a local Kaazing WebSocket Gateway server via UDP from C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Test.Playground
{
[TestClass]
public class KaazingPublishTest
@mjul
mjul / with-timeout.clj
Created May 15, 2011 18:51
with-timeout macro
(defmacro with-timeout [ms & body]
`(let [f# (future ~@body)]
(.get #^java.util.concurrent.Future f# ~ms
java.util.concurrent.TimeUnit/MILLISECONDS)))
@mjul
mjul / incanter.clj
Created October 14, 2011 14:08
Incanter statistical regression test, plot measurements of latency and overlay a linear model for the scalability (latency as function of number of clients) (Clojure Incanter)
(defn regression-nclients-ms [ds]
(with-data ds
(let [x ($ :nclients)
y ($ :ms)
lm (linear-model y x)]
(doto (scatter-plot x y :x-label "Number of clients." :y-label "Latency (ms)")
(add-lines x (:fitted lm))
(set-title "Regression: linear model of number of clients to latency")
view))))
@mjul
mjul / incanter-histogram-with-normal-distribution.clj
Created October 14, 2011 14:45
Plot the distribution of a set of measurements in a histogram and the corresponding normal distribution (Clojure Incanter)
(defn histogram-with-normal-distribution [ds]
"Produce a histgoram of the single-dimensional dataset ds and the corresponding normal distribution."
(with-data ds
(let [x-mean (mean ds)
x-sd (sd ds)
x-min (apply min ds)
x-max (apply max ds)
h (doto (histogram ds :nbins 100 :density true)
(set-stroke-color java.awt.Color/blue)
(set-title "Distribution of latencies")
@mjul
mjul / CustomSerializer.clj
Created October 16, 2011 17:42
Custom Serialization from Clojure for Backtype Storm (Joda DateTime example)
(ns storm-tutorial.CustomSerializer
(:import [backtype.storm StormSubmitter LocalCluster]
[backtype.storm.serialization ISerialization]
[java.io DataInputStream DataOutputStream]
[org.joda.time DateTime])
(:use [backtype.storm clojure config]
[clj-time.core :only (date-time now)]
[clj-time.format :only (formatters parse unparse)])
(:gen-class
:implements [backtype.storm.serialization.ISerialization]))
@mjul
mjul / incanter-charts.clj
Created October 24, 2011 11:40
Clojure pattern for named parameters with defaults
;; From Incanter Charts - neat way to do named parameters with defaults in Clojure:
([chart & options]
(let [{:keys [width dash series dataset]
:or {width 1.0 dash 1.0 series 0 dataset 0}} (apply hash-map options)
@mjul
mjul / scatter-plot.clj
Created October 24, 2011 14:35
Incanter: Customize scatter-plot to have single-pixel points
;; Customize scatter plot to have single-pixel points
(let [sp (scatter-plot (range 10) (range 10))
r (-> sp .getXYPlot .getRenderer)]
(.setSeriesShape r 0 (java.awt.Rectangle. 0 0 1 1))
(view sp))
@mjul
mjul / websocket.clj
Created December 6, 2011 16:12
Clojure JVM Kaazing JMS STOMP websocket client
(ns apiclient.websocket
(:import (java.net URL)
(java.util Properties)
(javax.naming Context InitialContext)
(javax.jms BytesMessage Connection ConnectionFactory Destination
ExceptionListener JMSException
Message MessageConsumer MessageListener MessageProducer
Session TextMessage Topic TopicSubscriber)
(com.kaazing.gateway.jms.client.stomp StompInitialContextFactory
GenericBaseMessage GenericBaseMessageImpl