{{ message }}

Instantly share code, notes, and snippets.

Texas Toland texastoland

Created Nov 6, 2019
Notion formula inspired by Moment's fromNow
View fromNow.js
 // ((dateBetween(now(), prop("Created"), "months") >= 2) ? (format(dateBetween(now(), prop("Created"), "months")) + " months") : ((dateBetween(now(), prop("Created"), "weeks") >= 2) ? (format(dateBetween(now(), prop("Created"), "weeks")) + " weeks") : ((dateBetween(now(), prop("Created"), "days") >= 2) ? (format(dateBetween(now(), prop("Created"), "days")) + " days") : ((dateBetween(now(), prop("Created"), "hours") >= 2) ? (format(dateBetween(now(), prop("Created"), "hours")) + " hours") : ((dateBetween(now(), prop("Created"), "minutes") >= 2) ? (format(dateBetween(now(), prop("Created"), "minutes")) + " minutes") : "1 minute"))))) + " ago" ( dateBetween(now(), prop("Created"), "months") >= 2 ? format(dateBetween(now(), prop("Created"), "months")) + " months" : dateBetween(now(), prop("Created"), "weeks") >= 2 ? format(dateBetween(now(), prop("Created"), "weeks")) + " weeks" : dateBetween(now(), prop("Created"), "days") >= 2 ? format(dateBetween(now(), prop("Cr
Last active Aug 21, 2019
First steps in functional JavaScript
View combineAll.js
 const N = 10 // sum natural numbers from 1 to n const sumNat = n => n * (n + 1) / 2 console.log(`sumNat(\${N}) = \${sumNat(N)}`) // implement iteratively const sumNat1 = n => { let count = 0 for (let i = 1; i <= n; i++) count += i
Created May 22, 2019
5 ways to implement reduce in TypeScript
View reduce.ts
 // test data const xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] const add = (x: number, y: number) => x + y const mult = (x: number, y: number) => x * y // types type Reducer = (acc: U, x: T) => U type Reduce = (f: Reducer, acc: U, xs: readonly T[]) => U { // 1. baseline const reduce: Reduce = (f, acc, xs) => xs.reduce(f, acc)
Created Mar 12, 2019
Is everything just a monoid in the category of something?
View categories.hs
 (.) :: (b -> c) -> (a -> b) -> (a -> c) -- functions (.) :: cat b c -> cat a b -> cat a c -- categories (++) :: String -> String -> String -- strings (<>) :: a -> a -> a -- monoids (<>) :: cat a a -> cat a a -> a a -- categories (++) :: [a] -> [a] -> [a] -- lists (<|>) :: f a -> f a -> f a -- alternatives (<|>) :: cat a a -> cat a a -> a a -- categories
Created Aug 15, 2018
Legacy cron job to update everything
View sysupdate.sh
 #!/bin/bash all_cmds=(wait apple mas brew cask yarn) get_flags() { local cmd_flags=wambcy if [[ \$# -eq 0 ]]; then set -- -"\$cmd_flags" fi local all_flags=:-:h\$cmd_flags local opt
Created Jul 3, 2018
Fibonacci, tail recursion with int64, memoization with lazy, mutually recursive lets
View fib.ml
 let rec badFib = function | 0 -> 0 | 1 -> 1 | n when n < 0 -> invalid_arg {j|expected n >= 0 but got \$n|j} | n -> badFib (n - 1) + badFib (n - 2) open Int64 (* tail recursion *) let rec goodFib ?(n2 = zero) ?(n1 = one) = function
Created Jul 1, 2018
My first OSS circa 2008
View align.js
 /** * @author jtoland */ Ext.ux.extendMixin('Ext.ux.layout.AlignLayout', Ext.layout.ContainerLayout, function( \$super ) { return { monitorResize:true, parseMargins : function( val ) { if (typeof val == 'string') { return arguments.callee.call(null, val.split(' ')); }
Created Jun 24, 2018
HKT in OCaml/Reason with functors
View tree.ml
 (** `Tree` abstracts over differnet kinds of trees *) module Tree = struct (** `BasicType` is what tree implementations provide *) module type BasicType = sig type _ children (* polymorphic variant easier to use in implementations *) type 'a tree = [ `Leaf of 'a | `Branch of 'a children ] val flatten : 'a tree -> 'a list end