Dark mode
ตัวอย่างโค้ดโครงสร้างข้อมูลพื้นฐาน
Array (อาเรย์)
typescript
// FP: Array operations (immutable)
export const getElement = <T>(arr: readonly T[], idx: number): T | undefined => arr[idx];
export const setElement = <T>(arr: readonly T[], idx: number, value: T): T[] =>
arr.map((v, i) => i === idx ? value : v);
// Usage:
// getElement([10, 20, 30], 2) // 30
// setElement([10, 20, 30], 1, 25) // [10, 25, 30]
Linked List (ลิงค์ลิสต์)
typescript
// FP: Linked List as algebraic data type
export type ListNode<T> = { value: T; next: ListNode<T> | null };
export type LinkedList<T> = ListNode<T> | null;
export const listAppend = <T>(list: LinkedList<T>, value: T): LinkedList<T> =>
list === null ? { value, next: null } : { ...list, next: listAppend(list.next, value) };
export const listToArray = <T>(list: LinkedList<T>): T[] => {
const result: T[] = [];
let node = list;
while (node) {
result.push(node.value);
node = node.next;
}
return result;
};
// Usage:
// let l: LinkedList<number> = null;
// l = listAppend(l, 10);
// l = listAppend(l, 20);
// l = listAppend(l, 30);
// listToArray(l) // [10, 20, 30]
Stack (สแตก)
typescript
// FP: Stack using array and pure functions
export type Stack<T> = readonly T[];
export const stackPush = <T>(stack: Stack<T>, item: T): Stack<T> => [...stack, item];
export const stackPop = <T>(stack: Stack<T>): [T | undefined, Stack<T>] =>
stack.length === 0 ? [undefined, stack] : [stack[stack.length - 1], stack.slice(0, -1)];
export const stackPeek = <T>(stack: Stack<T>): T | undefined => stack[stack.length - 1];
export const stackIsEmpty = <T>(stack: Stack<T>): boolean => stack.length === 0;
// Usage:
// let s: Stack<string> = [];
// s = stackPush(s, "A");
// s = stackPush(s, "B");
// stackPeek(s) // "B"
// const [popped, s2] = stackPop(s);
Hash Table (แฮชเทเบิล)
typescript
// FP: Hash table using Record and pure functions
export type HashTable<V> = Readonly<Record<string, V>>;
export const hashSet = <V>(table: HashTable<V>, key: string, value: V): HashTable<V> => ({ ...table, [key]: value });
export const hashGet = <V>(table: HashTable<V>, key: string): V | undefined => table[key];
export const hashHas = <V>(table: HashTable<V>, key: string): boolean => key in table;
// Usage:
// let h: HashTable<number> = {};
// h = hashSet(h, "age", 25);
// hashGet(h, "age") // 25
// hashHas(h, "age") // true
Binary Search Tree (ไบนารีเสิร์ชทรี)
typescript
// FP: Binary Search Tree as algebraic data type and pure functions
export type BSTNode = { key: number; left: BSTNode | null; right: BSTNode | null };
export type BST = BSTNode | null;
export const bstInsert = (tree: BST, key: number): BST =>
tree === null
? { key, left: null, right: null }
: key < tree.key
? { ...tree, left: bstInsert(tree.left, key) }
: { ...tree, right: bstInsert(tree.right, key) };
export const bstInorder = (tree: BST): number[] =>
tree === null ? [] : [...bstInorder(tree.left), tree.key, ...bstInorder(tree.right)];
// Usage:
// let bst: BST = null;
// bst = bstInsert(bst, 50);
// bst = bstInsert(bst, 30);
// bst = bstInsert(bst, 70);
// bstInorder(bst) // [30, 50, 70]