Refactor generic functions to separate utils namespace, finaly bump version

This commit is contained in:
Felisp 2024-01-05 01:45:41 +01:00
parent 5551be7012
commit 43a9781893
3 changed files with 46 additions and 8 deletions

View file

@ -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" :description "RSS based thread watcher"
:url "http://example.com/FIXME" :url "http://example.com/FIXME"
:license {:name "AGPL-3.0-only" :license {:name "AGPL-3.0-only"

View file

@ -18,15 +18,12 @@
[ring.util.response :as response] [ring.util.response :as response]
[clj-rss.core :as rss] [clj-rss.core :as rss]
[clojure.string :as s] [clojure.string :as s]
[rss-thread-watch.watcher :as watcher]) [rss-thread-watch.watcher :as watcher]
[rss-thread-watch.utils :as ut])
(:gen-class)) (: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 (defn new-guid-always
"Generates always unique GUID for Feed item. "Generates always unique GUID for Feed item.
@ -56,7 +53,7 @@
(let [time-of-generation @watcher/time-of-cache (let [time-of-generation @watcher/time-of-cache
guid-fn (if repeat? (fn [x] (new-guid-always x time-of-generation)) guid-fn (if repeat? (fn [x] (new-guid-always x time-of-generation))
update-only-guid) 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)) cache))
;; So we don't have to search thru everything we have cached ;; So we don't have to search thru everything we have cached
needed-cache-part (subvec cache cache-start-index) needed-cache-part (subvec cache cache-start-index)

View 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#)))