Skip to content

Instantly share code, notes, and snippets.

@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
@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))
(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
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
(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)
(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
@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
@carlohamalainen
carlohamalainen / tunnel.py
Created September 29, 2012 12:06
check ssh tunnel from Python
"""
From the man page for ssh:
-f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask
for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way
to start X11 programs at a remote site is with something like ssh -f host xterm.
If the ExitOnForwardFailure configuration option is set to “yes”, then a client started with -f will wait
for all remote port forwards to be successfully established before placing itself in the background.
@carlohamalainen
carlohamalainen / gist:4128947
Created November 22, 2012 01:37
Fiddling with STM and processes
-- Fiddling around with STM and shell processes.
-- Carlo Hamalainen <carlo.hamalainen@gmail.com>
import Control.Concurrent
import Control.Concurrent.MVar
import Control.Concurrent.STM
import Control.Concurrent.STM.TChan
import Control.Exception
@carlohamalainen
carlohamalainen / install_wxWidgets_2.9.4.sh
Created November 24, 2012 07:21
Install wxWidgets 2.9.4 on Debian Squeeze
#!/bin/bash
set -x
set -e
module load python/2.7.3
mkdir -p /opt/src
cd /opt/src