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
| // - Time: O(n^2 + ∑ |tail[i]|) | |
| // - Space: O(n) | |
| export default function intersectionWith(comparator, ...arrays) { | |
| if (arrays == null || arrays.length === 0) { | |
| return []; | |
| } | |
| const [head, ...tail] = arrays; | |
| let intersections = []; |
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 useWindowSize() { | |
| const width = useSyncExternalStore( | |
| subscribe, | |
| () => window.innerWidth, | |
| () => 0, | |
| ); | |
| const height = useSyncExternalStore( | |
| subscribe, | |
| () => window.innerHeight, |
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 { useState } from "react"; | |
| const COLS = 7; | |
| const ROWS = 6; | |
| const STRIDE = ROWS + 1; | |
| const BITBOARD_SIZE = STRIDE * COLS; | |
| const VISUAL_BOARD_SIZE = ROWS * COLS; | |
| const VERTICAL_SHIFT = 1n; | |
| const DIAGONAL_RIGHT_SHIFT = BigInt(STRIDE + 1); |
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
| class Semaphore { | |
| #queue = []; | |
| #slots; | |
| constructor(limit) { | |
| this.#slots = Math.max(1, limit); | |
| } | |
| acquire() { |
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 { useRef, useState, useEffect } from "react"; | |
| /** | |
| * @template T | |
| * @param {() => Promise<T>} fn | |
| * @param {import("react").DependencyList} deps | |
| */ | |
| export default function useQuery(fn, deps = []) { | |
| const latestFn = useRef(fn); | |
| const [state, setState] = useState({ status: "loading" }); |
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 { useId, useRef, useState, useCallback } from "react"; | |
| export default function Accordion({ sections }) { | |
| const baseId = useId(); | |
| const buttonsRef = useRef([]); | |
| const [openSections, setOpenSections] = useState(new Set()); | |
| const n = sections.length; | |
| const handleKeyDown = useCallback((e, 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 { useId, useRef, useState } from "react"; | |
| export default function Tabs({ defaultValue, items = [] }) { | |
| const id = useId(); | |
| const tabsRef = useRef([]); | |
| const [activeValue, setActiveValue] = useState( | |
| defaultValue ?? items[0]?.value, | |
| ); |
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 { useState, useEffect, useMemo } from "react"; | |
| export default function App() { | |
| return <Table data={table} />; | |
| } | |
| const DELAY = 300; | |
| const table = [ | |
| [[1], [1], [1]], |