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 / pcapdemo.pas
Created July 5, 2014 12:54
pascal / winpcap example ... i don't get any packets
{$mode delphi}
program pcapdemo;
uses pcap, sysutils, crt;
procedure throw(msg:string; detail:pchar);
begin raise Exception.Create(msg + ': ' + detail)
end;
function IPv4ToStr(addr:dword) : string;
// this probably exists somewhere but i don't know where.
@tangentstorm
tangentstorm / oK-bot.js
Created September 11, 2015 22:44
K5 bot for #learnprogramming and #jsoftware on freenode
// An IRC bot for the k5 programming language,
// using oK from : https://github.com/JohnEarnest/ok
"use strict";
var irc = require('irc');
var ok = require('./ok/ok');
const MAXLINES = 8;
var client = new irc.Client('irc.freenode.net', 'oK-bot', {
channels: ['#jsoftware', '#learnprogramming']
@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 / animation.ijs
Created August 12, 2019 14:58
Basic animation in J
NB. Code from the "Basic Animation In J" video
NB. https://www.youtube.com/watch?v=uL-70fMTVnw
NB. ------------------------------------------------------------------------
NB. animation demo
load 'viewmat'
coinsert'jgl2'
wd 'pc w0 closeok' NB. parent control (window) named 'w0'
@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 / gramco.k
Last active July 8, 2021 07:34
Rough port of my grammar combinator thing to k3.
/ grammar combinators in K
/ for longer description (in python), see:
/ http://tangentstorm.github.io/draft/wejalboot.py.html
/ -- misc helper functions ------------------------------------
join:{[sep;strs] / join strs with 'sep' as delimiter
(#sep) _ ,/ sep,' strs}