Skip to content

Instantly share code, notes, and snippets.

@th507
th507 / fpc-3-different-level.js
Last active August 14, 2023 07:57
3 different form of fixed point combinators
// Y is the fixed point combinator. w1, w2, w3 are its building block.
// one level deep
var w1 = f => n => f( w1(f) )(n)
var Y = w1
// two level deep
var w2 = g => f => n => f( g(g)(f) )(n)
var Y = w2(w2)
@th507
th507 / self2fpc.js
Last active July 13, 2023 10:06
A step-by-step explanation: From self-referencing function to fixed-point combinator
/* A step-by-step explanation (with generating Fibonacci Number):
From naïve self-referencing function to fixed-point combinator
*/
// test runner
const t = (i => f => console.log(`v${i++}`, [1,2,3,4,5].map(f)))(1)
// 1 naïve approach
var fib = n => n < 2 ? 1 : fib(n - 2) + fib(n - 1)
t(fib)
@th507
th507 / toggle_menu_bar.scpt
Created October 8, 2021 07:16
Toggle macOS menubar (compatible w/ earlier versions of macOS)
set os_version to system version of (system info)
if os_version as string < "11.0" then
#display dialog "Smaller than 11"
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.general"
end tell
delay 0.5
@th507
th507 / toggle-finder.scpt
Last active February 19, 2021 07:05
Toggle Finder (on mac)
appName = "Finder"
app = Application(appName)
if (!app.running()) {
app.activate()
}
else {
if (! app.visible()) {
app.activate()
@th507
th507 / song-list-on-amazon.js
Created May 27, 2020 03:36
Output Album Song List on Amazon
console.log(
(x => $$('div.a-section a.TitleLink')
.map(y => y.innerText)
.filter((name, bool) => (bool = (x !== name), x = name, bool))
.reduce((result, name, i) => `${result}${i + 1}. ${name}
`, "\n")
)()
)
@th507
th507 / dark-mode-toggle-scpt
Last active August 15, 2019 02:29
Toggle macOS Dark Mode (via JSXA & macOS Script Editor Automation)
pref = Application("System Events").appearancePreferences
// weird syntax with no documentation...
pref.darkMode = !pref.darkMode()
@th507
th507 / shrink-microsoft-office-for-mac.js
Created April 7, 2017 16:58
Reduce disk usage for Microsoft Office 365 (Mac) by linking identical files
const fs = require('fs')
const child = require('child_process')
if (process.env.SUDO_UID === undefined) {
console.log(`Permission denied
sudo required`)
process.exit(1)
}
let yCombinator = f => (g => (...p) => f(g(g))(...p))(g => (...p) => f(g(g))(...p))
var factory = f => (...a) => a.length - a[0].length > 1 ? f.call.call(...a) : f.bind(null, ...a)
//
// we could add some fancy code to eliminate fn.call & fn.bind
//
// var topsy = (f => f.bind(f))(Function.call)
// var turvy = (f => f.bind(f))(Function.bind)
// var factory = f => (...a) => (a.length - a[0].length > 1 ? topsy : turvy(f, null))(...a)
//
@th507
th507 / currying-with-generator.js
Last active August 29, 2015 14:23
Currying with generator
function *step(fn) {
var arr = Array(fn.length);
for (var i = 0; i < arr.length; i++) {
arr[i] = (yield);
}
var gen = fn.apply(null, arr);
return gen;
}
@th507
th507 / ftcTracker.coffee
Last active December 27, 2015 09:19
logging offline visits across sessions
# logging offline visits across sessions
# for in-house tracking and Google Analytics
# See my blog for more information (Chinese)
# http://ljw.me/2013/07/24/logging-offline-visits.html
#
# Usage:
#
# var tracker = new FTCTracker(beaconURLPrefix)
# tracker.push(url)
#