Skip to content

Instantly share code, notes, and snippets.

@svanellewee
Created July 18, 2014 06:27
Show Gist options
  • Save svanellewee/c1f056800a16e189e63c to your computer and use it in GitHub Desktop.
Save svanellewee/c1f056800a16e189e63c to your computer and use it in GitHub Desktop.
SO Clojure Increment Counter
;;For http://stackoverflow.com/questions/24798831/clojure-increment-a-counter
(ns hack1
;; (:use clojure.contrib.command-line)
)
(def collection-x ['({:customer_id "111", :product_id "222"})
'({:customer_id "333", :product_id "444"}{:customer_id "555", :product_id "666"}{:customer_id "777", :product_id "888"})] )
(defn number-in-list [cur-collection]
(loop [ [an-item & rest-of-collection] cur-collection
idx 1
result []]
(if (nil? an-item)
;;(println "done",result)
result
(let [new-elem (map (fn [e]
{ :customer_id (:customer_id e)
:number idx }) an-item) ]
(recur rest-of-collection (inc idx) (conj result new-elem))
)
)
)
)
;; (defn number-item
;; ([cur-item] (number-item cur-item 1))
;; ([cur-item idx]
;; (let [[elem & the-rest] cur-item ]
;; (cons
;; (when-not (nil? elem) { :product_id (:product_id elem) :number idx })
;; (lazy-seq (number-item the-rest (inc idx))))
;; )
;; )
;; )
(defn number-item
([cur-item] (number-item cur-item 1))
([cur-item idx]
(let [[elem & the-rest] cur-item
{:keys [product_id ] } elem ]
(cons
(when-not (nil? elem) { :product_id product_id :number idx })
(lazy-seq (number-item the-rest (inc idx))))
)
)
)
(def not-nil? (fn [ x] (not (nil? x))) )
(def not-empty? (fn [ x] (not (empty? x))) )
(defn also-number-in-list
([cur-collection ]
(let [[an-item & rest-of-collection] cur-collection ]
(cons
(take-while not-nil? (number-item an-item))
(lazy-seq (also-number-in-list rest-of-collection )) )
) )
)
;; recur, not just for loops !
(defn list-me
" I do the following:
BLA
>> three
BLA
>> two
BLA
>> one
BLA
>> i'm
BLA
>> the
BLA
>> bomb
BLA
user>
"
([ elems ]
(println "BLA")
(let [[ first-elem & the-rest ] elems ]
(when-not (nil? first-elem)
(println ">>" first-elem)
(recur the-rest)))))
;; BLA
;; >> three
;; BLA
;; >> two
;; BLA
;; >> one
;; BLA
;; >> i'm
;; BLA
;; >> the
;; BLA
;; >> bomb
;; BLA
;;
;; user>
(list-me '("three", "two", "one", "i'm" , "the", "bomb")) ;; ah-ha-haaaa!
(comment
(hack1/number-in-list hack1/collection-x)
(println "-->>" (take-while not-empty? (also-number-in-list collection-x)))
)
;; (defn fib
;; ([] (fib 1 1))
;; ([ a b ] (cons a (lazy-seq (fib b (+ b a))))))
;; (take 90 (fib))
;; (defn fib [a b] (cons a (lazy-seq (fib b (+ b a)))))
;; (def collection-x [{:customer_id "111", :product_id "222"} ] )
;; (defn -main[& args]
;; (loop [ collection-1 collection-x result [] count 1]
;; (let [ [cur-value & the-rest] (first collection-1) ]
;; (println cur-value ">>" the-rest)
;; (when-not (empty? collection-1)
;; (conj result cur-value)
;; (recur (rest collection-1) result (inc count) ))
;; ))
;; )
;; (defn -main []
;; (loop [ [ first-elem & the-rest ] collection-x ]
;; (println ">>>>>>first" first-elem, "and the rest", the-rest)
;; (when-not (empty? the-rest)
;; (recur the-rest) ))
;; )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment