Refactor generic functions to separate utils namespace, finaly bump version
This commit is contained in:
parent
5551be7012
commit
43a9781893
3 changed files with 46 additions and 8 deletions
|
@ -1,4 +1,4 @@
|
|||
(defproject rss-thread-watch "0.1.0-SNAPSHOT"
|
||||
(defproject rss-thread-watch "0.3.0-SNAPSHOT"
|
||||
:description "RSS based thread watcher"
|
||||
:url "http://example.com/FIXME"
|
||||
:license {:name "AGPL-3.0-only"
|
||||
|
|
|
@ -18,15 +18,12 @@
|
|||
[ring.util.response :as response]
|
||||
[clj-rss.core :as rss]
|
||||
[clojure.string :as s]
|
||||
[rss-thread-watch.watcher :as watcher])
|
||||
[rss-thread-watch.watcher :as watcher]
|
||||
[rss-thread-watch.utils :as ut])
|
||||
(:gen-class))
|
||||
|
||||
|
||||
(defn indices
|
||||
;; https://stackoverflow.com/questions/8641305/find-index-of-an-element-matching-a-predicate-in-clojure
|
||||
"Returns indexes of elements passing predicate"
|
||||
[pred coll]
|
||||
(keep-indexed #(when (pred %2) %1) coll))
|
||||
|
||||
|
||||
(defn new-guid-always
|
||||
"Generates always unique GUID for Feed item.
|
||||
|
@ -56,7 +53,7 @@
|
|||
(let [time-of-generation @watcher/time-of-cache
|
||||
guid-fn (if repeat? (fn [x] (new-guid-always x time-of-generation))
|
||||
update-only-guid)
|
||||
cache-start-index (first (indices (fn [x] (>= (:chod x) chod-treshold))
|
||||
cache-start-index (first (ut/indices (fn [x] (>= (:chod x) chod-treshold))
|
||||
cache))
|
||||
;; So we don't have to search thru everything we have cached
|
||||
needed-cache-part (subvec cache cache-start-index)
|
||||
|
|
41
src/rss_thread_watch/utils.clj
Normal file
41
src/rss_thread_watch/utils.clj
Normal file
|
@ -0,0 +1,41 @@
|
|||
(ns rss-thread-watch.utils
|
||||
"Util functions"
|
||||
(:gen-class))
|
||||
;; ===== Generic functions ====
|
||||
|
||||
(defn indices
|
||||
;; https://stackoverflow.com/questions/8641305/find-index-of-an-element-matching-a-predicate-in-clojure
|
||||
"Returns indexes of elements passing predicate"
|
||||
[pred coll]
|
||||
(keep-indexed #(when (pred %2) %1) coll))
|
||||
|
||||
;; ===== Macros =====
|
||||
(defmacro nil?-else
|
||||
"Return x unless it's nil, the return y"
|
||||
[x y]
|
||||
`(let [result# ~x]
|
||||
(if (nil? result#)
|
||||
~y
|
||||
result#)))
|
||||
|
||||
(defmacro when-else
|
||||
"Evaluates tst, if it's true returns it's result.
|
||||
If it's not, return else"
|
||||
[tst else]
|
||||
`(let [res# ~tst]
|
||||
(if res#
|
||||
res#
|
||||
~else)))
|
||||
|
||||
(defmacro ret=
|
||||
"compares two values using [=]. If the result is true
|
||||
returns the value, else the result of [=].
|
||||
|
||||
Usefull with if-else"
|
||||
[x y]
|
||||
`(let [x# ~x
|
||||
y# ~y
|
||||
result# ~(= x y)]
|
||||
(if result#
|
||||
~x
|
||||
result#)))
|
Loading…
Reference in a new issue