Skip to content

Instantly share code, notes, and snippets.

View chrispsn's full-sized avatar

Chris Pearson chrispsn

View GitHub Profile
@chrispsn
chrispsn / trees.k
Last active January 1, 2020 12:30
Trees in k
/ "Apter trees" http://archive.vector.org.uk/art10500340
/ https://github.com/stevanapter/hypertree#concepts
/ http://nsl.com/k/treetable/treetable_k3.html
/ http://www.nsl.com/k/trees.k
/ John Earnest notes https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Trees.md
/ Aaron Hsu talk on high-performance tree wrangling https://www.youtube.com/watch?v=hzPd3umu78g
/ https://www.dyalog.com/uploads/conference/dyalog18/presentations/U19_Tree_Wrangling_the_APL_Way.pdf
/ Translation into k by JE: https://github.com/JohnEarnest/ok/blob/gh-pages/examples/key.k
/ Conquering recursion: http://archive.vector.org.uk/art10501740
/ https://news.ycombinator.com/item?id=21680133 - comments from geocar and John Earnest
@chrispsn
chrispsn / eval_in_context.js
Last active October 21, 2019 21:11
IE11-compatible version of Brian Beck's eval-in-context
// https://twitter.com/ua6oxa/status/1170457046153650176
"use strict";
function evalInContext(env, code) {
const
s = JSON.stringify,
locals = Object.keys(env),
keys = locals.map(function(k){return s(k)}),
values = locals.map(function(k){return env[k]}),
body = s("return eval(" + code + ")")
@chrispsn
chrispsn / compression.js
Created August 20, 2019 09:09
Exercise in writing compressed JavaScript using b-like techniques
// Inspired by http://kparc.com/b/c.h
// See also https://chat.stackexchange.com/transcript/message/51402407#51402407
const x="x",y="y"
// Function makers
function fn() {return new Function(...arguments)}
function f0(b) {return fn(b)}
function f1(b) {return fn(x,b)}
function f2(b) {return fn(x,y,b)}
@chrispsn
chrispsn / kOS.md
Last active March 13, 2024 03:25
A summary of everything we know about kOS.
@chrispsn
chrispsn / sheet_code_repr_choices.js
Last active January 27, 2019 10:31
Which sheet code representation do you prefer?
// OPTION A: Nest value and coords within same obj
_CELLS = {
a: {
v: 123,
l: "B2"
},
b: {
get v() {return a * 2},
l: "B3"
@chrispsn
chrispsn / using_mesh_sheet_as_fn.js
Last active January 23, 2019 10:33
Testing ways to use a Mesh sheet as a function in another sheet.
/* Caveats:
- The below code assumes we have access to the consumed sheet's _CELLS object. Unclear how that would be established (c+p?).
- Will somehow need to deal with accessing cells in topological order. We record this when a sheet is run - maybe we can use that?
- Unclear whether/how we will import sheet boilerplate fns (maybe a good idea to move it to a dedicated _BOILERPLATE object...)
*/
var _CELLS = {
a: 123,
get b() {return a * 2}
}
@chrispsn
chrispsn / SUM.js
Last active January 20, 2019 13:46
Simple sum function
// Balancing speed and concision, with a preference for speed. Can we do better?
// No ES6! Mesh is ES5-compatible.
// Added to global namespace by calling the following:
// g._defFunctions = function() {for (let k in _FUNCTIONS) g[k] = _FUNCTIONS[k]}
// Vanilla
const _FUNCTIONS = {
SUM: function() {
const l=arguments.length,f=function(x,y){return x+y};
@chrispsn
chrispsn / json_example.js
Created December 30, 2018 20:50
JSON example
'use strict';
const _CELLS = {
B2: ({
l: [1,1],
n: false,
get v() {
return `{"some": "simple value", "another": "simple_value", "nested": [1, 2, 3], "obj": {"hello": "world!", "it's": "me"}}`;
}
}),
@chrispsn
chrispsn / why_tables.js
Last active November 18, 2018 03:22
Exploring why tables are more appropriate for Mesh than arrays or objects alone
// 1. Array literals don't have a way to set getters, which are essential for self-references
// They *do* work if you manage them directly, but this produces source code that's difficult to manage.
const arr = [1];
Object.defineProperty(arr, '1', {get: function() {return this[0] + 1}})
console.log(arr[1]) // prints 2
arr[0] = 1234;
console.log(arr[1]) // prints 1235
// 2. Objects literals don't have a key order, which is essential for self-refences to preceding values.
// (You could *potentially* mimic this by making an object literal with keys that are numbers that has __proto__ = Array.prototype,
@chrispsn
chrispsn / ASCII_separator.js
Last active November 6, 2018 12:33
ASCII separator characters in action
// https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
const FS = String.fromCharCode(28); // File separator; end of file. Or between a concatenation of what might otherwise be separate files.
const GS = String.fromCharCode(29); // Group separator; between sections of data. Not needed in simple data files.
const RS = String.fromCharCode(30); // Record separator; end of a record or row.
const US = String.fromCharCode(31); // Unit separator; between fields of a record, or members of a row.
function print_to_string_table(str) {
let o = "";
for (var i = 0; i < str.length; i++) {
const char = str[i];