Tuesday, October 27, 2009

Clojure Syntax

Not many languages are simple enough to have their entire syntax conveniently summarized in a table that fits on one screen. Luckily, Clojure is. The following is a summary put together based on the Clojure reader documentation. I'm a fan of tabular data, so I put this together for my own personal reference.

Reader forms
Symbolsatom, foo-bar, *foo*, etc.
Literals42, "foo", nil, true, false, \c, :foo
Keywords:foo (like symbols, prefixed with colon)
Lists(a b c)
Vectors[a b c]
Maps (hashes){:a 1 :b 2} or {:a 1, :b 2}
Sets#{:a :b :c}
Macro Characters
Character\a \b \c
Comment; txt
Meta^a ^b ^c
Syntax-quote` (backtick)
Unquote~ (tilde)
Unquote-splicing~@ (tilde at)
Macro Characters (Dispatch)
Sets#{:a :b :c}
Metadata#^{:a 1 :b 2} [1 2 3] or #^Type x
Anonymous function#(fn [args] (...))
Ignore next form#_

Thursday, October 15, 2009

Crushing PNGs With Ruby

Nothing too special here... just a quick hack. I needed to recursively run pngcrush over a directory structure, and I couldn't find any examples of how to do this online. The following solves the problem nicely. Hopefully someone else finds this helpful.

require 'fileutils'
require 'find'

input_dir = ARGV[0]
rewrite_dir = "/tmp/crush/"

raise("Usage #{$0} dirname") unless input_dir
Dir.mkdir(rewrite_dir) unless test(?d, rewrite_dir)

Find.find(input_dir) do |file|
  if file =~ /\.png$/
    base_dir = File.dirname(file)
    base_file = File.basename(file)
    new_dir = rewrite_dir + base_dir
    FileUtils.mkdir_p(new_dir) unless test(?d, new_dir)
    puts(%Q!pngcrush -reduce -brute "#{file}" "#{new_dir}/#{base_file}"!)
From there, you can just do something like: crush.rb images | sh.