Skip to content

Instantly share code, notes, and snippets.

@kidd
kidd / init.el
Last active April 18, 2024 08:16
advice browse-url-at-point to account for stripe ids
;;; (keymap-global-set "C-c o" 'browse-url-at-point)
(defun rgc/add-stripe (args)
"prepend the right stripe path to be able to find the entity
you're looking for"
(let* ((arg (s-replace-regexp "^https?://" "" (car args)))
(path
(cond
((s-match "^price_.*" arg) "prices")
((s-match "^sub_.*" arg) "subscriptions")
(def hh [{:a 1, :b 2, :c 3 :aa 4} {:a 1, :b 2, :c 4 :aa 4} {:a 99, :b 2, :c 3 :aa 5}])
(def rname :trial-ups)
(def main-f [:b :a])
;; first stab, we pivot over the keys of the "root" entity,
;; group by them, and strip the rest out
(defn hydrate-with-main-entity-keys [m ks as]
(->> (group-by #(select-keys % main-f) hh)
(m/map-vals (partial map #(apply dissoc % main-f)))
@kidd
kidd / dup-file.sh
Created October 9, 2023 11:16
Duplicate file finder helpers
# helping to find dupes. Not complete,
# but this can serve as a basis for a pure shellscript dup finder
find . -type f -exec du -s {} \; -exec /usr/bin/basename {} \; >/tmp/list.txt
cat /tmp/list.txt | cut -f1 -d' ' | paste - - | grep png | sort | uniq| wc -l
cat /tmp/list.txt | cut -f1 -d' ' | paste - - | grep png | sort | wc -l
@kidd
kidd / fzf.sh
Last active October 4, 2023 07:53
pure shell naive version of a picker like fzf (without the filtering, but only picking by number)
# fzf is either fzf or a naive version of it
# the input is a sed line number, so it can be
# single number: 42
# range: 1,10
# separate lines: 10p;50p
mute command -v fzf ||
fzf() {
local in=$(cat)
for p in "${@}"; do
[ "$p" = "-0" ] && [ "$(echo "$in" | wc -l)" -eq 1 ] && [ "" = "$in" ] && return 1
@kidd
kidd / whiteboard.js
Created September 21, 2023 07:58
whiteboard bookmarklet
data:text/html,<canvas id="v"><script>d=document,d.body.style.margin=0,f=0,c=v.getContext("2d"),v.width=innerWidth,v.height=innerHeight,c.lineWidth=2,x=e=>e.clientX||e.touches[0].clientX,y=e=>e.clientY||e.touches[0].clientY,d.onmousedown=d.ontouchstart=e=>{f=1,e.preventDefault(),c.moveTo(x(e),y(e)),c.beginPath()},d.onmousemove=d.ontouchmove=e=>{f&&(c.lineTo(x(e),y(e)),c.stroke())},d.onmouseup=d.ontouchend=e=>f=0</script>
mb:maybehelp() {
local msg="$1";shift
if any equals_help "$@"; then
warn "$msg"
return 1
fi
}
mb:maybehelp "mmsg" "$@" || return 1
-- select internal.array_unique(array['foo','bar'] || array['foo'])
create or replace function array_unique (a text[]) returns text[] as $$
select array (
select distinct v from unnest(a) as b(v)
)
$$ language sql;
-- Example usage:
-- select ts, op, jsonb_pretty(mb_jsonb_diff(record, old_record))
-- from audit.record_version
@kidd
kidd / util.clj
Last active September 6, 2023 17:04
(defn apply-if
"if(p(m)) then f(m, ...args) else m
Useful for `maybe-*` like updates."
[m p f & args]
(if (p m)
(apply f m args)
m))
(defn fork [l c r]
@kidd
kidd / postgres_fdw.sql
Created August 16, 2023 09:42
Demo of postgres_fdw to "tunnel" access to a remote table;
create extension postgres_fdw ;
create server dev foreign data wrapper postgres_fdw options (host 'localhost', dbname 'db_dev');
select user;
create user mapping for my_user server dev options (user 'my_user' );
create schema dev;
import foreign schema public limit to ( table_to_copy ) from server dev into dev;
select * from dev.table_to_copy;
@kidd
kidd / duckdb.sql
Last active February 24, 2024 16:52
arg_max example in duckdb
D create table test as SELECT i as i ,random() as r, i%4 as gr from generate_series(1,100) t(i);
D select * from test;
┌───────┬──────────────────────┬───────┐
│ i │ r │ gr │
│ int64 │ double │ int64 │
├───────┼──────────────────────┼───────┤
│ 1 │ 0.14984029697949075 │ 1 │
│ 2 │ 0.20274345139105418 │ 2 │
│ 3 │ 0.07478489612107744 │ 3 │