Skip to content

Instantly share code, notes, and snippets.

View erwinv's full-sized avatar

Erwin Dee Villejo erwinv

View GitHub Profile
@erwinv
erwinv / conditional_prop_types.ts
Created March 27, 2022 09:34
Required/optional props depending on another prop/flag
interface CommonProps {
a: number
b: string
}
interface EnabledProps {
onChangeFn: (newVal: string) => void
}
type ConditionalProps =
package main
import "fmt"
// List represents a singly-linked list that holds
// values of any type.
type List[T any] struct {
next *List[T]
val T
}
twinPrimes = filter twin (zip primes (tail primes))
twin (x,y) = y == x+2
primes = sieve [2..]
sieve (p:ps) = p : sieve [x | x <- ps, mod x p /= 0]
@erwinv
erwinv / factorial-uyz-combinator.mjs
Last active January 30, 2022 14:38
Non-recursive Factorial implementation using U/Y/Z combinators
// U-combinator
const U = f => f(f)
const Uz = f => v => f(f)(v) // like U, but for eager/non-lazy languages
// (stops infinite expansion caused by eager evaluation)
// Y/Z-combinator
const Y = g => (f => g( f(f) )) (f => g( f(f) )) // or g => U( f => g( U(f) ) )
const Z = g => (f => g( v => f(f)(v) )) (f => g( v => f(f)(v) )) // or g => U( f => g( Uz(f) ) )
// properties:
// Y g = g(Y g) = g(g(Y g)) = g(...g(Y g)...)
function* parenthesesPermutations_(prefix, nOpen, nClose, score) {
if (score < 0) {
return;
}
if (nOpen === 0 && nClose === 0) {
if (score === 0) {
yield prefix;
}
return;
@erwinv
erwinv / header.puml
Last active February 14, 2022 15:18
PlantUML header
!pragma teoz true
!theme plain
hide footbox
skinparam ParticipantPadding 20
skinparam BoxPadding 20
@erwinv
erwinv / promisify.ts
Last active July 18, 2022 02:59
TypeScript Variadic Tuples for typing `promisify`
type Tuple<T=any> = readonly T[]
type Nullable<T> = T extends null | undefined ? T : T | null | undefined
type ErrorFirstCallback<E extends Error, R> = (err: Nullable<E>, result: R) => void
type Resolve<R> = (value: R | PromiseLike<R>) => void
type Reject<E extends Error> = (reason: Nullable<E>) => void
function makeCallback<E extends Error, R>(resolve: Resolve<R>, reject: Reject<E>): ErrorFirstCallback<E, R> {
return (error, result) => {
if (error) reject(error)
else resolve(result)
import readline from 'readline'
import { Readable } from 'stream'
import { EOL } from 'os'
class MyUrl extends URL {
constructor(...args) {
super(...args)
this.searchKeys = new Set(this.searchParams.keys())
}
isSame(other) {
@erwinv
erwinv / genApiDoc.ts
Created March 26, 2021 04:22
Generate API document from Markdown
import { promises as fs } from 'fs';
import { JSDOM } from 'jsdom';
import MarkdownIt from 'markdown-it';
import open from 'open';
import { tmpdir } from 'os';
import { join as joinPath } from 'path';
import request from 'request-promise-native';
import SVGO from 'svgo';
const PLANTUML_SERVER = 'https://www.plantuml.com/plantuml'
@erwinv
erwinv / vigenere.js
Created October 4, 2020 07:19
Vigenere Cipher
// https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher#Description
class VigenereCipher {
constructor(...keys) {
this.keys = Array.from(keys).map(normalize)
}
get key() { return this._key }
set key(key) {
this._keys = [key]
this._key = normalize(key)