Skip to content

Instantly share code, notes, and snippets.

View stevekane's full-sized avatar

Steven stevekane

View GitHub Profile
@stevekane
stevekane / iteratevsallocate.js
Created October 10, 2014 21:54
An example of avoiding double allocation when building typed arrays by iterating twice
/*
When creating typed arrays (Float32Array etc) in javascript you often need
to first create a plan Array and then pass it to the typed array constructor
to get out the final memory-packed typed array.
In a tight gameloop, these arrays can be very large and allocating the
javascript array is undesireable as it puts additional GC pressure on your
app which has memory implications and also may produce jutter on devices that
interupt the main javascript thread to do GC.
@stevekane
stevekane / tasjs.js
Last active August 26, 2015 21:49
An overview of using generators to model processes that evolve over time
/*
There are often sequences of behavior that happen over time in complex applications. Sometimes these
are animations, sometimes sequences of async actions, and sometimes both. We would thus like to define
a simple way to express our intent without having to jump through incredible mental hurdles or use limited
features of libaries/frameworks (state transitions and tweens being chief among these) to get the work done.
Here is some code that shows an approach I have been developing for modeling these behaviors in javascript itself
using the re-entrant properties of generators.
*/
@stevekane
stevekane / async-main-fn.ts
Last active September 13, 2016 02:40
Example of a async-oriented monadic main function for javascript
async function raf<T> (f: (t: T) => Promise<T>, t: T): Promise<T> {
return new Promise(res => requestAnimationFrame(_ => res(f(t)))) as Promise<T>
}
async function forever<T> (f: (t: T) => Promise<T>, t: T): Promise<T> {
return await f(t).then(t1 => forever(f, t1))
}
async function update (state: T): Promise<T> {
// Your update logic here
@stevekane
stevekane / json-serialize.js
Created October 27, 2016 15:20
JSON copy w/ function objects
const HDR = '#SOURCE'
const compile = (f) => { try { return eval(f) } catch (e) { return new Error(f) } }
const ser = (_, f) => f instanceof Error ? HDR + f.message : f instanceof Function ? HDR + f.toString() : f
const des = (_, f) => f.indexOf && f.indexOf(0, HDR) ? compile(f.slice(HDR.length)) : f
var o = {
goodFn: () => 5,
badFn: new Error('() =>< 6')
}
var o2 = JSON.parse(JSON.stringify(o, ser), des)
@stevekane
stevekane / monadic_parser.ts
Last active October 27, 2016 16:09
Monadic parsers in Typescript/Javascript
const parse_int =
then(or(match('-'), of('')), => s
then(many(isNumber), => digits
of(Number(s + digits))))
/*
in ML-style lang, this might look like this:
parse_int = do
s <- match '-' <|> unit ''
using System;
using Unity.NetCode;
using Unity.Entities;
[Serializable]
[GenerateAuthoringComponent]
public struct TriggerPlate : IComponentData {
public enum TriggerState { JustTriggered, Triggered, JustUnTriggered, UnTriggered }
[GhostField] public bool Active;
using System;
using Unity.Entities;
[Serializable]
[GenerateAuthoringComponent]
public struct FluidLike : IComponentData {
public float density;
public float lagrangeMultiplier;
}
Stack
empty : Π(τ:U) Stack_(τ,Z)
push : Π(τ:U, t:τ, s:Stack_(τ,n)) Stack_(τ,n+1)
pop : Π(τ:U, s:Stack_(τ,S(n))) (τ × Stack_(τ,n))
pop_push : Π(τ:U, s:Stack_(τ,n), i:τ) (pop ∘ push i) s ≡ i × s
Vector
peek : Π(τ:U, n:N, i:Fin_n, Vector_(τ,i)) τ
poke : Π(τ:U, n:N, i:Fin_n, t:τ, Vector_(τ,i)) Vector_(τ,i)
peek_poke : Π(τ:U, n:N, i:Fin_n, v:Vector_(τ,i), t:τ) (peek i ∘ poke i t) v ≡ t