Skip to content

Instantly share code, notes, and snippets.

View c-garcia's full-sized avatar

cristobal garcia c-garcia

View GitHub Profile
@c-garcia
c-garcia / rsync-pass.tcl
Created September 19, 2015 05:57
rsync a path when it is not possible use ssh key pairs (warning, warning, warning)
#!/usr/bin/expect
# Example script for rsync-ing a path when
# it is not possible to use ssh keys.
# This is not good practice. Adding a password
# to a file never is. Even less when it is used
# to bypass some security mechanisms implemented by
# ssh.
# It should be used only in very specific situations.
# It is left here for reference.
@c-garcia
c-garcia / partition.py
Created September 18, 2015 07:38
Python equivalent to clojure partition (simple case)
def partition(seq, num):
"Partititons a seq en disjoint seqs of up to num elems"
return [seq[start:start+count] \
for (start, count) in \
[(num * x,y) for (x,y) in \
zip(range(len(seq)/num), [num]*(num*(len(seq)/num)))] +
[(num * (len(seq)/num), len(seq) % num)]
if count >0]
@c-garcia
c-garcia / pipe.clj
Last active August 29, 2015 14:22
Macro to pipe functions through core.async channels
(defmacro validate-args
[exp msg]
`(when-not ~exp
(throw (IllegalArgumentException. ~msg))))
(defmacro chan-pipe
;; first and last body functions will be prepended an argument (the out and in channel)
;; internal body functions will be prepended two arguments in and out channels
[bindings & body]
(validate-args (vector? bindings) "bindings should be a vector")
@c-garcia
c-garcia / register_font.clj
Created April 12, 2015 10:06
Register a TrueType font from a file
;; From the REPL
(import '(java.awt GraphicsEnvironment Font) java.io.File)
(.. (GraphicsEnvironment/getLocalGraphicsEnvironment)
(registerFont (Font/createFont Font/TRUETYPE_FONT
(File. "font.ttf"))))
@c-garcia
c-garcia / print_fonts.clj
Created April 12, 2015 09:58
Prints available font family names
; from the REPL
(import java.awt.GraphicsEnvironment)
(doseq [fnt (.. (GraphicsEnvironment/getLocalGraphicsEnvironment) getAvailableFontFamilyNames)]
(println fnt))
@c-garcia
c-garcia / Sink.clj
Created April 8, 2015 20:59
Use gen-class to create a flume Sink implementation. The process method is not fully finished since the purpose of the gist is to show how to gen-class enables us to do this.
(ns minisink.Sink
(:import
minisink.Sink
(org.apache.flume Context Sink$Status))
(:gen-class
:extends org.apache.flume.sink.AbstractSink
:implements [org.apache.flume.conf.Configurable]
;; state an object will have. Use atoms, refs or agents
;; to introduce mutability. It will be exposed as a method.
:state state
@c-garcia
c-garcia / histog.clj
Created April 2, 2015 07:33
make an text histogram from a vector of integers
;; From the REPL
(require '[clojure.math.numeric.tower :as math])
(defn histo
[v & {:keys [size] :or {size 40}}]
(letfn [(digits-for-num [x]
(inc
(int (math/floor (/ (Math/log x) (Math/log 10))))))]
(let [mm (apply max v)
digits-x (digits-for-num (count v))
@c-garcia
c-garcia / tick-while.clj
Last active August 29, 2015 14:17
Repeatedly execute a function with side effects with certain period while a reference evaluates to true
;; Based on
;; http://bjeanes.com/2012/09/call-clojure-function-on-a-timer
;; Example:
;; (def flag (atom true))
;; (tick-while flag 1000 (fn [s] (println (str s ":" (System/currentTimeMillis)))) "Time")
;; wait for some time and
;; (swap! flag (fn [_] false))
(defn tick-while
"Executes the function f (side-effects) with args every t ms until p-ref de-references to false."
@c-garcia
c-garcia / openvpn.service
Created March 15, 2015 18:55
Start an OpenVPN Container as a systemd managed service under CoreOS
[Unit]
Description=OpenVPN
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Environment="OVPN_DATA=ovpn-data"
Environment="OVPN_IMAGE=kylemanna/openvpn"
Environment="OVPN_CONTAINER=openvpn_server"
@c-garcia
c-garcia / repl-session.clj
Last active August 29, 2015 14:07
Mars-rover kata usage from the REPL
(require
'[marsrover.grid :as grid]
'[marsrover.robot :as robot]
'[marsrover.core :refer :all])
;; 10x10 grid with obstacle at 1,1. Robot moves beside it.
(let [grid (grid/make-grid [10 10] [[1 1]])
robot (robot/make-robot [0 0] \n grid)]
(tell-robot robot "fffrf"))
;;{:robot {:pos [1 3], :dir \e, :grid {:dim [10 10], :obstacles {[1 1] true}}}, :status :ok}