Skip to content

Instantly share code, notes, and snippets.

@tangentstorm
tangentstorm / sh.mjs
Last active April 21, 2024 12:16
shorthand javascript
// sh.mjs: javascript shorthand
// array helpers (apl/j/k)
export const id=x=>x
export const af=(n,x)=>Array(n).fill(x) // TODO: make this 'afl' or 'fil' (aa?)
export const ii=(n,f)=>{for(let i=0;i<n;i++)f(i)}
export const im=(n,f)=>af(n,0).map((_,i)=>f(i))
export const ia=(n,f)=>im(n,id)
export const at=(a,ixs)=>ixs.map(i=>a[i])
export const io=(xs,ys)=>ys.map([].indexOf.bind(xs))
@tangentstorm
tangentstorm / ts-list.js
Created March 7, 2024 06:55
a list component i made in the chrome developer tools by running developer tool snippets against about:blank
customElements.define('ts-list', class extends HTMLElement {
static observedAttributes = ['ix']
constructor() { super(); this.ix=0 }
connectedCallback() {
let sh = this.attachShadow({mode: 'open'})
sh.innerHTML=`
<slot></slot>
<button id="up">^</button>
<button id="add">+</button>
<button id="dn">v</button>`
@tangentstorm
tangentstorm / stripped.js
Created February 19, 2024 06:28
javascript parser combinators ( https://youtube.com/live/ZfjORjtkDpI )
let set=(o,k,v)=>{let r={...o}; r[k]=v; return r}
let gs=k=>(x,y)=> y===undefined ? x[k] : set(y,k,x)
let mb=gs('mb'), // match bit
ib=gs('ib'), // input buffer
ix=gs('ix'), // index into input buffer
ch=gs('ch'), // current character (ib[ix])
mk=gs('mk'); // start index of current token
let s0=()=>({mb:0,ib:'',ch:'',ix:-1,mk:-1})
on=s=>ix(0,ch(s[0],ib(s,s0())))
m1=s=>mb(1,s) // match
@tangentstorm
tangentstorm / GsEllipse.st
Last active November 21, 2023 22:56
Port of Roassal's RSAnimationExamples >> example05ElasticEllipses to Bloc
Class {
#name : #GsEllipse,
#superclass : #GsObject,
#instVars : [
'radius',
'position'
],
#category : #GameSketchLib
}
@tangentstorm
tangentstorm / CmdParser.gd
Created March 10, 2022 03:32
simple command language parser for jprez in gdscript (godot).
# i wrote this, and it works, but then i decided to just use Godot's own Expression parser.
# https://docs.godotengine.org/en/latest/tutorials/scripting/evaluating_expressions.html
func _examples():
test('@title["the deck"]')
test('@show["jp-editor"; 0]')
test('@ed.xy[ 0 5]')
func test(cmd:String):
if cmd.begins_with('@'):
@tangentstorm
tangentstorm / ed.ijs
Last active July 29, 2021 10:59
A tiny editor in J
NB. Core logic for a tiny editor in J.
NB. No select/copy/paste (in this gist), but it does support multiple cursors.
NB. This started as the code for editing a single line of text, but I'm now
NB. using three copies simultaneously: one for a single token, one for
NB. boxed tokens on a line, and one for boxed lines in a buffer.
coclass 'ed'
init =: {{
B =: '' NB. the buffer to edit.
C =: 0 NB. cursor position(s)
@tangentstorm
tangentstorm / parsers.ijs
Last active July 7, 2021 16:44
parser combinators for J
clear''
NB. Parser Combinators for J
NB.
NB. The semantics here are heavily inspired by
NB. Allesandro Warth's ometa system:
NB.
NB. http://tinlizzie.org/ometa/
NB.
NB. but implemented as parser combinators rather than a standalone language.
NB.
@tangentstorm
tangentstorm / asmvm.js
Last active July 2, 2021 05:21
ASM vm... This started as an attempt at a bootstrapping virtual machine, then I got caught up with the idea that bytecode could be human readable, and it probably got a bit out of hand.
const DATA=0, CALL=1, WORK=2, TEMP=3
class Worker {
vm = null // virtual machine
w = 0 // worker number
f = 0 // function pointer
e = 0 // execution pointer
t = 0 // current token in function
@tangentstorm
tangentstorm / meld.js
Created December 2, 2020 03:25
meld a list of dictionaries into a smaller list of dictionaries
glue = (x,y) => x === undefined ? y : Array.isArray(x) ? x.concat(y) : [x,y]
function meld(xs) {
let idx = {}, res = [], it
for (x of xs) {
if (x.id in idx) it = res[idx[x.id]]
else { it = {id: x.id}; idx[x.id] = res.length; res.push(it) }
for (k of Object.keys(x)) if (k!=='id') it[k] = glue(it[k], x[k]) }
return res }
c =: '01' ([: +/ =)"0 _ ] NB. count 0 and 1
t =: {{(*/ <:/"1 c\ y) *. =/c y }} NB. the two rules
g =: {{'01' {~ #:i.2^2*y}} NB. generate the numbers
f =: {{y {~ I.t"1 y}} NB. filter by t
f g 3