Skip to content

Instantly share code, notes, and snippets.

import random
def is_terminal(token):
return token[0] != "_"
def expand(grammer, tokens):
result = []
for token in tokens:
if is_terminal(token):
result.append(token)
@theptrk
theptrk / typescript-react-type-cheat-sheet.tsx
Last active November 14, 2021 02:18
This cheatsheet is for working with types in typescript react
/* Formatting dates */
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
// leave locale undefined (varies according to default locale)
new Date().toLocaleDateString(undefined, {
year: "numeric",
month: "2-digit",
day: "2-digit",
})
/* document event: keydown */
@theptrk
theptrk / react-click-outside.js
Created September 23, 2021 14:39
react click outside of container / form
// answer comes from: https://stackoverflow.com/questions/32553158/detect-click-outside-react-component/54292872#54292872
// this adds typescript and I've used this for a form
/*
Custom Hook
*/
function useOuterClick(callback: any) {
const innerRef = useRef() as React.MutableRefObject<HTMLInputElement>;
const callbackRef = useRef() as React.MutableRefObject<HTMLInputElement>;
@theptrk
theptrk / README.md
Last active June 23, 2020 16:13
pyenv - get available versions for installation

First check the python downloads page to view the current releases

$ open https://www.python.org/downloads/

This produces the list of ALL available pyenv versions

$ pyenv install --list

  ...
  pypy3-dev
@theptrk
theptrk / openweather_api_example.html
Last active June 3, 2020 00:40
This uses the OpenWeather current weather data API: https://openweathermap.org/current
<!DOCTYPE html>
<html>
<head>
<title>Open Weather API</title>
<style type="text/css">
pre {
background-color: #f1f1f1;
padding: 5px;
margin: 5px;
border-radius: 5px;
@theptrk
theptrk / optional_chaining.js
Last active May 20, 2020 16:06
Optional Chaining in javascript - avoid crashing your app
// This is an example of Optional Chaining
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName);
@theptrk
theptrk / app.js
Created May 18, 2020 14:31
This is a starter html snippet
// js goes here
console.log("hello js")
@theptrk
theptrk / html_onsubmit.html
Created May 17, 2020 15:42
HTML onsubmit connected to javascript function using a todo list input example
<!DOCTYPE html>
<html>
<head>
<title>Hello onsubmit</title>
</head>
<body>
<h1>Hello onsubmit</h1>
<form onsubmit="handleForm()">
<input type="text" name="todo" autocomplete="off">
<button type="submit">Create Todo</button>
import sys
print(sys.path)
function X_rec = recoverData(Z, U, K)
% initialize recovered values
X_rec = zeros(size(Z, 1), size(U, 1));
for i = 1:size(X_rec,1)
% projected_x is (1, K)
projected_x = Z(i, :);
% initialize x