Skip to content

Instantly share code, notes, and snippets.

View latompa's full-sized avatar

Thomas Olausson latompa

View GitHub Profile
@latompa
latompa / gist:c4d67fc89616798f7d8f
Created February 24, 2015 15:54
Hashie::Mash vs OpenStruct
(Hashie::Mash.new :key => 5).key
ArgumentError: wrong number of arguments (0 for 1)
from (irb):1:in `key'
from (irb):1
@latompa
latompa / init.el
Created February 13, 2015 16:09
~/.emacs.d/init.el
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "https://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
(defvar my-packages '(clojure-mode
@latompa
latompa / commas.clj
Last active August 29, 2015 14:02
add commas
(defn commas-foul [n]
(->> n
.toString
reverse
(partition-all 3)
(interpose ",")
flatten
reverse
(apply str)))
@latompa
latompa / barbell.clj
Last active August 29, 2015 14:02
load a barbell challenge
;; With these plates
(def available-plates [45 25 15 10 5 2.5 1.25])
;; Create a function that loads a barbell (weighs 45).
;; With the available plates as close to the goal weight as possible.
;; Assume you have unlimited supply of each plate.
;; Weight has to be evenly distributed on each side.
;; Use as few plates as possible.
;; Example: 45 pound bar bell with 152.5 pounds goal weight
@latompa
latompa / bookmarklet.js
Last active August 29, 2015 13:55
bookmarklet
javascript:(function() {
var puppyBasket = {
square: [
"http://fc04.deviantart.net/fs48/i/2012/019/e/b/husky_puppy_2151_by_sooper_husky-d28f7yz.jpg",
"http://www.rollingmeadowspuppies.com/images/Rose_poochon_male_Rm2_3_2.JPG",
"http://us.123rf.com/400wm/400/400/isselee/isselee0803/isselee080300569/2776137-yorkshire-terrier-puppies-1-month-in-front-of-a-white-background.jpg",
"http://media.bureauoftrade.com/p/2013/09/29/apollo-labrador-retriever-puppy-400c.jpg",
"http://helenwoodwardanimalcenter.files.wordpress.com/2012/04/snowcone-headshot.jpg%3Fw%3D400",
"http://imagecache6.allposters.com/LRG/61/6167/9LTG100Z.jpg",
"http://thumbs.dreamstime.com/x/german-shepherd-puppies-448837.jpg",
; given {1 [:a :b :c] 2 [:b :c :d]}
; flip the keys like so
; {:a [1] :b [1 2] :c [1 2] :d [2]
(defn explode-keys-to-map
"take [a b c] 1, return {a [1] b [1] c [1]}"
[m ks v]
(let [add-val-to-seq (fn [m k v]
(if (m k)
(assoc m k (conj (m k) v))
; solution to http://www.4clojure.com/problem/177
(defn balanced?
[x]
(let [tokens (vec (clojure.string/replace x #"[^\(\)\[\]\{\}]" ""))
brackets { \{ \} \( \) \[ \]}
starting-bracket? (fn [x] (brackets x))
matching-bracket? (fn [a b] (= (brackets a) b))]
(loop [stack '()
[curr & more] tokens]
@latompa
latompa / expiring_sorted_set.rb
Last active December 10, 2015 21:48
Individual set member expiry, inspired by https://github.com/antirez/redis/issues/135
require 'redis'
#
# The drawback with this approach is old members are deleted as you add new, creates a bit of overhead.
# The whole set expires, using normal redis expiration, if you don't add anything until the last member was added
#
class ExpiringSortedSet
attr_reader :key
def initialize(args)
@latompa
latompa / dict.clj
Created September 20, 2012 19:55
simple dictionary without using hashmap
(defn new-dict
[x]
'nil)
(defn add-to-dict
[dict key val]
#(if (= key %) val (dict %)))
(def mydict (add-to-dict (add-to-dict new-dict "b" 42) "a" 10))
@latompa
latompa / dict.rb
Created December 17, 2011 01:03
simple dictionary without using arrays or hashes
def dictionary
lambda {|x| nil }
end
def add(key, value, dictionary)
lambda do |x|
key == x ? value : dictionary.call(x)
end
end