An educational pure functional programming library in TypeScript
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Convert function declarations to arrow functions in examples

+23 -23
+1 -1
examples/http-client/with-purus.ts
··· 111 111 // Demo - run it 112 112 // ----------------------------------------------------------------------------- 113 113 114 - async function main() { 114 + const main = async () => { 115 115 console.log("=== HTTP Client (with purus) ===\n") 116 116 117 117 // Test 1: Successful request with retry + timeout
+3 -3
examples/http-client/without-purus.ts
··· 15 15 // Fetch with retry and timeout - vanilla implementation 16 16 // ----------------------------------------------------------------------------- 17 17 18 - async function fetchWithRetryAndTimeout( 18 + const fetchWithRetryAndTimeout = async ( 19 19 url: string, 20 20 options: { 21 21 retries?: number 22 22 timeoutMs?: number 23 23 retryDelayMs?: number 24 24 } = {} 25 - ): Promise<User> { 25 + ): Promise<User> => { 26 26 const { retries = 3, timeoutMs = 5000, retryDelayMs = 1000 } = options 27 27 let lastError: unknown 28 28 ··· 80 80 // Demo - run it 81 81 // ----------------------------------------------------------------------------- 82 82 83 - async function main() { 83 + const main = async () => { 84 84 console.log("=== HTTP Client (without purus) ===\n") 85 85 86 86 try {
+1 -1
examples/task-queue/with-purus.ts
··· 181 181 // Demo 182 182 // ----------------------------------------------------------------------------- 183 183 184 - async function main() { 184 + const main = async () => { 185 185 console.log("=== Task Queue (with purus) ===\n") 186 186 187 187 const jobs: Job[] = [
+6 -6
examples/task-queue/without-purus.ts
··· 25 25 // Job Execution with Timeout 26 26 // ----------------------------------------------------------------------------- 27 27 28 - async function executeJob(job: Job): Promise<void> { 28 + const executeJob = async (job: Job): Promise<void> => { 29 29 // Simulate job processing 30 30 const delay = Math.random() * 200 + 50 31 31 await new Promise(resolve => setTimeout(resolve, delay)) ··· 38 38 logger.info(`Job ${job.id} (${job.type}) completed`) 39 39 } 40 40 41 - async function executeWithTimeout(job: Job, timeoutMs: number): Promise<void> { 41 + const executeWithTimeout = async (job: Job, timeoutMs: number): Promise<void> => { 42 42 // PROBLEM: Promise.race doesn't cancel the losing promise! 43 43 // The job keeps running even after timeout - we just ignore the result 44 44 const result = await Promise.race([ ··· 54 54 // Retry Logic 55 55 // ----------------------------------------------------------------------------- 56 56 57 - async function executeWithRetry( 57 + const executeWithRetry = async ( 58 58 job: Job, 59 59 maxRetries: number, 60 60 timeoutMs: number 61 - ): Promise<void> { 61 + ): Promise<void> => { 62 62 let lastError: unknown 63 63 64 64 for (let attempt = 1; attempt <= maxRetries; attempt++) { ··· 82 82 // Queue Processing 83 83 // ----------------------------------------------------------------------------- 84 84 85 - async function processQueue(jobs: Job[]): Promise<void> { 85 + const processQueue = async (jobs: Job[]): Promise<void> => { 86 86 // PROBLEM: Manual promise tracking - easy to mess up 87 87 const results: Array<{ job: Job; success: boolean; error?: unknown }> = [] 88 88 ··· 106 106 // Demo 107 107 // ----------------------------------------------------------------------------- 108 108 109 - async function main() { 109 + const main = async () => { 110 110 console.log("=== Task Queue (without purus) ===\n") 111 111 112 112 const jobs: Job[] = [
+6 -6
examples/workflow-engine/with-purus.ts
··· 75 75 // ----------------------------------------------------------------------------- 76 76 77 77 // SOLUTION: Only accepts DraftOrder - can't pass PaidOrder or ShippedOrder! 78 - function payOrder(order: DraftOrder): Result<PaidOrder, OrderError> { 78 + const payOrder = (order: DraftOrder): Result<PaidOrder, OrderError> => { 79 79 console.log(`[Payment] Processing payment for order ${order.id}`) 80 80 // Transition from Draft to Paid 81 81 const toPaid = transition<OrderData, "Draft", "Paid">() ··· 83 83 } 84 84 85 85 // SOLUTION: Only accepts PaidOrder - compile error if you try to ship a draft! 86 - function shipOrder(order: PaidOrder): ShippedOrder { 86 + const shipOrder = (order: PaidOrder): ShippedOrder => { 87 87 console.log(`[Shipping] Shipping order ${order.id}`) 88 88 // Transition from Paid to Shipped 89 89 const toShipped = transition<OrderData, "Paid", "Shipped">() ··· 91 91 } 92 92 93 93 // SOLUTION: Arguments have distinct types - swapping them is a compile error! 94 - function lookupOrder( 94 + const lookupOrder = ( 95 95 orderId: OrderId, // <-- Branded type 96 96 customerId: CustomerId // <-- Different branded type 97 - ): Result<DraftOrder, OrderError> { 97 + ): Result<DraftOrder, OrderError> => { 98 98 if (orderId === "order-1" && customerId === "cust-1") { 99 99 const data: OrderData = { 100 100 id: orderId, ··· 119 119 // SOLUTION: Exhaustive pattern matching - can't forget error cases 120 120 // ----------------------------------------------------------------------------- 121 121 122 - function formatError(error: OrderError): string { 122 + const formatError = (error: OrderError): string => { 123 123 // SOLUTION: If you add a new error type and forget to handle it, compiler errors! 124 124 return match(error)({ 125 125 InvalidTransition: ({ from, to }) => ··· 135 135 // Demo - run it 136 136 // ----------------------------------------------------------------------------- 137 137 138 - function main() { 138 + const main = () => { 139 139 console.log("=== Workflow Engine (with purus) ===\n") 140 140 141 141 // Test 1: Successful flow
+6 -6
examples/workflow-engine/without-purus.ts
··· 32 32 // Error handling via thrown exceptions 33 33 // ----------------------------------------------------------------------------- 34 34 35 - function payOrder(order: Order): Order { 35 + const payOrder = (order: Order): Order => { 36 36 // PROBLEM: Runtime check that could be forgotten 37 37 if (order.status !== "draft") { 38 38 throw new Error(`Cannot pay order in ${order.status} status`) ··· 41 41 return { ...order, status: "paid" } 42 42 } 43 43 44 - function shipOrder(order: Order): Order { 44 + const shipOrder = (order: Order): Order => { 45 45 // PROBLEM: Another runtime check - easy to forget or get wrong 46 46 if (order.status !== "paid") { 47 47 throw new Error(`Cannot ship order in ${order.status} status`) ··· 51 51 } 52 52 53 53 // PROBLEM: Arguments are all strings - swapping them compiles fine! 54 - function lookupOrder(orderId: OrderId, customerId: CustomerId): Order | null { 54 + const lookupOrder = (orderId: OrderId, customerId: CustomerId): Order | null => { 55 55 // Simulate database lookup 56 56 if (orderId === "order-1" && customerId === "cust-1") { 57 57 return { ··· 66 66 } 67 67 68 68 // PROBLEM: This has a bug - arguments are swapped! But it compiles fine. 69 - function buggyLookup() { 69 + const buggyLookup = () => { 70 70 const customerId: CustomerId = "cust-1" 71 71 const orderId: OrderId = "order-1" 72 72 ··· 79 79 // Error formatting with switch 80 80 // ----------------------------------------------------------------------------- 81 81 82 - function formatError(error: unknown): string { 82 + const formatError = (error: unknown): string => { 83 83 if (error instanceof Error) { 84 84 const message = error.message 85 85 ··· 100 100 // Demo - run it 101 101 // ----------------------------------------------------------------------------- 102 102 103 - function main() { 103 + const main = () => { 104 104 console.log("=== Workflow Engine (without purus) ===\n") 105 105 106 106 // Test 1: Successful flow