Skip to content

Instantly share code, notes, and snippets.

@rotaliator
rotaliator / elisp-time-duration.el
Created November 23, 2023 16:56
Elisp time between
(defun rot-time-duration (begin &optional end)
(if end
(org-minutes-to-clocksum-string
(- (org-duration-to-minutes end)
(org-duration-to-minutes begin)))
(seq-let (begin end) (mapcar 's-trim (s-split "-" begin))
(rot-time-duration begin end))))
;; (rot-time-duration "9:00" "17:00")
@rotaliator
rotaliator / b64encode.clj
Created October 17, 2023 09:45
base64 encode clojure
(import '(java.util Base64))
(defn b64encode [to-encode]
(.encode (Base64/getEncoder) (.getBytes to-encode)))
@rotaliator
rotaliator / fixed_length_password.cljc
Created August 10, 2023 13:52
Generates fixed length password
(defn fixed-length-password
([] (fixed-length-password 20))
([n]
(let [chars (map char (concat (range 48 58) (range 65 91) (range 97 123)))
password (repeatedly n #(rand-nth chars))]
(reduce str password))))
@rotaliator
rotaliator / deep_get.clj
Created October 10, 2022 07:06
Clojure deep get
(defn deep-get
[m k]
(let [values_ (transient [])]
(walk/prewalk (fn [e] (if (and (map? e) (get e k))
(do (conj! values_ (get e k)) e)
e)) m)
(persistent! values_)))
@rotaliator
rotaliator / now_with_pattern.clj
Created September 29, 2022 05:55
Get current time as string with pattern
(defn now-with-pattern
"Returns current time as string with pattern"
[pattern]
(.format (java.time.LocalDateTime/now) (java.time.format.DateTimeFormatter/ofPattern pattern)))
(now-with-pattern "yyyyMMddHHmmss")
@rotaliator
rotaliator / maps-to-table.clj
Created September 22, 2022 07:22
Convert list of map to data table
(defn maps-to-table
([labels list-of-maps]
(maps-to-table labels list-of-maps true))
([labels list-of-maps with-headers?]
(let [header (when with-headers? [(mapv name labels)])
data (mapv (apply juxt labels) list-of-maps)]
(if with-headers?
(concat header data)
data))))
@rotaliator
rotaliator / bb-main.clj
Created September 13, 2022 11:39
like: if __name__ == "__main__" in Python but for Babashka
(defn -main [& args]
)
(when (= *file* (System/getProperty "babashka.file"))
(-main *command-line-args*))
@rotaliator
rotaliator / md5.clj
Created March 25, 2022 10:17
md5 clojure
;; https://gist.github.com/jizhang/4325757?permalink_comment_id=2633984#gistcomment-2633984
(ns md5-clj
(:import (java.security MessageDigest)
(java.math BigInteger)))
(defn md5
[^String s]
(->> s
.getBytes
(.digest (MessageDigest/getInstance "MD5"))
@rotaliator
rotaliator / shell.clj
Last active March 17, 2022 20:08
Windows and Linux sh in clojure
(require '[clojure.string :as str])
(require '[clojure.java.shell :refer [sh]])
(defn shell-linux [cmd]
(sh "sh" "-c" cmd))
(defn shell-windows [cmd]
(sh "cmd" "/C" cmd))
(def shell
@rotaliator
rotaliator / cprint.py
Created February 1, 2022 08:12
Simple python color print
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'