Skip to content

Instantly share code, notes, and snippets.

View lildata's full-sized avatar

Programming is fun lildata

View GitHub Profile
@scramjet
scramjet / bench-list.comprehension.clj
Created September 3, 2015 02:26
Bench testing Clojure list comprehensions against map
user> (use 'criterium.core)
nil
user> (def sites (vec (for [i (range 1000)] {:stations (vec (for [j (range 10)] [{:id (str i "-" j)}]))})))
#'user/sites
user> (bench (doall (mapcat (fn [site] (map :id (:stations site))) sites)))
Evaluation count : 74280 in 60 samples of 1238 calls.
Execution time mean : 802.413222 µs
Execution time std-deviation : 10.087328 µs
Execution time lower quantile : 786.940250 µs ( 2.5%)
Execution time upper quantile : 823.245990 µs (97.5%)
@kawasima
kawasima / pdf2svg.clj
Created March 25, 2014 09:58
MS Excel to SVG by Clojure
(ns pdf2svg
(:require [clojure.java.io :as io])
(:import [org.apache.pdfbox.pdmodel PDDocument PDPageable]
[org.apache.batik.dom GenericDOMImplementation]
[org.apache.batik.svggen SVGGeneratorContext SVGGraphics2D]))
(let [document (PDDocument/load "hoge.pdf")
pageable (PDPageable. document)
dom-impl (GenericDOMImplementation/getDOMImplementation)
svg-document (.createDocument dom-impl "http://www.w3.org/2000/svg" "svg" nil)
@aldonline
aldonline / gist:6959105
Created October 13, 2013 07:21
SQL string to Scala Stream[ResultSet] and common Loan pattern usages when dealing with SQL connections
object x {
// stream a sql query
def sql( str:String ):Stream[ResultSet] = withStatement { s =>
val rs = s executeQuery str
new Iterator[ResultSet] { def hasNext = rs.next ; def next = rs }.toStream
}
// loan a sql statement
(require '[clojure.core.async :as async :refer :all])
(defn fake-search [kind]
(fn [query]
(Thread/sleep (int (* (java.lang.Math/random) 1000)))
(str kind " result for " query)))
(def web (fake-search "Web"))
(def image (fake-search "Image"))
(def video (fake-search "Video"))
@gerritjvv
gerritjvv / gist:5866665
Last active July 19, 2024 13:29
Calling an external process from Clojure
(comment
This is the easiest and most concise way of calling an external process in Java. The inheritIO methods causes the command to output stdout and errout to the same place as your current process (which most of the times is the console), no need to create mechanisms for reading asynchronously from input streams to get at the information.
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
)
(def ret (.waitFor (-> (ProcessBuilder. ["gzip" "-t" "g.txt.gz"]) .inheritIO .start)))
@weissjeffm
weissjeffm / jgraph.clj
Created February 22, 2013 17:06
repl session so i can remember how this works
; 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]
@daveray
daveray / busy-cursor.clj
Created August 31, 2012 01:56
Clojure/Seesaw busy cursor example
(ns busy-cursor.core
(:use seesaw.core))
(defn long-running-task
[]
(Thread/sleep 5000)
"The result")
(defn run-task-with-busy-cursor
[c]
@bayan
bayan / gist:3382884
Created August 17, 2012 21:27
Clojure DSL to generate static HTML
;; Generating static HTML using Clojure macros
;; It is possible to write a DSL in Clojure that generates HTML markup without the need to write a separate parser and compiler (e.g. HAML).
;; Our aim here would be to write code that converts the following Clojure code into the HTML below it
;; (html
;; (head)
;; (body
;; (h1 "An example")
@daveray
daveray / table-test.clj
Created April 5, 2012 12:44
Highlighting table rows with Seesaw
(ns table-test.core
(:use [seesaw core table swingx]))
; A predicate that decides whether a row should be highlighted
; adapter is an instance of JXTable.TableAdapter
; http://projects.joshy.org/projects/painterreview/swingx/org/jdesktop/swingx/JXTable.TableAdapter.html
(defn hl-predicate [renderer adapter]
; Highligh all rows where :age is over thirty
(> (.getValueAt adapter (.row adapter) 0) 30))
@btoone
btoone / curl.md
Last active October 10, 2025 20:21
A curl tutorial using GitHub's API

Introduction

An introduction to curl using GitHub's API.

The Basics

Makes a basic GET request to the specifed URI

curl https://api.github.com/users/caspyin