((comp set filter) #{0 1 2 3} #{2 3 4 5})
#{3 2}
Tuesday, January 23, 2018
Sunday, January 21, 2018
Clojure: map construction
((fn [kvect, vvect] (apply hash-map (interleave kvect vvect))) [:a :b :c] [1 2 3])
{:c 3, :b 2, :a 1}
Thursday, January 18, 2018
Wednesday, January 17, 2018
Clojure: replicate elements in sequence
((fn [s n] (mapcat (fn [x] (repeat n x)) s)) [1 2 3] 2) ((1 1 2 2 3 3))
Clojure: interleave implementation
((fn interlive [s1 s2] (mapcat vector s1 s2)) [:a :b :c] [1 2 3]) (:a 1 :b 2 :c 3)
Clojure: flatten implementation
((fn flat [s] (if (sequential? s) (mapcat flat s) [s])) '((1 2) 3 [4 [5 6]])) (1 2 3 4 5 6)
Monday, January 15, 2018
Clojure: remove consecutive duplicates from a sequence
(#(map first (partition-by identity %)) [1 1 2 3 3 2 2 3]) [1 2 3 2 3]
Saturday, January 13, 2018
Clojure: generate Fibonacci sequence
((fn [n] (map second (take n (iterate (fn [[a b]] [b (+ a b)]) [0 1])))) 6) (1 1 2 3 5 8)
Clojure: build map from value and keys sequence
((fn [defval vect] (reduce (fn [m k] (conj m {k defval})) {} vect)) 0 [:a :b])
{:a 0, :b 0}
or
((fn [defval vect] (zipmap vect (repeat defval))) 0 [:a :b :c])
{:a 0, :b 0, :c 0}
Clojure: inc map values
Source: Braveclojure
or, with reduce-kv
(reduce (fn [m [k v]] (assoc m k (inc v))) {} {:a 1 :b 2})
{:a 2, :b 3}
NB: In this example, reduce treats the argument {:a 1 :b 2} as a sequence of vectors, like ([:a 1] [:b 2])
or, with reduce-kv
(reduce-kv (fn [m k v] (assoc m k (inc v))) {} {:a 1 :b 2})
{:a 2, :b 3}
Wednesday, November 29, 2017
Clojure: vector 2 map
(reduce #(assoc %1 %2 1) {} ["apn1" "apn2" "apn3"])
{"apn1" 1, "apn2" 1, "apn3" 1}
Thursday, November 23, 2017
byobu: avoid terminal scrolling at each status update
Did not find a proper way to avoid terminal scrolling to the bottom when the byobu status is updated.
So, increased the tmux status-interval with a big enough value to not be bothered.
So, increased the tmux status-interval with a big enough value to not be bothered.
vi ~/.byoburc/.tmux.conf
set -g status-interval 86400
Friday, November 3, 2017
PostgreSQL: Table dependencies via Foreign Keys
Source: How to list tables affected by cascading delete
WITH RECURSIVE t AS (
SELECT
c.oid AS origin_id,
c.oid::regclass::text AS origin_table,
c.oid AS referencing_id,
c.oid::regclass::text AS referencing_table,
c2.oid AS referenced_id,
c2.oid::regclass::text AS referenced_table,
ARRAY[c.oid::regclass,c2.oid::regclass] AS chain
FROM pg_catalog.pg_constraint AS co
INNER JOIN pg_catalog.pg_class AS c ON c.oid = co.conrelid
INNER JOIN pg_catalog.pg_class AS c2 ON c2.oid = co.confrelid
-- Add this line as an input parameter if you want to make a one-off query
-- WHERE c.oid::regclass::text = 'YOUR TABLE'
UNION ALL
SELECT
t.origin_id,
t.origin_table,
t.referenced_id AS referencing_id,
t.referenced_table AS referencing_table,
c3.oid AS referenced_id,
c3.oid::regclass::text AS referenced_table,
t.chain || c3.oid::regclass AS chain
FROM pg_catalog.pg_constraint AS co
INNER JOIN pg_catalog.pg_class AS c3 ON c3.oid = co.confrelid
INNER JOIN t ON t.referenced_id = co.conrelid
WHERE
-- prevent infinite recursion by pruning paths where the last entry in
-- the path already appears somewhere else in the path
NOT (
ARRAY[ t.chain[array_upper(t.chain, 1)] ] -- an array containing the last element
<@ -- "is contained by"
t.chain[1:array_upper(t.chain, 1) - 1] -- a slice of the chain,
-- from element 1 to n-1
)
)
SELECT origin_table,
referenced_table,
array_upper(chain,1) AS "depth",
array_to_string(chain,',') as chain
FROM t
where referenced_table ~ 'shared_db'
Labels:
postgresql
Linux: Alert when running process finishes
Source: Alert when running process finishes
(while kill -0 $pid; do sleep 1; done) && echo "finished"
PostgreSQL: Running SQL scripts with psql
Source: Running SQL scripts with psql
PGOPTIONS='--client-min-messages=warning' psql -X -q -a -1 -v ON_ERROR_STOP=1 --pset pager=off -d mydb -f dump.sql
Labels:
postgresql
Thursday, October 12, 2017
PostgreSQL: Display materialized view definition
select definition from pg_matviews where matviewname = 'mv_jersey_tap_data_3months_lag';
Labels:
postgresql
PostgreSQL: Find numeric rows w/o digit after decimal point
Source: Fetch records that are non zero after the decimal point in PostgreSQL
SELECT count(*) FROM t WHERE amount != trunc(amount)
Labels:
postgresql
Wednesday, October 11, 2017
Delete files older than file
Source: Delete files older than specific file
find __dir__ -type f ! -newer __file__ -delete
Subscribe to:
Posts (Atom)