Skip to content

Instantly share code, notes, and snippets.

@getify
Last active December 8, 2023 17:51
Show Gist options
  • Save getify/faedec7c918a09820fef91519a9d1b69 to your computer and use it in GitHub Desktop.
Save getify/faedec7c918a09820fef91519a9d1b69 to your computer and use it in GitHub Desktop.
JS vs Foi symbol count comparision: FP (partial application, pipelines, etc)
// note: this uses proposed JS syntax for pipeline operator: https://github.com/tc39/proposal-pipeline-operator
// CHARACTER STATS (code comments and their padded whitespace ignored)...
// letters, numbers (a-z A-Z 0-9): 317
// non-letters-non-whitespace: 138
// simple symbols (+ - * / . " : ( ) [ ] { } , =): 94
// compound symbols (=> ?? ?. |> %%): 15 (30 chars)
// optional semicolons: 14
// whitespace: 90
const arithmetic = op => ({
add: (x,y) => x + y,
subtract: (x,y) => x - y,
multiply: (x,y) => x * y,
divide: (x,y) => x / y
})?.[op] ?? new Error("Invalid");
const adder = arithmetic("add");
const subtractor = arithmetic("subtract");
const tripler = arithmetic("multiply").bind(null,3);
const add3 = adder.bind(null,3);
const sub5 = x => subtractor(x,5);
const compute = v => sub5(add3(tripler(v)));
adder(3, 4); // 7
add3(4); // 7
subtractor(12, 5); // 7
sub5(12); // 7
3 |> tripler(%%) |> adder(3, %%) |> sub5(%%); // 7
compute(3); // 7
sub5(add3(tripler(3))); // 7
// note: the .java on this filename is only for approx syntax highlighting on github
// CHARACTER STATS (code comments and their padded whitespace ignored)...
// letters, numbers (a-z A-Z 0-9): 246
// non-letters-non-whitespace: 123
// simple symbols (+ - * / " : ( ) [ ] { } , | ^ # @ ?): 94
// compound symbols (#> +> <+): 6 (12 chars)
// optional semicolons: 17
// whitespace: 85
defn arithmetic(op) ^(
?(op){
["add"]: (+);
["subtract"]: (-);
["multiply"]: (*);
["divide"]: (/);
: Left@ "Invalid"
}
)
def adder: arithmetic("add");
def subtractor: arithmetic("subtract");
def tripler: (*)|3|;
def add3: adder|3|;
def sub5: subtractor|,5|;
def compute: tripler +> add3 +> sub5;
adder(3, 4); // 7
add3(4); // 7
subtractor(12, 5); // 7
sub5(12); // 7
3 #> tripler #> adder(3, #) #> sub5; // 7
compute(3); // 7
(<+)(sub5, add3, tripler)(3); // 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment