Skip to content

Instantly share code, notes, and snippets.

View marsgpl's full-sized avatar
🔴
Recording

Iurii Belobeev marsgpl

🔴
Recording
View GitHub Profile
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))
}
@marsgpl
marsgpl / openUrl.dart
Last active May 11, 2020 13:42
Flutter open url
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('://')) {
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) {
<root>
root
<a>
a
<aa>aa</aa>
<bb>bb</bb>
</a>
<b>b
<ba>ba</ba>
<bb>bb</bb>
@marsgpl
marsgpl / findLongestCommonString.ts
Created June 9, 2023 09:27
Find Longest Common String
const S = "sustainable";
const K = "disabling";
interface Tree {
[char: string]: Tree;
}
function buildTree(str: string): Tree {
const tree: Tree = {};
const prevs = [tree];
@marsgpl
marsgpl / deepEquals.ts
Last active December 6, 2023 17:28
deepEquals.ts
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
@marsgpl
marsgpl / promiseAll.ts
Last active April 28, 2024 22:48
promiseAll.ts
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
}
@marsgpl
marsgpl / floodFill.ts
Last active December 5, 2023 02:33
floodFill.ts
function floodFill(
image: number[][],
x: number,
y: number,
newColor: number,
): void {
const minY = 0
const maxY = image.length - 1
if (maxY < 0) { return }
@marsgpl
marsgpl / graph.c
Last active December 5, 2023 06:07
graph.c
#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;
@marsgpl
marsgpl / findLargestArea.ts
Created December 5, 2023 23:25
findLargestArea.ts
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>