Skip to content

Instantly share code, notes, and snippets.

Avatar

Dominic Muller domgetter

  • Bakersfield, CA
View GitHub Profile
View socket_client.clj
;; Client
(import 'java.net.Socket)
(def output (.getOutputStream (Socket. "localhost" 3000)))
(.write output 3)
View resource_ala_rails.clj
;; Use defroutes to create a defresource macro that generates restful routes like
;; Rails: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
(def restful-routes
[['GET "" "/index" ]
['GET "/new" "/new" ]
['POST "" "/create" ]
['GET "/:id" "/show" 'id]
['GET "/:id/edit" "/edit" 'id]
['PATCH "/:id" "/update" 'id]
View repeatedly.rb
class Object
def repeatedly
Enumerator.new do |y|
loop { y << yield(self) }
end
end
end
[3,4,5].repeatedly(&:sample).take(10)
#=> [4, 3, 4, 4, 4, 4, 3, 5, 5, 5]
View before.rb
def state
case
when !collected? then 'booked'
when !ironed? then 'collected'
when !delivered? then 'ironed'
else 'delivered'
end
end
View find_with_key.clj
(defn find-with-key
([ds key] (find-with-key (:root ds) key [:root]))
([ds key path]
(if (= key (:key (:data ds)))
path
(if (empty? (:children ds)) nil
(first (filter identity (for [[index child] (map-indexed list (:children ds))]
(find-with-key ((:children ds) index) key (conj path :children index)))))))))
(def my-map {:key nil
View partition_while.clj
(defn partition-while
"partitions elements in order while the predicate function holds
the predicate function is given the current element
and the current partition. "
[f coll]
(loop [truck [] box [] [book & books] coll]
(if (empty? books) ; if there are no more books to pack
(conj truck box) ; tape up the last box, put it in the truck, and return the truck
(if (f book box) ; otherwise, if we can put the current book in the pending box
(recur truck (conj box book) books) ; put the book in the box and work on the next book
View each_cons.rb
[*"a".."h"].each_cons(2) {|(last, current)| puts "last: #{last}, current: #{current}" }
# last: a, current: b
# last: b, current: c
# last: c, current: d
# last: d, current: e
# last: e, current: f
# last: f, current: g
# last: g, current: h
View rolling_variance.clj
(defn rolling-variance
([] [0.0 0.0 0])
([[m v c] val]
(let [mean (+ m (/ (- val m) (inc c)))
var (+ v (* (- val m) (- val mean)))]
[mean var (inc c)])))
(defn standard-deviation [coll]
(let [[_ variance count] (reduce rolling-variance (rolling-variance) coll)]
(Math/sqrt (/ variance count))))
View excpetions.rb
begin # start catching exceptions here
ldap = Net::LDAP.open(
:host => host,
:port => port,
:auth => {
:method => :simple,
:username => CONFIG['repldn'],
:password => CONFIG['replpw']
}
)
View bottomfeeder.rb
module Auspice
class BottomFeeder
attr_accessor :x, :y, :z, :in, :out
def initialize(size,w,h)
@font = Gosu::Font.new(size,:name => DAT+'ttfs/ATComputer.ttf')
@rows = ((h / size)+size)
@cols = (w / @font.text_width('_',1))
@size = size
@dtext = ['']