Skip to content

Instantly share code, notes, and snippets.

View scarf005's full-sized avatar

scarf scarf005

View GitHub Profile
import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts"
/** @example
* const input = [1, 1, -1, 1, -1, 1]
* assertEquals(span(input, (x) => x > 0), [[1, 1], [-1, 1, -1, 1]])
*/
export const span = <T>(array: T[], predicate: (line: T) => boolean): [T[], T[]] => {
const index = array.findIndex((line) => !predicate(line))
const before = index === -1 ? array : array.slice(0, index)
const after = index === -1 ? [] : array.slice(index)
import { assertEquals } from "https://deno.land/std@0.177.0/testing/asserts.ts"
import { outdent } from "npm:outdent"
import { trisection } from "./partition_utils.ts"
import { walk } from "https://deno.land/std@0.182.0/fs/walk.ts"
import { asynciter } from "https://deno.land/x/asynciter@0.0.15/asynciter.ts"
import { fromFileUrl, join, resolve } from "https://deno.land/std@0.177.0/path/mod.ts"
export const SCRIPT_DIR = fromFileUrl(import.meta.url)
export const TOP_DIR = resolve(SCRIPT_DIR, "..", "..", "..")
export const JSON_DIR = join(TOP_DIR, "data")
@scarf005
scarf005 / github-local-timezone.user.js
Last active November 13, 2023 02:55
Make github use local timezone format
// ==UserScript==
// @name Github local timezone format
// @namespace https://github.com/scarf005
// @match https://github.com/*
// @match https://gist.github.com/*
// @version 1.1.0
// @description Make github use local timezone format
// @icon https://github.githubassets.com/pinned-octocat.svg
// @grant none
// @downloadURL https://gist.githubusercontent.com/scarf005/20a54f241aabebe89ff3d375b1fc59ff/raw/github-local-timezone.user.js
@scarf005
scarf005 / Promise.allSettled.ts
Created February 27, 2023 13:36
extract only successful values out of Promise.allSettled
function successes<T>(xs: PromiseSettledResult<T>[]): T[] {
return xs
.filter((x): x is PromiseFulfilledResult<T> => x.status === "fulfilled")
.map((x) => x.value)
}
{
"editor.tabSize": 2,
"editor.indentSize": "tabSize",
"editor.detectIndentation": false,
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"[c][cpp]": {
"editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
},
"[log]": {
type PluginName = `${string}/${string}`
type Year = `${number}${number}${number}${number}`
/** An array of glob patterns indicating the files that the configuration object should apply to. If not specified, the configuration object applies to all files matched by any other configuration object. */
type Files = string[]
/** An array of glob patterns indicating the files that the configuration object should not apply to. If not specified, the configuration object applies to all files matched by {@link Files}. */
type Ignores = string[]
/** An object containing settings related to how JavaScript is configured for linting. */
export const TIME1 = {
MILLISECOND: 1,
SECOND: 1000,
MINUTE: 1000 * 60,
HOUR: 1000 * 60 * 60,
DAY: 1000 * 60 * 60 * 24,
WEEK: 1000 * 60 * 60 * 24 * 7,
MONTH: 1000 * 60 * 60 * 24 * 30,
YEAR: 1000 * 60 * 60 * 24 * 365,
}
@scarf005
scarf005 / namusettings.user.js
Created January 26, 2023 14:07
unfold footnotes by default and also other opinionated defaults (overwriteable)
// ==UserScript==
// @name NamuSettings
// @namespace http://tampermonkey.net/
// @version 0.1.0
// @description unfold footnotes by default and also other opinionated defaults (overwriteable)
// @author scarf005
// @match https://namu.wiki/w/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=namu.wiki
// @grant none
// ==/UserScript==
@scarf005
scarf005 / multiprocessing.py
Created January 15, 2023 23:53
learn which method is fastest
import time
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
def split_ranges(n: int, tasks: int) -> list[range]:
"create list of ranges split evenly using number of jobs"
return [range(i, i + n // tasks) for i in range(0, n, n // tasks)]
def math_sum(n: int, _: int | None = None) -> int:
import datetime
from datetime import datetime as Datetime
# Get current date and time in local time zone
current_time = datetime.datetime.now()
# Format the date in ISO 8601 format
iso_date = current_time.isoformat()