Skip to content

Instantly share code, notes, and snippets.

View lildata's full-sized avatar

Programming is fun lildata

View GitHub Profile
@lildata
lildata / SimpleStopwatch.scala
Created June 19, 2015 12:59
A very simple stopwatch
object Stopwatch {
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0) + "ns")
result
}
}
@lildata
lildata / jgraph.clj
Last active August 29, 2015 14:23 — forked from weissjeffm/jgraph.clj
; nREPL 0.1.7-preview
user> (use 'seesaw.core)
nil
user> (def myframe (frame :title "Hello" :on-close :exit))
#'user/myframe
user> (import com.mxgraph.view.mxGraph)
com.mxgraph.view.mxGraph
user> (def mygraph (mxGraph.))
#'user/mygraph
user> (defn display [content]
@lildata
lildata / Change java.library.path at Runtime.scala
Created June 24, 2015 15:45
You usually can't change java.library.path at runtime since it seems to be evaluated at the start and then cached. But with this little trick it is being reevaluated.
System.setProperty("java.library.path", newpath)
val fieldSysPath = classOf[ClassLoader].getDeclaredField("sys_paths")
fieldSysPath.setAccessible(true)
fieldSysPath.set(null, null)
object CSV extends RegexParsers {
override val skipWhitespace = false // meaningful spaces in CSV
def COMMA = ","
def DQUOTE = "\""
def DQUOTE2 = "\"\"" ^^ { case _ => "\"" } // combine 2 dquotes into 1
def CRLF = "\r\n" | "\n"
def TXT = "[^\",\r\n]".r
def SPACES = "[ \t]+".r
;; $ lein try ring/ring-core 1.4.0 ring/ring-jetty-adapter 1.4.0
user=> (use 'ring.adapter.jetty)
user=> (defn handler [request]
#_=> {:status 200
#_=> :headers {"Content-Type" "text/plain"}
#_=> :body "Hello [Clojure Ring Jetty] !"})
#'user/handler
user=> (def g1 (weighted-digraph [:usr.1 :bro.1 1] [:usr.1 :itr.1 1] [:bro.1 :itr.1 0.5] [:itr.1 :prm.1 1] [:itr.1 :prm.2 1] [:usr.1 :bro.2 1] [:bro.2 :itr.2 1] [:itr.2 :prm.2 1]))
#'user/g1
user=> (shortest-path g1 :usr:1 :prm:2)
nil
user=> (shortest-path g1 :usr.1 :prm.2)
(:usr.1 :itr.1 :prm.2)
user=> (bellman-ford g1 :usr.1)
[{:itr.2 2, :itr.1 1, :bro.1 1, :prm.2 2, :bro.2 1, :prm.1 2, :usr.1 0} {:usr.1 (:usr.1), :prm.1 (:usr.1 :itr.1 :prm.1), :bro.2 (:usr.1 :bro.2), :prm.2 (:usr.1 :itr.1 :prm.2), :bro.1 (:usr.1 :bro.1), :itr.1 (:usr.1 :itr.1), :itr.2 (:usr.1 :bro.2 :itr.2)}]
; Copyright (c) Dave Ray, 2011. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
( defn tableify [ row ] ( apply format "%-20s | %-20s | %-20s" row ))