Skip to content

Instantly share code, notes, and snippets.

@phkahler
phkahler / gist:1ddddb79fc57072c4269fdd6716bfb72
Created October 5, 2020 22:09
Slow smooth blink an LED
{ // do this every 1ms or two
static int16_t x;
static int16_t a;
int16_t y = 0x4000 - (((int32_t)x * x) >> 16);
a = (a & 0x0fff) + (((int32_t)y * y) >> 16);
LED_io_pin = a >> 12;
x += 35; // slow blink speed
}
@Jaykul
Jaykul / About Batch and PowerShell.md
Last active July 28, 2024 04:33
Executable PowerShell (wrap a ps1 and name it cmd)

This is always my answer to all those "compile your .ps1" solutions that are floating around. Why would you wrap your PowerShell in an exe and some custom host?

Just paste this header on the top of your PowerShell script, and then before you share it, rename it with the .cmd extension. While you're writing, it's a nice syntax-highlighted PowerShell script, and when you rename it, *poof* it's double-clickable. Drop it in the PATH and you can execute it from the Run dialog or from PowerShell, batch, whatever.

Because there are still people around who actually use CMD, I've updated it to show how to handle passing parameters.

A couple of notes:

  1. It runs with ExecutionPolicy unrestricted because if you're still using CMD you probably haven't configured your ExecutionPolicy
  2. It uses -NoProfile to make sure the environment is the same on everyone's PC...
  3. It only creates function :: {} because that allows us to ignore the :: on the first line
@danopia
danopia / spaces-to-csv.ts
Created September 10, 2020 14:48
Small Deno tools
// In a Unix pipe, accepts 'tableized' input with many spaces between fields,
// and outputs the data as a traditional CSV.
// Does not handle input where fields contain their own spaces.
import { BufReader, BufWriter } from "https://deno.land/[email protected]/io/bufio.ts";
import { TextProtoReader } from "https://deno.land/[email protected]/textproto/mod.ts";
const input = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
@buhichan
buhichan / get_free_vars.ts
Last active October 15, 2020 00:40
js get free variables hack
/**
* some insane hack, just want to avoid using expensive parser.
*/
export function getFreeVariables(expr:string, knownSymbols:Record<string,unknown>){
const freeVariables = new Set<string>();
//eslint-disable-next-line
const anyThingPrimitive = ()=>{};
@zalanlevai
zalanlevai / typescript.code-snippets
Created August 25, 2020 23:04
TypeScript code snippets for discrimated unions and unique types
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
@mmichlin66
mmichlin66 / primitive-types-proxy-and-decorator.ts
Last active October 24, 2022 09:07
Virtualizing TypeScript Properties with Proxy and Decorator - listing 6
// @virtual decorator function
function virtual( target: any, name: string)
{
// symbol to keep the proxy handler value
let sym = Symbol( name + "_proxy_handler");
Object.defineProperty( target, name, {
enumerable: true,
get()
@mmichlin66
mmichlin66 / virtualizing-numbers.ts
Last active October 15, 2020 01:59
Virtualizing TypeScript Properties with Proxy and Decorator - listing 5
class Base
{
@virtual a = 1;
b = this.a;
}
class Derived extends Base
{
a = 2;
}
@mmichlin66
mmichlin66 / proxy-and-decorator.ts
Last active October 15, 2020 01:59
Virtualizing TypeScript Properties with Proxy and Decorator - listing 4
export function virtual( target: any, name: string)
{
// symbol to keep the proxy handler value
let sym = Symbol( name + "_proxy_handler");
Object.defineProperty( target, name, {
enumerable: true,
get()
{
@mmichlin66
mmichlin66 / virtualizing-with-proxy.ts
Last active October 15, 2020 02:00
Virtualizing TypeScript Properties with Proxy and Decorator - listing 3
class VirtHandler
{
target: any;
get( t: any, p: PropertyKey, r: any): any
{
return this.target[p];
}
}
@mmichlin66
mmichlin66 / virtualizing-with-function.ts
Last active October 15, 2020 02:00
Virtualizing ypesScript Class Properties with Proxy and Decorator - listing 2
class Base
{
first = { v: 1 };
second = () => this.first;
}
class Derived extends Base
{
first = { v: 2 };
}