Skip to content

Instantly share code, notes, and snippets.

@potix2
Created March 17, 2013 09:28
Show Gist options
  • Save potix2/5180803 to your computer and use it in GitHub Desktop.
Save potix2/5180803 to your computer and use it in GitHub Desktop.
example of carmine
(ns carmine-test.core
(:require [taoensso.carmine :as car])
(:require [taoensso.carmine.locks :as carl]))
(def pool (car/make-conn-pool))
(def spec-server1 (car/make-conn-spec))
(defmacro wcar [& body] `(car/with-conn pool spec-server1 ~@body))
(wcar
(car/ping)
(car/set "foo" "bar")
(car/get "foo"))
(wcar
(car/set "clj-key" {:bigint (bigint 12345678901234567890)
:vec (vec (range 5))
:set #{true false :a :b :c :d}
:bytes (byte-array 5)})
(car/get "clj-key"))
(use 'clojure.repl)
(wcar (doall (repeatedly 5 car/ping)))
(let [first-names ["Selvatore" "Rich"]
surnames ["Sanfilippo" "Hickey"]]
(wcar (doall (map #(car/set %1 %2) first-names surnames))
(doall (map car/get first-names))))
; (def listener
; (car/with-new-pubsub-listener
; spec-server1 {"foobar" (fn f1 [msg] (println "Channel match: " msg))
; "foo*" (fn f2 [msg] (println "Pattern match: " msg))}
; (car/subscribe "foobar" "foobaz")
; (car/psubscribe "foo*")))
;
; (wcar (car/publish "foobar" "Hello to foobar!"))
;
; (car/with-open-listener listener
; (car/unsubscribe)
; (car/psubscribe "an-extra-channel"))
;(doc taoensso.carmine.locks)
(when-let [uuid (carl/acquire-lock "my-lock" 2000 500)]
[(Thread/sleep 100)
(carl/have-lock? "my-lock" uuid)
(Thread/sleep 2000)
(carl/have-lock? "my-lock" uuid)])
(wcar (car/keys "*"))
(def item-data
["map" 9 150
"compass" 13 35
"water" 153 200
"sandwich" 50 160
"glucose" 15 60
"tin" 68 45
"banana" 27 60
"apple" 39 40
"cheese" 23 30
"beer" 52 10
"suntan cream" 11 70
"camera" 32 30
"t-shirt" 24 15
"trousers" 48 10
"umbrella" 73 40
"waterproof trousers" 42 70
"waterproof overclothes" 43 75
"note-case" 22 80
"sunglasses" 7 20
"towel" 18 12
"socks" 4 50
"book" 30 10])
(defstruct item :name :weight :value)
(def items (vec (map #(apply struct item %) (partition 3 item-data))))
(partition 2 (range 10))
(declare mm)
(defn m [i w]
(cond
(< i 0) [0 []]
(= w 0) [0 []]
:else
(let [{wi :weight vi :value} (get items i)]
(if (> wi w)
(mm (dec i) w)
(let [[vn sn :as no] (mm (dec i) w)
[vy sy :as yes] (mm (dec i) (- w wi))]
(if (> (+ vy vi) vn)
[(+ vy vi) (conj sy i)]
no))))))
(def mm (memoize m))
(use '[clojure.string :only [join]])
(let [[value indexes] (m (-> items count dec) 400)
names (map (comp :name items) indexes)]
(println "items to pack:" (join ", " names))
(println "total value:" value)
(println "total weight:" (reduce + (map (comp :weight items) indexes))))
(let [{x :baz y :bar} {:foo 1 :bar 2 :baz 3}]
(println x)
(println y))
;(def napsac-data [[2,3], [1,2], [3,4], [2,2]])
; (defn napsac-rec [data rest-weight total-value]
; (if
; (empty? data) total-value
; (let
; [w (first (first data))
; v (first (rest (first data)))]
; (if
; (< rest-weight w) (napsac-rec
; (rest data)
; rest-weight
; total-value)
; (max
; (napsac-rec
; (rest data)
; rest-weight
; total-value)
; (napsac-rec
; (rest data)
; (- rest-weight w)
; (+ total-value v)))))))
;
; (napsac-rec napsac-data 5 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment