Skip to content

Instantly share code, notes, and snippets.

View KSXGitHub's full-sized avatar
🏠
Working from home

Khải KSXGitHub

🏠
Working from home
  • Earth, Solar System, Orion Arm, Milky Way
  • X @hvksmr1996
View GitHub Profile
const fs = require('fs')
const path = require('path')
const semver = require('/home/khai/lib/node_modules/semver')
const pkgdir = path.join(__dirname, 'packages')
for (const pkgname of fs.readdirSync(pkgdir)) {
const dirname = path.join(pkgdir, pkgname)
const stats = fs.statSync(dirname)
if (!stats.isDirectory()) continue
const manifest = path.join(dirname, 'package.json')
#! /usr/bin/env node
const LIMIT = 256
const range = n => Array(n).fill(0).map((_, i) => i)
const unionRange = n => range(n).join(' | ')
console.log(`export type Foo<N extends ${unionRange(LIMIT)}> = [`)
for (const n of range(LIMIT)) {
console.log(` [${range(n).join(', ')}],`)
}
#! /usr/bin/env node
const LIMIT = 256
const range = n => Array(n).fill(0).map((_, i) => i)
const unionRange = n => range(n).join(' | ')
console.log(`export type Foo<N extends ${unionRange(LIMIT)}> = `)
for (const n of range(LIMIT)) {
console.log(` N extends ${n} ? [${range(n).join(', ')}] :`)
}
interface MaybeBase {
readonly loaded: boolean
}
interface Some<X> extends MaybeBase {
readonly loaded: true
readonly value: X
}
interface None extends MaybeBase {
@KSXGitHub
KSXGitHub / compose.rs
Last active December 25, 2018 16:07
Compose function in Rust
use core::ops::{Add, Mul};
use core::marker::Copy;
use std::fmt::Display;
fn compose<X, Y, Z> (
f: impl Fn(X) -> Y,
g: impl Fn(Y) -> Z
) -> impl Fn(X) -> Z {
move |x: X| g(f(x))
}
@KSXGitHub
KSXGitHub / sieve.hs
Last active November 7, 2018 15:45
Sieve of Eratosthenes
primes = take 15 (sieve [2..])
sieve (prime:rest) = prime : sieve [x | x <- rest, mod x prime /= 0]
#! /usr/bin/env lsc
require! 'path'
require! 'fs'
const {exists-sync, readlink-sync} = fs
const {stdout, stderr, exit, cwd} = require 'process'
const {spawn-sync} = require 'child_process'
require! 'js-yaml'
const {create-command-arguments} = require './lib/shell-command-utils.js'
const argv = let
@KSXGitHub
KSXGitHub / xset.ts
Created October 18, 2018 17:51
Type Guard experimentation of Set
interface XSet<X0> {
has<X1 extends X0> (x: X1): this is XSetWith<X1, X0>
}
interface XSetWith<X1 extends X0, X0> extends XSet<X0> {
has<X2 extends X0> (x: X2): this is XSetWith<X2, X1>
has<X2 extends X1> (x: X2): true
}
declare const xset: XSet<number>
@KSXGitHub
KSXGitHub / tuple-utils.ts
Last active September 12, 2018 11:23
Tuple generic
export namespace TupleUtils {
/**
* Get type of first element
* @example `First<[0, 1, 2]>` → `0`
*/
export type First<Tuple extends [any, ...any[]]> = Tuple[0]
/**
* Get type of last element
@KSXGitHub
KSXGitHub / komorebi.svg
Created August 7, 2018 09:32
Komorebi icons
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.