what the cache?!
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?
from operator import eq, attrgetter as attr | |
from typing import TypeVar, Callable, Iterator, Optional | |
eq_or_none = lambda x, y: y is None or eq(x, y) | |
T = TypeVar('T') | |
def follow(start: T, next_fn: Callable[[T], T], stop: Callable[[T, T], bool] = eq_or_none) -> Iterator[Optional[T]]: | |
yield start |
type Slice = [lowIndex: number, highIndex: number]; | |
function getRandomIntegerInRange([low, high]: Slice): number { | |
return Math.floor(Math.random() * (high - low + 1)) + low; | |
} | |
function createPartitionForSlice([lowIndex, highIndex]: Slice) { | |
return function partition(pivotIndex: number, items: number[]): number { | |
// Partitioning a slice works like so: | |
// - We get the pivot value from the pivot index (in our case, a random index |
export class TrieNode { | |
public children: Record<string, TrieNode> = {}; | |
public isEndOfWord = false; | |
} | |
export class Trie { | |
private root: TrieNode; | |
constructor() { | |
this.root = new TrieNode(); |
class Solution: | |
def search(self, nums: List[int], target: int) -> int: | |
# Special case for lists with only one or two elements. | |
if len(nums) <= 2: | |
try: | |
return nums.index(target) | |
except: | |
return -1 | |
# As the array has been sorted and then rotated, it's not actually |
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 Zipped<A extends readonly (readonly any[])[]> = { | |
[RowIndex in keyof A[0]]: RowIndex extends "length" ? number : { | |
[ArrayIndex in keyof A]: A[ArrayIndex] extends readonly any[] ? A[ArrayIndex][RowIndex] : never; | |
}; | |
}; | |
type NumericRange<TotalLength extends number, TempRange extends any[] = []> = | |
TempRange['length'] extends TotalLength ? TempRange : NumericRange<TotalLength, [...TempRange, TempRange['length']]>; | |
type TupleFromKeys<T, K extends readonly (keyof 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]; | |
} |
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?