Skip to content

Instantly share code, notes, and snippets.

@jarppe
jarppe / proxy_byte_channels_example.clj
Created June 16, 2024 12:17
Converting Channel to Java InputStream and OutputStream using proxies
(ns proxy-byte-channels-example
(:import (java.io InputStream
OutputStream)
(java.nio ByteBuffer)
(java.net UnixDomainSocketAddress
StandardProtocolFamily)
(java.nio.channels ByteChannel
SocketChannel)
(java.nio.charset StandardCharsets)))
@jarppe
jarppe / byte_channels_example.clj
Created June 16, 2024 11:58
Simple example for obtaining a ByterChannel to Unix domain socket and converting i/o to java Input and Output streams
(ns byte-channels-example
(:import (java.io InputStream
OutputStream)
(java.net UnixDomainSocketAddress
StandardProtocolFamily)
(java.nio.channels ByteChannel
SocketChannel
Channels)
(java.nio.charset StandardCharsets)))
@jarppe
jarppe / deps.edn
Last active May 22, 2024 12:04
Streaming json
{:paths ["src/main"]
:deps {org.clojure/clojure {:mvn/version "1.12.0-alpha11"}
ring/ring-core {:mvn/version "1.12.1"}
info.sunng/ring-jetty9-adapter {:mvn/version "0.33.1"}
metosin/jsonista {:mvn/version "0.3.8"}}}
@jarppe
jarppe / idle-guard.clj
Created October 18, 2021 08:03
Babashka script to shutdown idle host
#!/usr/local/bin/bb
(require '[clojure.java.shell :as sh]
'[clojure.string :as str])
(import '(java.util.concurrent TimeUnit)
'(java.time ZoneId ZonedDateTime)
'(java.time.format DateTimeFormatter))
@jarppe
jarppe / jsonb_diff.sql
Created April 6, 2021 11:18
PostgreSQL function to produce diff of two jsonb objects. Does not support arrays.
--
-- Helper function to produce a diff of two jsonb values.
--
-- Accepts:
-- val1: original jsonb value
-- val2: updated jsonb value
--
-- Returns:
-- jsonb of changed values
--
@jarppe
jarppe / audit_trail.clj
Last active November 17, 2016 18:24
Return all transactions and all their datoms that have contributed to given entity
(ns datomic-spike.audit-trail
(:require [datomic.api :as d]))
(defn entity-txs
"Accepts an entity, returns all transactions (as entities) that
have altered the given entity, sorted by transaction t"
[e]
(let [id (:db/id e)
db (d/entity-db e)
history (d/history db)]
@jarppe
jarppe / gist:257ac92ce789ca7b9528
Created May 17, 2015 11:08
Map, filter, reduce example, ClojureBridge Helsinki
(def authors [{:first "Margaret" :last "Atwood" :books 15}
{:first "Doris" :last "Lessing" :books 67}
{:first "Ursula" :last "Le Guin" :books 23}
{:first "Alice" :last "Munro" :books 21}])
(defn get-name [author]
(get author :first))
(defn first-name-short? [author]
(let [first-name (get-name author)
@jarppe
jarppe / lazy-seq-of-users.clj
Last active August 29, 2015 14:02
lazy-seq of random users from randomuser.me
; start with: lein try clj-http
(require '[clj-http.client :as http])
(defn fetch-random-users [n]
(->> (http/get "http://api.randomuser.me/" {:query-params {:results n} :as :json})
:body
:results
(map :user)))
@jarppe
jarppe / cout.clj
Last active August 29, 2015 13:56
...because C++ had that one good thing
(defmacro << [& body]
(let [[fmt args] (loop [fmt ""
args []
[p & more] body]
(if p
(cond
(and (symbol? p) (starts-with (name p) "%")) (recur (str fmt (name p)) (conj args (first more)) (rest more))
(symbol? p) (recur (str fmt "%s") (conj args p) more)
(string? p) (recur (str fmt (str p)) args more)
:else (recur (str fmt "%s") (conj args p) more))