Skip to content

Instantly share code, notes, and snippets.

Avatar

Khải KSXGitHub

View GitHub Profile
View first-line.ts
import { createReadStream } from 'fs'
import { createInterface } from 'readline'
const READ_STREAM_OPTIONS = { encoding: 'utf8' } as const
export function firstLine (filename: string) {
const stream = createReadStream(filename, READ_STREAM_OPTIONS)
const reader = createInterface(stream)
let result: string
reader.once('line', line => {
View tmp.zsh
#! /usr/bin/env zsh
script=https://ksxgithub.github.io/my-deno-scripts/cmd/insert-object-key.ts
deno fetch $script
run=(deno run $script)
insert=($run --before dependencies)
args-to-list packages/*/package.json | while read filename; do
echo '>>>' $filename
< $filename |
$insert browser '"index.mjs"' |
$insert module '"index.mjs"' |
View alt-ac-adapter.ts
import xs from 'xstream'
import spawn from 'advanced-spawn-async'
import { Option, Some, some, none, match } from '@tsfun/option'
export enum Status {
Plugged = 'Plugged',
Unplugged = 'Unplugged'
}
type UnknownStatus = Status | 'Unknown'
View tmp.update-workspace.js
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')
View generate-type.js
#! /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(', ')}],`)
}
View generate-type.js
#! /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(', ')}] :`)
}
View maybe.ts
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 Dec 25, 2018
Compose function in Rust
View compose.rs
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 Nov 7, 2018
Sieve of Eratosthenes
View sieve.hs
primes = take 15 (sieve [2..])
sieve (prime:rest) = prime : sieve [x | x <- rest, mod x prime /= 0]
You can’t perform that action at this time.