воскресенье, 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)

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

java Date to LocalDate

(defn inst2local-date
  "convert java.util.Date to java.time.LocalDate"  [^Date date]
  (-> date .toInstant (.atZone (ZoneId/systemDefault)) .toLocalDate))

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

Joda to LocalDate in Clojure

(def joda-local-date (t/local-date-time year month day))
(-> joda-local-date tc/to-long Instant/ofEpochMilli (.atZone (ZoneId/systemDefault)) .toLocalDate)