-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathllms.txt
More file actions
352 lines (261 loc) · 9.4 KB
/
llms.txt
File metadata and controls
352 lines (261 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# functional.js
Lightweight, TypeScript-first functional programming library. Data-last design, auto-curried utilities, tree-shakeable ESM + CJS, zero dependencies.
## Core Philosophy
- Data-last design for composition
- Auto-curried by default
- ~3KB gzipped, zero dependencies
- Pure, immutable-by-default utilities
## Positioning
- Simpler than fp-ts, better inference than Ramda
- More consistent TypeScript than lodash/fp
- Designed for modern build tooling
## Imports
```typescript
import { pipe, flow, map, filter, reduce } from "functional.js";
```
Named exports only. A `fjs` namespace export is available when needed.
## Quick Reference
All functions with TypeScript signatures.
### Core Composition
- `curry(fn: (...args: any[]) => any): CurriedFunction`
- `compose(...fns: Array<(arg: any) => any>): (arg: any) => any`
- `pipe(value: T, ...fns: Array<(arg: any) => any>): any`
- `flow(...fns: Array<(arg: any) => any>): (arg: any) => any`
### Arrays
All are data-last and auto-curried where applicable.
- `each(fn: (value: T, index: number) => any, items: T[]): void`
- `map(fn: (value: T, index: number) => U, items: T[]): U[]`
- `fold(fn: (acc: U, value: T, index: number) => U, initial: U, items: T[]): U`
- `reduce(fn: (acc: T, value: T, index: number) => T, items: T[]): T`
- `clone(items: T[]): T[]`
- `first(fn: (value: T, index: number) => boolean, items: T[]): T | undefined`
- `rest(fn: (value: T, index: number) => boolean, items: T[]): T[]`
- `last(fn: (value: T, index: number) => boolean, items: T[]): T | undefined`
- `every(fn: (value: T, index: number) => boolean, items: T[]): boolean`
- `any(fn: (value: T, index: number) => boolean, items: T[]): boolean`
- `select(fn: (value: T, index: number) => boolean, items: T[]): T[]`
- `best(fn: (a: T, b: T) => boolean, items: T[]): T | undefined`
- `whilst(fn: (value: T, index: number) => boolean, items: T[]): T[]`
- `partition(fn: (value: T, index: number) => boolean, items: T[]): [T[], T[]]`
- `group(fn: (value: T) => K, items: T[]): Record<K, T[]>`
- `shuffle(items: T[]): T[]`
- `nub(fn: (a: T, b: T) => boolean, items: T[]): T[]`
- `strictEquals(a: T, b: T): boolean`
Aliases:
- `foldl` → `fold`
- `reducel` → `reduce`
- `foldll` → `reduce`
- `head` → `first`
- `take` → `first`
- `tail` → `rest`
- `drop` → `rest`
- `all` → `every`
- `contains` → `any`
- `filter` → `select`
- `unique` → `nub`
- `distinct` → `nub`
### Array Ops
- `flatMap(fn: (value: T, index: number) => U[], items: T[]): U[]`
- `chain` → `flatMap`
- `flatten(items: T[][]): T[]`
- `zip(arr1: T[], arr2: U[]): Array<[T, U]>`
- `zipWith(fn: (a: T, b: U) => R, arr1: T[], arr2: U[]): R[]`
- `uniq(items: T[]): T[]`
- `uniqBy(fn: (value: T) => K, items: T[]): T[]`
### Objects
- `toArray(obj: Record<string, any>): Array<[key, value]>`
- `apply(methodOrTuple: K | [K, ...args], items: T[]): any[]`
- `assign(obj1: T, obj2: U): U & T`
- `extend` → `assign`
- `prop(key: K): (obj: Record<K, any>) => any`
- `pluck(key: K, items: T[]): Array<T[K]>`
- `pick(keys: K[], obj: T): Pick<T, K>`
- `omit(keys: K[], obj: T): Omit<T, K>`
- `path(pathArray: Array<string | number>, obj: any): T | undefined`
- `assoc(key: K, value: T[K], obj: T): T`
- `dissoc(key: K, obj: T): Omit<T, K>`
### Utilities
- `identity(value: T): T`
- `constant(value: T): () => T`
- `tap(fn: (value: T) => void): (value: T) => T`
### Async
- `mapAsync(fn: (value: T, index: number) => Promise<U>, items: T[]): Promise<U[]>`
- `filterAsync(fn: (value: T, index: number) => Promise<boolean>, items: T[]): Promise<T[]>`
- `reduceAsync(fn: (acc: T, value: T, index: number) => Promise<T>, items: T[]): Promise<T>`
- `foldAsync(fn: (acc: U, value: T, index: number) => Promise<U>, initial: U, items: T[]): Promise<U>`
- `eachAsync(fn: (value: T, index: number) => Promise<void>, items: T[]): Promise<void>`
- `pipeAsync(value: T, ...fns: Array<(arg: any) => Promise<any>>): Promise<any>`
- `flowAsync(...fns: Array<(arg: any) => Promise<any>>): (arg: any) => Promise<any>`
- `composeAsync(...fns: Array<(arg: any) => Promise<any>>): (arg: any) => Promise<any>`
### Type Checks
- `isFunction`, `isObject`, `isArray`, `isArguments`
- `isDate`, `isNumber`, `isRegExp`, `isString`
- `exists`, `truthy`, `falsy`
### Types
- `Predicate<T>`, `Comparator<T>`, `Mapper<T, U>`, `Reducer<T, U>`
- `UnaryFn<T, U>`, `AnyFunction`
- `Curried2`, `Curried3`, `Curried4`, `CurriedFunction`
## Common Patterns
### Data pipeline
```typescript
import { pipe, map, filter, reduce } from "functional.js";
const result = pipe(
data,
filter((x) => x.active),
map((x) => x.name),
reduce((acc, name) => acc + name)
);
```
### Reusable flow
```typescript
import { flow, filter, map } from "functional.js";
const getNames = flow(
filter((x) => x.active),
map((x) => x.name)
);
```
### Currying for reuse
```typescript
import { curry, map } from "functional.js";
const discount = curry((percentage: number, price: number) => price * (1 - percentage / 100));
const apply10 = discount(10);
const discounted = map(apply10, prices);
```
## Common Use Cases
### Transforming and filtering data
```typescript
import { pipe, filter, map } from "functional.js";
const names = pipe(
users,
filter((u) => u.active),
map((u) => u.name)
);
```
### Shaping objects
```typescript
import { pipe, pick, assoc, path } from "functional.js";
const summary = pipe(
profile,
pick(["id", "name"]),
assoc("city", path(["details", "city"], profile))
);
```
### Async pipelines
```typescript
import { pipeAsync, mapAsync, filterAsync } from "functional.js";
const run = async (ids: number[]) => {
const passingIds = await pipeAsync(
ids,
mapAsync((id) => fetchScore(id)),
filterAsync((score) => score >= 50)
);
return passingIds;
};
```
## Migration Guides
### From Ramda
- `R.pipe` → `pipe`
- `R.compose` → `compose`
- `R.map` → `map`
- `R.filter` → `filter`
- `R.reduce` → `reduce`
- `R.groupBy` → `group`
- `R.uniq` → `uniq`
- `R.prop` → `prop`
```typescript
import { pipe, map, filter } from "functional.js";
const result = pipe(
items,
filter((item) => item.ready),
map((item) => item.id)
);
```
### From lodash/fp
- `fp.flow` → `flow`
- `fp.compose` → `compose`
- `fp.map` → `map`
- `fp.filter` → `filter`
- `fp.reduce` → `reduce`
- `fp.get` → `path`
- `fp.pick` → `pick`
- `fp.omit` → `omit`
```typescript
import { flow, filter, map } from "functional.js";
const getReadyIds = flow(
filter((item) => item.ready),
map((item) => item.id)
);
```
## Performance Characteristics
- Small bundle size and tree-shakeable named exports
- Data-last API enables partial application without allocations
- Async utilities avoid intermediate arrays when chained in `pipeAsync`
## TypeScript Best Practices
- Rely on inference for callbacks in `map`, `filter`, `reduce`
- Add generics only when inference needs help
- Prefer `flow` for reusable pipelines and `pipe` for inline usage
- Avoid explicit types for intermediate values unless required
## Type System Guide
### When to Add Types
Prefer inference for callbacks, especially with `map`, `filter`, and `reduce`:
```typescript
const numbers = [1, 2, 3, 4, 5];
const doubled = map((x) => x * 2, numbers);
```
Add generics only when inference is insufficient:
```typescript
const getActiveNames = flow(
filter<User>((u) => u.active),
map((u) => u.name)
);
```
Use `flow` when you want a reusable typed pipeline that can be called multiple times.
## Composition Guide
### Using pipe
Use `pipe` when you have the data already and want to run a pipeline immediately:
```typescript
import { pipe, filter, map } from "functional.js";
const result = pipe(
users,
filter((u) => u.active),
map((u) => u.name)
);
```
### Using flow
Use `flow` when you want a reusable function:
```typescript
import { flow, filter, map } from "functional.js";
const getActiveNames = flow(
filter((u) => u.active),
map((u) => u.name)
);
const names1 = getActiveNames(users);
const names2 = getActiveNames(otherUsers);
```
### Using compose
Use `compose` for right-to-left composition (less common):
```typescript
import { compose, map, filter } from "functional.js";
const getActiveNames = compose(
map((u) => u.name),
filter((u) => u.active)
);
```
## Common Gotchas
- `reduce` throws on empty arrays without an initial value (use `fold` with an initial value instead)
- Most functions are curried and data-last, so partial application is expected
- `filter` is an alias of `select`, and `head` is an alias of `first`
- `assign` and `shuffle` are immutable and return new values (do not mutate inputs)
- Data comes LAST, not first (e.g., `map(fn, array)` not `map(array, fn)`)
- `curry` ignores extra arguments beyond the function's arity (standard currying behavior)
## Comparison Matrix
| Library | Bundle Size | TypeScript | Auto-Curry | Learning Curve |
| ------------- | ----------- | ---------- | ---------- | -------------- |
| functional.js | ~3KB | Strong | Yes | Low |
| Ramda | ~50KB | Medium | Yes | Medium |
| lodash/fp | ~24KB | Medium | Yes | Low |
| fp-ts | ~15KB | Strong | No | High |
## Notes
- `reduce` throws on empty arrays without an initial value
- Most functions are curried and data-last, enabling partial application
- Prefer `pipe` for immediate evaluation and `flow` for reusable pipelines