rss-thread-watcher/src/rss_thread_watch/utils.clj

53 lines
1.3 KiB
Clojure
Raw Normal View History

(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)))
;; Todo: Test this properly and if working, replace when-else with this
;; Todo: find a way to download clojure documentation for offline use
(defmacro when-else2
"Evaluates [tst], if it's truthy value returns that value.
If it's not, execute everything in [else] and return last expr."
[tst & else]
`(let [res# ~tst]
(if res#
res#
(do ~@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#)))