This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require("node-fetch") | |
const fetchAll = async function(urls, max=3) { | |
const results = Array(urls.length) | |
const workers = Array(max) | |
const index = {value:0} | |
for (let i=0; i<max; ++i) { | |
workers.push(fetchAll.worker(urls, results, index)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:url_launcher/url_launcher.dart'; | |
Future<bool> openUrl(String url) async { | |
if (url == null || url.trim().length == 0) { | |
return false; | |
} | |
url = Uri.encodeFull(url.trim()); | |
if (!url.contains('://')) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface PromiseAllContext<T> { | |
results: T[] | |
jobs: (() => Promise<T>)[] | |
launched: number | |
} | |
async function worker<T>(context: PromiseAllContext<T>) { | |
const { jobs, results } = context | |
while (context.launched < jobs.length) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<root> | |
root | |
<a> | |
a | |
<aa>aa</aa> | |
<bb>bb</bb> | |
</a> | |
<b>b | |
<ba>ba</ba> | |
<bb>bb</bb> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const S = "sustainable"; | |
const K = "disabling"; | |
interface Tree { | |
[char: string]: Tree; | |
} | |
function buildTree(str: string): Tree { | |
const tree: Tree = {}; | |
const prevs = [tree]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepEquals(v1: unknown, v2: unknown): boolean { | |
if (v1 === v2) { return true } // equals | |
const t1 = typeof v1 | |
const t2 = typeof v2 | |
if (t1 !== t2) { return false } // diff types | |
if (t1 !== 'object') { return false } | |
// objects or arrays |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function promiseAll<T = unknown>(promises: Promise<T>[]): Promise<T[]> { | |
const results: T[] = [] | |
for (let i = 0; i < promises.length; ++i) { | |
results.push(await promises[i]) | |
} | |
return results | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function floodFill( | |
image: number[][], | |
x: number, | |
y: number, | |
newColor: number, | |
): void { | |
const minY = 0 | |
const maxY = image.length - 1 | |
if (maxY < 0) { return } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
typedef struct graph_node { | |
char *name; | |
struct graph_node *parent; | |
struct graph_node **children; | |
size_t children_size; | |
size_t children_n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const grid = [ | |
[3, 1, 1, 1], | |
[2, 1, 0, 1], | |
[1, 1, 0, 2], | |
[2, 0, 1, 0], | |
] | |
interface Area { | |
value: number | |
points: Set<number> |