Skip to content

Instantly share code, notes, and snippets.

@carlohamalainen
carlohamalainen / shrink_pdf.sh
Created November 8, 2011 00:46
Shrink a PDF file by transferring to DJVU, then PS, then back to PDF.
#!/bin/bash
set -o nounset # explode on undefined variables
set -e # explode if any command fails
# Best way to shrink a PDF of scanned pages without losing quality. Found on
# http://ubuntuforums.org/archive/index.php/t-1133357.html
# example: ./shrink_pdf.sh big_file.pdf 600 small_file.pdf
(ns blerp
(:require [clojure.xml :as xml])
(import (java.io ByteArrayInputStream)))
(def xml-data "<body> <myheader> <author> bob </author> </myheader> <data> d1 </data> <data>d2</data> <data>d3</data> </body>")
(def xml-parsed (let [input-stream (ByteArrayInputStream. (.getBytes xml-data))]
(xml-seq (xml/parse input-stream))))
(let [name-blob (for [x xml-parsed
(defun blerp (x y)
"A closure that provides a linear interpolation of a function y = f(x). Attempts to evaluate f
at values of x below the minimum value of x result in zero, and attempts above the maximum
value of x result in the last value of y."
(assert (vectorp x))
(assert (vectorp y))
(let ((min-x (aref x 0))
(max-x (aref x (1- (length x))))
(interp-f (cl-numlib:make-interpolating-function x y)))
(lambda (x)
ncInq :: NCID -> IO (Int, Int, Int, Int)
ncInq ncid = do
alloca $ \ndims_ptr -> do
alloca $ \nvars_ptr -> do
alloca $ \natts_ptr -> do
alloca $ \unlimdimid_ptr -> do
status <- nc_inq ncid ndims_ptr nvars_ptr natts_ptr unlimdimid_ptr
ndims <- peek ndims_ptr
nvars <- peek nvars_ptr
natts <- peek natts_ptr
(ns testing-netcdf
(:import ucar.ma2.Array)
(:import ucar.ma2.ArrayDouble)
(:import ucar.ma2.ArrayDouble$D2))
(def a (Array/factory Double/TYPE (int-array [2 2])))
(println (type a)) ; ucar.ma2.ArrayDouble$D2
@carlohamalainen
carlohamalainen / gist:1124157
Created August 3, 2011 23:38
Run ggplot2 from Clojure using rincanter
; http://carlo-hamalainen.net/blog/2011/08/04/ggplot2-from-clojure
; To dump the plot to a file:
(use '(com.evocomputing rincanter)) ; https://github.com/jolby/rincanter
(r-eval "library(ggplot2)")
(r-eval-raw "qplot(rating, data=movies, geom=\"histogram\")") ; see http://had.co.nz/ggplot2/geom_histogram.html
(r-eval "ggsave('histogram-example.png')")
; To display on your screen (Unix example; see rincanter docs for alternatives to x11() call)
(use '(com.evocomputing rincanter))
@carlohamalainen
carlohamalainen / gist:1119348
Created August 2, 2011 00:40
weekdays in a range
(ns date-example
(:import java.util.Date)
(:import java.text.DateFormat))
(def df (DateFormat/getDateInstance))
; Simon's bit
(defn weekdays-in-range [start-date end-date]
(let [one-day (* 1000 60 60 24) ; millisecs