React components are synchronous, long-lived and can sometimes re-render very frequently.
How do React components written in a sychronous programming style read values from remote resources only available asynchronously?
#!/usr/bin/env node | |
function closestStraightCity(cities, cityXs, cityYs, queries) { | |
// We don't need to get the closest city to each city in (cities, cityXs, cityYs), | |
// as we allow the caller to provide specific queries. | |
// | |
// Instead, we use the `queries` to generate a list of city indexes. | |
const cityIndexes = queries.map((query) => | |
cities.findIndex((city) => city === query) | |
); |
#!/usr/bin/env ts-node | |
type ValueFn = (index?: number) => any | Promise<any>; | |
const identity = v => v; | |
async function* range(start: number, end: number, value: ValueFn = identity) { | |
let index = start; | |
while (index < end) { | |
yield await value(index); |
class Solution: | |
def findMin(self, nums: List[int]) -> int: | |
# Special case for lists with only two elements. | |
if len(nums) == 2: | |
return min(*nums) | |
# As the array has been sorted and then rotated, it's not actually correctly | |
# sorted and we can't use a binary search in the conventional way. However, | |
# because we're able to make definitive decisions on which side the minimum | |
# value will be found at on every iteration we're still able to use binary |
type _Tuple< | |
T, | |
N extends number, | |
R extends readonly T[] = [] | |
> = R["length"] extends N ? R : _Tuple<T, N, readonly [T, ...R]>; | |
type Tuple<T, N extends number> = _Tuple<T, N> & { | |
readonly length: N; | |
[I: number]: T; | |
[Symbol.iterator]: () => IterableIterator<T>; |
type Constructor<T> = new (...args: any[]) => T; | |
type DefaultType<T> = T extends Constructor<infer U> ? U : T; | |
function defaultObject<V, K extends string | symbol = string | symbol>(Type: Constructor<V> | V): Record<K, DefaultType<V>> & Iterable<[K, DefaultType<V>]> { | |
const store = new Map<K, DefaultType<V>>(); | |
const iterable = { | |
*[Symbol.iterator] () { | |
for (let [key, value] of store) { | |
yield [key, value]; |
from typing import List, Tuple | |
from collections import defaultdict, deque | |
def next_moves(stones: List[int], stone: int, previous_jump: int) -> List[Tuple[int, int]]: | |
return [ | |
(stone + next_jump, next_jump) | |
for next_jump in [previous_jump-1, previous_jump, previous_jump+1] | |
if next_jump > 0 and stone + next_jump in stones | |
] |
function layer(N) { | |
return Math.ceil(Math.sqrt(N)); | |
} | |
function layerRange(L) { | |
const start = Math.pow(L - 1, 2) + 1; | |
const end = Math.pow(L, 2); | |
return [start, end]; | |
} |
import { useCallback, useState } from "react"; | |
import "./styles.css"; | |
const EMPTY = false; | |
const MINE = true; | |
const MINE_LIKELIHOOD_THRESHOLD = 0.9; | |
interface Cell { | |
mine: boolean; | |
excavated: boolean; |
function letterFrequency(str) { | |
const uniques = {}; | |
for (let char of str) { | |
uniques[char] = uniques[char] ? ++uniques[char] : 1; | |
} | |
return uniques; | |
} | |
function letterFrequencyTemplate(uniques) { |