Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View kevinbarabash's full-sized avatar

Kevin Barabash kevinbarabash

  • Khan Academy
  • Montreal
View GitHub Profile
@kevinbarabash
kevinbarabash / proxied-array.js
Created June 27, 2023 03:23
Proxied Array to enabled negative indices on arrays in JavaScript
const arrayInstanceHandler = {
get(target, property, receiver) {
if (property < 0) {
property = target.length + parseInt(property);
}
return target[property];
},
};
const arrayConstructorHandler = {
class Sheep:
def __init__(self, name: str):
self.name = name
self.naked = False
def is_naked(self) -> bool:
return self.naked
def shear(self):
if self.is_naked():
@kevinbarabash
kevinbarabash / ast.rs
Created April 13, 2023 03:17
fmt::Display implementation for arena based tree struct
use std::fmt;
enum Node<T> {
Add { left: T, right: T },
Mul { left: T, right: T },
Num { value: i64 },
}
struct Arena {
nodes: Vec<Node<usize>>,
Machine({
id: "light",
initial: "green",
states: {
red: {
initial: "no_cars",
states: {
no_cars: {
on: {
SENSE_CAR: "cars",
const existingTransitions = {
EXISTING_PASSWORD: [
{
target: "check_existing",
actions: assign({ existing: (ctx, evt) => evt.value }),
},
],
};
const passwordTransitions = {
@kevinbarabash
kevinbarabash / math-utils.js
Created May 24, 2020 23:09
mocking named exports with jest
const ZERO = 0;
const ONE = 1;
export const add = (a, b) => a + b;
export const mul = (a, b) => a * b;
export const sum = (...args) => args.reduce(add, ZERO);
export const product = (...args) => args.reduce(mul, ONE);
@kevinbarabash
kevinbarabash / recursion-schemes-flow.js
Created May 22, 2019 03:59
recursion schemes in flow
// @flow
import type {Expr, ExprF} from "./ast.js";
const fmap = <A, B>(fn: A => B, expr: ExprF<A>): ExprF<B> => {
switch (expr.type) {
case "number":
return expr;
case "variable":
return expr;
case "apply":
@kevinbarabash
kevinbarabash / bar.js
Last active November 21, 2017 04:38
Why does foo, bar, and baz depend on all three vendor bundles?
const moment = require("moment");
module.exports = "bar";
@kevinbarabash
kevinbarabash / webpack.config.js
Created June 12, 2016 02:41
webpack.config.js for require math-input without having to delete math-input's node_modules or .babelrc
const path = require("path");
const webpack = require("webpack");
const includeEditor = process.env.INCLUDE_EDITORS === "true";
const prod = process.env.NODE_ENV === "production";
module.exports = {
entry: "./src/" + (includeEditor ? "editor-" : "") + "perseus.js",
output: {
path: "./build",
@kevinbarabash
kevinbarabash / pattern-matching.js
Created April 30, 2016 20:08
using operator overloading for pattern matching
console.log('');
`Number | String`; // what about chaining?
`{ type: String }`;
`{ type: 'FunctionDeclaration' | 'FunctionExpression' }`;
// etc.
// what if... we provide definitions for combining two functions or two literals to provide pattern matching