четверг, 16 марта 2017 г.

clojure queue FIFO

If you need simple FIFO queue you can use clojure.lang.PersistentQueue/EMPTY function with some wrappers on it.

;; ------------------------------------------------------------------
  (defn new-queue []
    (atom clojure.lang.PersistentQueue/EMPTY))

  (defn push-queue [queue value]
    (swap! queue conj value))

  (defn pop-queue [queue]
    (let [value (peek @queue)]
      (swap! queue pop)
      value))
;; ------------------------------------------------------------------

Now let's test it

  (def a (new-queue))
;; => nil

(push-queue a 1)
;; => #object...

(push-queue a 2)
;; => #object...

(push-queue a 3)
;; => #object...

(push-queue a "hello")
;; => #object...

Let's check a content of queue a:

(seq @a)
;; => (1 2 3 "hello")

Now time to take first item:

(pop-queue a)
;; => 1

(seq @a)
;; => (2 3 "hello")

понедельник, 13 марта 2017 г.

emacs interactive package install

(use-package company
:ensure t
:bind (("C-c /". company-complete))
:config
(global-company-mode))

четверг, 9 марта 2017 г.

воскресенье, 26 февраля 2017 г.

clojure reducers are fast

(def v1 (for [x (range 10000000)]
          (rand-int 10000000)))

(count v1)

(require '[clojure.core.reducers])
(defn x-in-coll? [x coll]
  (clojure.core.reducers/reduce (fn [res next-v] (if (= next-v x) (reduced true) false)) false coll))

(defn x-in-coll2? [x coll]
  (some #(= x %) coll))

(time (x-in-coll? 0 v1))
(time (x-in-coll2? 0 v1))

(time (x-in-coll? 0 v1))
"Elapsed time: 176.250871 msecs"
=> false
(time (x-in-coll2? 0 v1))
"Elapsed time: 559.220763 msecs"
=> nil

четверг, 23 февраля 2017 г.

netstat analog for mac os

lsof -iTCP -sTCP:LISTEN

lsof -iUDP

lsof -i4 -sTCP:LISTEN

lsof -i6 -sTCP:LISTEN


вторник, 21 февраля 2017 г.

clojure.spec for any namespace on the fly

(create-ns 'com.mycompany)
(alias 'a 'com.mycompany)
(s/def ::a/keyword keyword?)
(s/exercise ::a/keyword)