Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am ianthrive on github.
  • I am ianthrive (https://keybase.io/ianthrive) on keybase.
  • I have a public key ASCF0C2HlS5xcLlaKqC29XdE4HwR9RUHoTxVZu9W0N1KzQo

To claim this, I am signing this object:

CREATE OR REPLACE FUNCTION first_dow_of_month(year INTEGER, month INTEGER, dow INTEGER)
RETURNS date LANGUAGE plpgsql
AS $$
DECLARE
first_day DATE := (year||'-'||month||'-1')::DATE;
first_day_plus_week DATE := first_day + '1 week'::INTERVAL;
BEGIN
RETURN first_day_plus_week + dow - DATE_PART('dow', first_day_plus_week)::INTEGER;
END;
$$;
CREATE OR REPLACE FUNCTION json_diff(l JSONB, r JSONB) RETURNS JSONB LANGUAGE sql AS $$
SELECT jsonb_object_agg(a.key, a.value) FROM
( SELECT key, value FROM jsonb_each(l) ) a LEFT OUTER JOIN
( SELECT key, value FROM jsonb_each(r) ) b ON a.key = b.key
WHERE a.value != b.value OR b.key IS NULL;
$$;
@ianthrive
ianthrive / levenshtein.js
Created June 1, 2015 02:54
Levenshtein string distance implementation in JavaScript.
function levenshtein(a, b) {
if(a.length == 0) return b.length;
if(b.length == 0) return a.length;
var matrix = [];
// increment along the first column of each row
var i;
for(i = 0; i <= b.length; i++) {
matrix[i] = [i];
CREATE OR REPLACE FUNCTION public.random_md5() RETURNS text LANGUAGE sql
AS $$ SELECT md5(gen_random_bytes(1024)); $$;
CREATE OR REPLACE FUNCTION public.sha256(subject bytea) RETURNS text LANGUAGE sql
AS $function$ SELECT ENCODE(DIGEST($1, 'sha256'), 'hex'); $function$
CREATE OR REPLACE FUNCTION public.random_sha256() RETURNS text LANGUAGE sql
AS $$ SELECT sha256(gen_random_bytes(1024)); $$;
@ianthrive
ianthrive / clear_dns.sh
Last active August 29, 2015 14:20
Clear DNS cache.
# OS X 10.10
sudo discoveryutil udnsflushcaches; sudo discoveryutil mdnsflushcache
# OS X 10.9
dscacheutil -flushcache; sudo killall -HUP mDNSResponder
# OS X 10.7-10.8
sudo killall -HUP mDNSResponder
# OS X 10.5-10.6
create or replace function diff_elements(anyarray, anyarray)
returns anyarray language sql immutable as $$
select array(
select unnest($2)
except
select unnest($1)
);$$;
create operator - (
procedure = diff_elements,
@ianthrive
ianthrive / is_leap_year.sql
Last active April 29, 2021 09:37
PostgreSQL functions to determine if a date is a leap year. Two different functions to demonstrate two different ways.
CREATE OR REPLACE FUNCTION is_leap_year(year integer)
RETURNS BOOLEAN AS $$
SELECT ($1 % 4 = 0) AND (($1 % 100 <> 0) or ($1 % 400 = 0))
$$ LANGUAGE sql IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION is_leap_year(date date)
RETURNS BOOLEAN AS $$
SELECT DATE_PART('month', DATE_TRUNC('year', $1)+'1 months 28 days'::INTERVAL) = 2;
$$ LANGUAGE sql IMMUTABLE STRICT;
@ianthrive
ianthrive / nth_dow.sql
Last active August 29, 2015 14:06
Calculates and returns the Nth day of the week for the given date.
CREATE OR REPLACE FUNCTION public.extract_nth_dow(d date) RETURNS integer
AS $$
-- Calculates and returns the Nth day of the week for the given date.
SELECT (EXTRACT(day FROM d)::INTEGER - 1) / 7 + 1;
$$ LANGUAGE sql;
@ianthrive
ianthrive / macos-hide-user.sh
Created June 16, 2014 08:45
Hide Mac OS user account.
sudo defaults write /Library/Preferences/com.apple.loginwindow HiddenUsersList -array-add dlt