Skip to content

Instantly share code, notes, and snippets.

@damionjunk
damionjunk / gist:1396939
Created November 27, 2011 03:57
Clojure bracket balanced? function
(defn bbalanced? [s]
"Is the provided string bracket balanced?"
(zero? (count (reduce (fn [v ch]
(if (= ch \[)
(conj v ch)
(if (= ch \])
(if (= \[ (peek v))
(pop v)
(conj v ch))
v)))
@damionjunk
damionjunk / gist:1397899
Created November 27, 2011 17:53
String balanced reduction.
(defn b-reduction
"Reduces a string to un-balanced left and right enclosing characters."
[s & {:keys [ls rs] :or {ls \[ rs \]}}]
(reduce (fn [v ch]
(if (= ch ls)
(conj v ch)
(if (= ch rs)
(if (= ls (peek v))
(pop v)
(conj v ch))
@damionjunk
damionjunk / gist:1397938
Created November 27, 2011 18:31
Is our string balanced?
(defn b-balanced? [s]
"Is the provided string bracket balanced?"
(empty? (b-reduction s)))))
@damionjunk
damionjunk / gist:1398003
Created November 27, 2011 19:14
Provide the balancing closers.
(defn b-closers
"Provides the 'brackets' necessary to close the given string."
[s & {:keys [ls rs] :or {ls \[ rs \]}}]
(map #(if (= ls %) rs ls) (b-reduction s)))
@damionjunk
damionjunk / gist:1398012
Created November 27, 2011 19:19
Naively balance a string.
(defn str-b-balance
"Naive bracket balance in the provided string."
[s & {:keys [ls rs] :or {ls \[ rs \]}}]
(let [bc (b-closers s)]
(reduce (fn [st ch]
(if (= ls ch) (str ch st) (str st ch)))
s
bc)))
@damionjunk
damionjunk / gist:1468104
Created December 12, 2011 16:31
BPM of Current Metronome in Overtone
(defn cur-bpm
"Returns the BPM of the provided metronome 'm'.
The difference between two consecutive beats divided by 1000 to find the
number of seconds. 60 divided by this number to find the beats per minute."
[m] (Math/round (/ 60 (/ (- (m 2) (m 1)) 1000))))
(def metro (metronome 89))
(cur-bpm metro)
;;=> 89
@damionjunk
damionjunk / gist:1500336
Created December 20, 2011 05:02
Binaural Beats with Overtone and Clojure
(ns overtone-clj-toys.binaural
(:use [overtone.live]))
;;
;; Binaural Beat Synthesis:
;; Generates binaural beats given the provided carrier and desired
;; frequency. Brown noise is used to soften the background and
;; block out outside noise.
;; freq effect
@damionjunk
damionjunk / gist:3087594
Created July 11, 2012 02:37
Data struct manipulations for 'inverting' and 'reverting' maps.
;; for a key, return a list of values
(defn sc-values
"For a given seq of maps, returns all of the values associated with
the given key 'k'. "
[k maps]
(map (fn [x] (k x)) maps))
(defn m-inverto
"invert maps, based on entries of first map, assumes uniform.
(m-inverto [{:a 1 :b 4 :c 3} {:a 2 :b 5} {:a 3 :b 6}])
@damionjunk
damionjunk / gist:3757402
Created September 20, 2012 18:01
Locates a long,lat given a pre-processed shape file
(defn locate-point
"Given a map of shapes see (process-shape-file), locate the entry that
contains the given lat/long, optionally filtered by keys."
([shapes long lat] (locate-point shapes lat long nil))
([shapes long lat keys]
(let [locs (if keys (select-keys shapes keys) shapes)
point (.. JTSFactoryFinder (getGeometryFactory)
(createPoint (Coordinate. long lat)))
flocs (first
(filter (fn [x]
@damionjunk
damionjunk / gist:3757473
Created September 20, 2012 18:12
A more complete example of using GeoTools from clojure.
(ns geoloader.core
(:require [clojure.java.io :as io])
(:import [com.vividsolutions.jts.geom Point Coordinate MultiPolygon]
[org.geotools.data.simple SimpleFeatureCollection SimpleFeatureIterator SimpleFeatureSource]
[org.geotools.data FileDataStore FileDataStoreFinder]
[org.geotools.geometry.jts JTSFactoryFinder]
[org.opengis.feature.simple SimpleFeature SimpleFeatureType]
[org.opengis.feature.type AttributeDescriptor AttributeType]))