From 43a9781893256d5bedfc0ffc0fa4b9edd00203f6 Mon Sep 17 00:00:00 2001 From: Felisp Date: Fri, 5 Jan 2024 01:45:41 +0100 Subject: [PATCH] Refactor generic functions to separate utils namespace, finaly bump version --- project.clj | 2 +- src/rss_thread_watch/feed_generator.clj | 11 +++---- src/rss_thread_watch/utils.clj | 41 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/rss_thread_watch/utils.clj diff --git a/project.clj b/project.clj index 73ebdc5..7a7f6b9 100644 --- a/project.clj +++ b/project.clj @@ -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" diff --git a/src/rss_thread_watch/feed_generator.clj b/src/rss_thread_watch/feed_generator.clj index 2ec8388..c5c4c63 100644 --- a/src/rss_thread_watch/feed_generator.clj +++ b/src/rss_thread_watch/feed_generator.clj @@ -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) diff --git a/src/rss_thread_watch/utils.clj b/src/rss_thread_watch/utils.clj new file mode 100644 index 0000000..492677f --- /dev/null +++ b/src/rss_thread_watch/utils.clj @@ -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#)))