--- name: fp-data-transforms description: "Practical Data Transformations workflow skill. Use this skill when the user needs Everyday data transformations using functional patterns - arrays, objects, grouping, aggregation, and null-safe access and the operator should preserve the upstream workflow, copied support files, and provenance before merging or handing off." version: "0.0.1" category: data-ai tags: ["functional-programming", "typescript", "data-transformation", "fp-ts", "arrays", "objects", "grouping", "aggregation"] complexity: advanced risk: caution tools: ["codex-cli", "claude-code", "cursor", "gemini-cli", "opencode"] source: community author: "Claude" date_added: "2026-04-15" date_updated: "2026-04-25" --- # Practical Data Transformations ## Overview This public intake copy packages `plugins/antigravity-awesome-skills-claude/skills/fp-data-transforms` from `https://github.com/sickn33/antigravity-awesome-skills` into the native Omni Skills editorial shape without hiding its origin. Use it when the operator needs the upstream workflow, support files, and repository context to stay intact while the public validator and private enhancer continue their normal downstream flow. This intake keeps the copied upstream files intact and uses the `external_source` block in `metadata.json` plus `ORIGIN.md` as the provenance anchor for review. # Practical Data Transformations This skill covers the data transformations you do every day: working with arrays, reshaping objects, normalizing API responses, grouping data, and safely accessing nested values. Each section shows the imperative approach first, then the functional equivalent, with honest assessments of when each approach shines. Imported source sections that did not map cleanly to the public headings are still preserved below or in the support files. Notable imported sections: Table of Contents, 1. Array Operations, 2. Object Transformations, 3. Data Normalization, 4. Grouping and Aggregation, 5. Null-Safe Access. ## When to Use This Skill Use this section as the trigger filter. It should make the activation boundary explicit before the operator loads files, runs commands, or opens a pull request. - You need to transform arrays, objects, grouped data, or nested values in TypeScript. - The task involves reshaping API responses, null-safe access, aggregation, or normalization. - You want practical functional patterns for everyday data work instead of low-level loops. - Simple transformations: .map(), .filter(), .reduce() are perfectly good - No composition needed: You're doing a one-off transformation - Team familiarity: Everyone knows native methods ## Operating Table | Situation | Start here | Why it matters | | --- | --- | --- | | First-time use | `metadata.json` | Confirms repository, branch, commit, and imported path through the `external_source` block before touching the copied workflow | | Provenance review | `ORIGIN.md` | Gives reviewers a plain-language audit trail for the imported source | | Workflow execution | `SKILL.md` | Starts with the smallest copied file that materially changes execution | | Supporting context | `SKILL.md` | Adds the next most relevant copied source file without loading the entire package | | Handoff decision | `## Related Skills` | Helps the operator switch to a stronger native skill when the task drifts | ## Workflow This workflow is intentionally editorial and operational at the same time. It keeps the imported source useful to the operator while still satisfying the public intake standards that feed the downstream enhancer flow. 1. Confirm the user goal, the scope of the imported workflow, and whether this skill is still the right router for the task. 2. Read the overview and provenance files before loading any copied upstream support files. 3. Load only the references, examples, prompts, or scripts that materially change the outcome for the current request. 4. Execute the upstream workflow while keeping provenance and source boundaries explicit in the working notes. 5. Validate the result against the upstream expectations and the evidence you can point to in the copied files. 6. Escalate or hand off to a related skill when the work moves out of this imported workflow's center of gravity. 7. Before merge or closure, record what was used, what changed, and what the reviewer still needs to verify. ### Imported Workflow Notes #### Imported: Summary | Task | Imperative | Functional | Recommendation | |------|-----------|------------|----------------| | Transform array elements | for loop with push | `.map()` | Use map | | Filter array | for loop with condition | `.filter()` | Use filter | | Accumulate values | for loop with accumulator | `.reduce()` | Use reduce for complex, loop for simple | | Group by key | for loop with object | `groupBy` utility | Create reusable utility | | Pick object fields | manual property copy | `pick` utility | Use spread for one-off, utility for repeated | | Merge objects | property-by-property | spread syntax | Use spread | | Deep merge | nested conditionals | recursive utility | Use utility or library | | Null-safe access | `if (x && x.y)` | `?.` or Option | Use `?.` for simple, Option for composition | | Normalize API data | nested loops | extraction functions | Break into composable functions | **The functional approach is better when:** - You need to compose operations - You want reusable transformations - You value explicit data flow over implicit state - Type safety for missing values matters **The imperative approach is acceptable when:** - The transformation is a one-off - The logic is simple and linear - Performance is critical and you've measured - The team is more comfortable with it #### Imported: Table of Contents 1. [Array Operations](#1-array-operations) 2. [Object Transformations](#2-object-transformations) 3. [Data Normalization](#3-data-normalization) 4. [Grouping and Aggregation](#4-grouping-and-aggregation) 5. [Null-Safe Access](#5-null-safe-access) 6. [Real-World Examples](#6-real-world-examples) 7. [When to Use What](#7-when-to-use-what) --- ## Examples ### Example 1: Ask for the upstream workflow directly ```text Use @fp-data-transforms to handle . Start from the copied upstream workflow, load only the files that change the outcome, and keep provenance visible in the answer. ``` **Explanation:** This is the safest starting point when the operator needs the imported workflow, but not the entire repository. ### Example 2: Ask for a provenance-grounded review ```text Review @fp-data-transforms against metadata.json and ORIGIN.md, then explain which copied upstream files you would load first and why. ``` **Explanation:** Use this before review or troubleshooting when you need a precise, auditable explanation of origin and file selection. ### Example 3: Narrow the copied support files before execution ```text Use @fp-data-transforms for . Load only the copied references, examples, or scripts that change the outcome, and name the files explicitly before proceeding. ``` **Explanation:** This keeps the skill aligned with progressive disclosure instead of loading the whole copied package by default. ### Example 4: Build a reviewer packet ```text Review @fp-data-transforms using the copied upstream files plus provenance, then summarize any gaps before merge. ``` **Explanation:** This is useful when the PR is waiting for human review and you want a repeatable audit packet. ### Imported Usage Notes #### Imported: 6. Real-World Examples ### Example 1: Transform API Response to UI-Ready Data ```typescript // API response interface ApiOrder { order_id: string; customer: { id: string; full_name: string; }; line_items: Array<{ product_id: string; product_name: string; qty: number; unit_price: number; }>; order_date: string; status: 'pending' | 'processing' | 'shipped' | 'delivered'; } // What the UI needs interface OrderSummary { id: string; customerName: string; itemCount: number; total: number; formattedTotal: string; date: string; statusLabel: string; statusColor: string; } // Transformation const STATUS_CONFIG: Record = { pending: { label: 'Pending', color: 'yellow' }, processing: { label: 'Processing', color: 'blue' }, shipped: { label: 'Shipped', color: 'purple' }, delivered: { label: 'Delivered', color: 'green' }, }; const formatCurrency = (cents: number): string => `$${(cents / 100).toFixed(2)}`; const formatDate = (iso: string): string => new Date(iso).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', }); const toOrderSummary = (order: ApiOrder): OrderSummary => { const total = order.line_items.reduce( (sum, item) => sum + item.qty * item.unit_price, 0 ); const status = STATUS_CONFIG[order.status] ?? STATUS_CONFIG.pending; return { id: order.order_id, customerName: order.customer.full_name, itemCount: order.line_items.reduce((sum, item) => sum + item.qty, 0), total, formattedTotal: formatCurrency(total), date: formatDate(order.order_date), statusLabel: status.label, statusColor: status.color, }; }; // Transform all orders const toOrderSummaries = (orders: ApiOrder[]): OrderSummary[] => orders.map(toOrderSummary); ``` ### Example 2: Merge User Settings with Defaults ```typescript interface AppSettings { theme: { mode: 'light' | 'dark' | 'system'; primaryColor: string; fontSize: 'small' | 'medium' | 'large'; }; notifications: { email: boolean; push: boolean; sms: boolean; frequency: 'immediate' | 'daily' | 'weekly'; }; privacy: { showProfile: boolean; showActivity: boolean; allowAnalytics: boolean; }; } type DeepPartial = { [P in keyof T]?: T[P] extends object ? DeepPartial : T[P]; }; const DEFAULT_SETTINGS: AppSettings = { theme: { mode: 'system', primaryColor: '#007bff', fontSize: 'medium', }, notifications: { email: true, push: true, sms: false, frequency: 'immediate', }, privacy: { showProfile: true, showActivity: true, allowAnalytics: true, }, }; const deepMergeSettings = ( defaults: AppSettings, user: DeepPartial ): AppSettings => ({ theme: { ...defaults.theme, ...user.theme }, notifications: { ...defaults.notifications, ...user.notifications }, privacy: { ...defaults.privacy, ...user.privacy }, }); // Usage const userPreferences: DeepPartial = { theme: { mode: 'dark' }, notifications: { sms: true, frequency: 'daily' }, }; const finalSettings = deepMergeSettings(DEFAULT_SETTINGS, userPreferences); ``` ### Example 3: Group Orders by Customer with Totals ```typescript interface Order { id: string; customerId: string; customerName: string; items: Array<{ name: string; price: number; quantity: number }>; date: string; } interface CustomerOrderSummary { customerId: string; customerName: string; orderCount: number; totalSpent: number; orders: Order[]; } const calculateOrderTotal = (order: Order): number => order.items.reduce((sum, item) => sum + item.price * item.quantity, 0); const groupOrdersByCustomer = (orders: Order[]): CustomerOrderSummary[] => { const grouped = groupBy((order: Order) => order.customerId)(orders); return Object.entries(grouped).map(([customerId, customerOrders]) => ({ customerId, customerName: customerOrders[0].customerName, orderCount: customerOrders.length, totalSpent: customerOrders.reduce( (sum, order) => sum + calculateOrderTotal(order), 0 ), orders: customerOrders, })); }; ``` ### Example 4: Safely Access Deeply Nested Config ```typescript interface AppConfig { services?: { api?: { endpoints?: { users?: string; orders?: string; products?: string; }; auth?: { type?: 'bearer' | 'basic' | 'oauth'; token?: string; }; }; database?: { primary?: { host?: string; port?: number; name?: string; }; }; }; } import * as O from 'fp-ts/Option'; import { pipe } from 'fp-ts/function'; // Create a type-safe config accessor const getConfigValue = ( config: AppConfig, path: (config: AppConfig) => T | undefined, defaultValue: T ): T => path(config) ?? defaultValue; // Usage with optional chaining (simplest) const apiUsersEndpoint = getConfigValue( config, c => c.services?.api?.endpoints?.users, '/api/users' ); // For more complex scenarios, use Option const getEndpoint = (config: AppConfig, name: 'users' | 'orders' | 'products'): string => pipe( O.fromNullable(config.services), O.flatMap(s => O.fromNullable(s.api)), O.flatMap(a => O.fromNullable(a.endpoints)), O.flatMap(e => O.fromNullable(e[name])), O.getOrElse(() => `/api/${name}`) ); // Reusable pattern for multiple values const getDbConfig = (config: AppConfig) => ({ host: config.services?.database?.primary?.host ?? 'localhost', port: config.services?.database?.primary?.port ?? 5432, name: config.services?.database?.primary?.name ?? 'app', }); ``` --- ## Best Practices Treat the generated public skill as a reviewable packaging layer around the upstream repository. The goal is to keep provenance explicit and load only the copied source material that materially improves execution. - Keep the imported skill grounded in the upstream repository; do not invent steps that the source material cannot support. - Prefer the smallest useful set of support files so the workflow stays auditable and fast to review. - Keep provenance, source commit, and imported file paths visible in notes and PR descriptions. - Point directly at the copied upstream files that justify the workflow instead of relying on generic review boilerplate. - Treat generated examples as scaffolding; adapt them to the concrete task before execution. - Route to a stronger native skill when architecture, debugging, design, or security concerns become dominant. ## Troubleshooting ### Problem: The operator skipped the imported context and answered too generically **Symptoms:** The result ignores the upstream workflow in `plugins/antigravity-awesome-skills-claude/skills/fp-data-transforms`, fails to mention provenance, or does not use any copied source files at all. **Solution:** Re-open `metadata.json`, `ORIGIN.md`, and the most relevant copied upstream files. Check the `external_source` block first, then restate the provenance before continuing. ### Problem: The imported workflow feels incomplete during review **Symptoms:** Reviewers can see the generated `SKILL.md`, but they cannot quickly tell which references, examples, or scripts matter for the current task. **Solution:** Point at the exact copied references, examples, scripts, or assets that justify the path you took. If the gap is still real, record it in the PR instead of hiding it. ### Problem: The task drifted into a different specialization **Symptoms:** The imported skill starts in the right place, but the work turns into debugging, architecture, design, security, or release orchestration that a native skill handles better. **Solution:** Use the related skills section to hand off deliberately. Keep the imported provenance visible so the next skill inherits the right context instead of starting blind. ## Related Skills - `@00-andruia-consultant` - Use when the work is better handled by that native specialization after this imported skill establishes context. - `@00-andruia-consultant-v2` - Use when the work is better handled by that native specialization after this imported skill establishes context. - `@10-andruia-skill-smith` - Use when the work is better handled by that native specialization after this imported skill establishes context. - `@10-andruia-skill-smith-v2` - Use when the work is better handled by that native specialization after this imported skill establishes context. ## Additional Resources Use this support matrix and the linked files below as the operator packet for this imported skill. They should reflect real copied source material, not generic scaffolding. | Resource family | What it gives the reviewer | Example path | | --- | --- | --- | | `references` | copied reference notes, guides, or background material from upstream | `references/n/a` | | `examples` | worked examples or reusable prompts copied from upstream | `examples/n/a` | | `scripts` | upstream helper scripts that change execution or validation | `scripts/n/a` | | `agents` | routing or delegation notes that are genuinely part of the imported package | `agents/n/a` | | `assets` | supporting assets or schemas copied from the source package | `assets/n/a` | ### Imported Reference Notes #### Imported: 1. Array Operations Array operations are the bread and butter of data transformation. Let's replace verbose loops with expressive, chainable operations. ### Map: Transform Every Element **The Task**: Convert an array of prices from cents to dollars. #### Imperative Approach ```typescript const pricesInCents = [999, 1499, 2999, 4999]; function convertToDollars(prices: number[]): number[] { const result: number[] = []; for (let i = 0; i < prices.length; i++) { result.push(prices[i] / 100); } return result; } const dollars = convertToDollars(pricesInCents); // [9.99, 14.99, 29.99, 49.99] ``` #### Functional Approach ```typescript const pricesInCents = [999, 1499, 2999, 4999]; const toDollars = (cents: number): number => cents / 100; const dollars = pricesInCents.map(toDollars); // [9.99, 14.99, 29.99, 49.99] ``` **Why functional is better here**: The intent is immediately clear. `map` says "transform each element." The transformation logic (`toDollars`) is named and reusable. No index management, no manual array building. ### Filter: Keep What Matches **The Task**: Get all active users from a list. #### Imperative Approach ```typescript interface User { id: string; name: string; isActive: boolean; } function getActiveUsers(users: User[]): User[] { const result: User[] = []; for (const user of users) { if (user.isActive) { result.push(user); } } return result; } ``` #### Functional Approach ```typescript const isActive = (user: User): boolean => user.isActive; const activeUsers = users.filter(isActive); // Or inline for simple predicates const activeUsers = users.filter(user => user.isActive); ``` **Why functional is better here**: The predicate (`isActive`) is separated from the iteration logic. You can reuse, test, and compose predicates independently. ### Reduce: Accumulate Into Something New **The Task**: Calculate the total price of items in a cart. #### Imperative Approach ```typescript interface CartItem { name: string; price: number; quantity: number; } function calculateTotal(items: CartItem[]): number { let total = 0; for (const item of items) { total += item.price * item.quantity; } return total; } ``` #### Functional Approach ```typescript const calculateTotal = (items: CartItem[]): number => items.reduce( (total, item) => total + item.price * item.quantity, 0 ); // Or break out the line total calculation const lineTotal = (item: CartItem): number => item.price * item.quantity; const calculateTotal = (items: CartItem[]): number => items.map(lineTotal).reduce((a, b) => a + b, 0); ``` **Honest assessment**: For simple sums, the imperative loop is actually quite readable. The functional version shines when you need to compose the accumulation with other transformations, or when the reduction logic is complex enough to benefit from being named. ### Chaining: Combine Operations **The Task**: Get the names of all active premium users, sorted alphabetically. #### Imperative Approach ```typescript interface User { id: string; name: string; isActive: boolean; tier: 'free' | 'premium'; } function getActivePremiumNames(users: User[]): string[] { const result: string[] = []; for (const user of users) { if (user.isActive && user.tier === 'premium') { result.push(user.name); } } result.sort((a, b) => a.localeCompare(b)); return result; } ``` #### Functional Approach ```typescript const getActivePremiumNames = (users: User[]): string[] => users .filter(user => user.isActive) .filter(user => user.tier === 'premium') .map(user => user.name) .sort((a, b) => a.localeCompare(b)); // Or with named predicates for reuse const isActive = (user: User): boolean => user.isActive; const isPremium = (user: User): boolean => user.tier === 'premium'; const getName = (user: User): string => user.name; const alphabetically = (a: string, b: string): number => a.localeCompare(b); const getActivePremiumNames = (users: User[]): string[] => users .filter(isActive) .filter(isPremium) .map(getName) .sort(alphabetically); ``` **Why functional is better here**: Each step in the chain has a single responsibility. You can read the transformation as a series of steps: "filter active, filter premium, get names, sort." Adding or removing a step is trivial. ### Using fp-ts Array Module fp-ts provides additional array utilities with better composition support: ```typescript import * as A from 'fp-ts/Array'; import * as O from 'fp-ts/Option'; import { pipe } from 'fp-ts/function'; // Safe head (first element) const first = pipe( [1, 2, 3], A.head ); // Some(1) const firstOfEmpty = pipe( [] as number[], A.head ); // None // Safe lookup by index const third = pipe( ['a', 'b', 'c', 'd'], A.lookup(2) ); // Some('c') // Find with predicate const found = pipe( users, A.findFirst(user => user.id === 'abc123') ); // Option // Partition into two groups const [inactive, active] = pipe( users, A.partition(user => user.isActive) ); // Take first N elements const topThree = pipe( sortedScores, A.takeLeft(3) ); // Unique values const uniqueTags = pipe( allTags, A.uniq({ equals: (a, b) => a === b }) ); ``` --- #### Imported: 2. Object Transformations Objects need reshaping constantly: picking fields, omitting sensitive data, merging settings, and updating nested values. ### Pick: Select Specific Fields **The Task**: Extract only the public fields from a user object. #### Imperative Approach ```typescript interface User { id: string; name: string; email: string; passwordHash: string; internalNotes: string; } function getPublicUser(user: User): { id: string; name: string; email: string } { return { id: user.id, name: user.name, email: user.email, }; } ``` #### Functional Approach ```typescript // Generic pick utility const pick = ( keys: K[] ) => (obj: T): Pick => keys.reduce( (result, key) => { result[key] = obj[key]; return result; }, {} as Pick ); const getPublicUser = pick(['id', 'name', 'email']); const publicUser = getPublicUser(user); ``` **Why functional is better here**: The `pick` utility is reusable across your codebase. Type safety ensures you can only pick keys that exist. ### Omit: Remove Specific Fields **The Task**: Remove sensitive fields before logging. #### Imperative Approach ```typescript function sanitizeForLogging(user: User): Omit { const { passwordHash, internalNotes, ...safe } = user; return safe; } ``` #### Functional Approach ```typescript // Generic omit utility const omit = ( keys: K[] ) => (obj: T): Omit => { const result = { ...obj }; for (const key of keys) { delete result[key]; } return result as Omit; }; const sanitizeForLogging = omit([ 'passwordHash', 'internalNotes', ]); ``` **Honest assessment**: For one-off omits, destructuring (the imperative approach) is perfectly fine and very readable. The functional `omit` utility pays off when you have many such transformations or need to compose them. ### Merge: Combine Objects **The Task**: Merge user settings with defaults. #### Imperative Approach ```typescript interface Settings { theme: 'light' | 'dark'; fontSize: number; notifications: boolean; language: string; } function mergeSettings( defaults: Settings, userSettings: Partial ): Settings { return { theme: userSettings.theme !== undefined ? userSettings.theme : defaults.theme, fontSize: userSettings.fontSize !== undefined ? userSettings.fontSize : defaults.fontSize, notifications: userSettings.notifications !== undefined ? userSettings.notifications : defaults.notifications, language: userSettings.language !== undefined ? userSettings.language : defaults.language, }; } ``` #### Functional Approach ```typescript const mergeSettings = ( defaults: Settings, userSettings: Partial ): Settings => ({ ...defaults, ...userSettings, }); // Usage const defaults: Settings = { theme: 'light', fontSize: 14, notifications: true, language: 'en', }; const userPrefs: Partial = { theme: 'dark', fontSize: 16, }; const finalSettings = mergeSettings(defaults, userPrefs); // { theme: 'dark', fontSize: 16, notifications: true, language: 'en' } ``` **Why functional is better here**: Spread syntax is concise and handles any number of keys. Later spreads override earlier ones, giving you natural "defaults with overrides" behavior. ### Deep Merge: Nested Object Combination **The Task**: Merge nested configuration objects. #### Imperative Approach ```typescript interface Config { api: { baseUrl: string; timeout: number; retries: number; }; ui: { theme: string; animations: boolean; }; } function deepMerge( target: Config, source: Partial ): Config { const result = { ...target }; if (source.api) { result.api = { ...target.api, ...source.api }; } if (source.ui) { result.ui = { ...target.ui, ...source.ui }; } return result; } ``` #### Functional Approach ```typescript // Generic deep merge for one level of nesting const deepMerge = >( target: T, source: { [K in keyof T]?: Partial } ): T => { const result = { ...target }; for (const key of Object.keys(source) as Array) { if (source[key] !== undefined) { result[key] = { ...target[key], ...source[key] }; } } return result; }; // Usage const defaultConfig: Config = { api: { baseUrl: 'https://api.example.com', timeout: 5000, retries: 3 }, ui: { theme: 'light', animations: true }, }; const customConfig = deepMerge(defaultConfig, { api: { timeout: 10000 }, ui: { theme: 'dark' }, }); // api.baseUrl preserved, api.timeout overridden // ui.theme overridden, ui.animations preserved ``` ### Immutable Updates: Change Nested Values **The Task**: Update a deeply nested value without mutation. #### Imperative (Mutating) Approach ```typescript interface State { user: { profile: { settings: { theme: string; }; }; }; } function updateTheme(state: State, newTheme: string): void { state.user.profile.settings.theme = newTheme; // Mutation! } ``` #### Functional (Immutable) Approach ```typescript // Manual spread nesting const updateTheme = (state: State, newTheme: string): State => ({ ...state, user: { ...state.user, profile: { ...state.user.profile, settings: { ...state.user.profile.settings, theme: newTheme, }, }, }, }); // With a lens-like helper const updatePath = ( obj: T, path: string[], value: V ): T => { if (path.length === 0) return value as unknown as T; const [head, ...rest] = path; return { ...obj, [head]: updatePath((obj as Record)[head], rest, value), } as T; }; const newState = updatePath(state, ['user', 'profile', 'settings', 'theme'], 'dark'); ``` **Honest assessment**: The spread nesting is verbose but explicit. For deeply nested updates, consider using a library like `immer` or fp-ts lenses. The verbosity of the functional approach is the price of immutability. --- #### Imported: 3. Data Normalization API responses rarely match the shape your app needs. Normalization transforms nested, denormalized data into flat, indexed structures. ### API Response to App State **The Task**: Transform a nested API response into a normalized state. #### API Response (What You Get) ```typescript interface ApiResponse { orders: Array<{ id: string; customerId: string; customerName: string; customerEmail: string; items: Array<{ productId: string; productName: string; quantity: number; price: number; }>; total: number; status: string; }>; } ``` #### App State (What You Need) ```typescript interface NormalizedState { orders: { byId: Record; allIds: string[]; }; customers: { byId: Record; allIds: string[]; }; products: { byId: Record; allIds: string[]; }; } interface Order { id: string; customerId: string; itemIds: string[]; total: number; status: string; } interface Customer { id: string; name: string; email: string; } interface Product { id: string; name: string; price: number; } ``` #### Imperative Approach ```typescript function normalizeApiResponse(response: ApiResponse): NormalizedState { const state: NormalizedState = { orders: { byId: {}, allIds: [] }, customers: { byId: {}, allIds: [] }, products: { byId: {}, allIds: [] }, }; for (const order of response.orders) { // Extract customer if (!state.customers.byId[order.customerId]) { state.customers.byId[order.customerId] = { id: order.customerId, name: order.customerName, email: order.customerEmail, }; state.customers.allIds.push(order.customerId); } // Extract products and build item IDs const itemIds: string[] = []; for (const item of order.items) { if (!state.products.byId[item.productId]) { state.products.byId[item.productId] = { id: item.productId, name: item.productName, price: item.price, }; state.products.allIds.push(item.productId); } itemIds.push(item.productId); } // Add normalized order state.orders.byId[order.id] = { id: order.id, customerId: order.customerId, itemIds, total: order.total, status: order.status, }; state.orders.allIds.push(order.id); } return state; } ``` #### Functional Approach ```typescript import { pipe } from 'fp-ts/function'; import * as A from 'fp-ts/Array'; import * as R from 'fp-ts/Record'; // Helper to create normalized collection interface NormalizedCollection { byId: Record; allIds: string[]; } const createNormalizedCollection = ( items: T[] ): NormalizedCollection => ({ byId: pipe( items, A.reduce({} as Record, (acc, item) => ({ ...acc, [item.id]: item, })) ), allIds: items.map(item => item.id), }); // Extract entities const extractCustomers = (orders: ApiResponse['orders']): Customer[] => pipe( orders, A.map(order => ({ id: order.customerId, name: order.customerName, email: order.customerEmail, })), A.uniq({ equals: (a, b) => a.id === b.id }) ); const extractProducts = (orders: ApiResponse['orders']): Product[] => pipe( orders, A.flatMap(order => order.items), A.map(item => ({ id: item.productId, name: item.productName, price: item.price, })), A.uniq({ equals: (a, b) => a.id === b.id }) ); const extractOrders = (orders: ApiResponse['orders']): Order[] => orders.map(order => ({ id: order.id, customerId: order.customerId, itemIds: order.items.map(item => item.productId), total: order.total, status: order.status, })); // Compose into final normalization const normalizeApiResponse = (response: ApiResponse): NormalizedState => ({ orders: createNormalizedCollection(extractOrders(response.orders)), customers: createNormalizedCollection(extractCustomers(response.orders)), products: createNormalizedCollection(extractProducts(response.orders)), }); ``` **Why functional is better here**: Each extraction is independent and testable. The `createNormalizedCollection` helper is reusable. Adding a new entity type means adding one new extraction function. ### Transform API Response to UI-Ready Data **The Task**: Convert API data to what your components need. ```typescript // API gives you this interface ApiUser { user_id: string; first_name: string; last_name: string; email_address: string; created_at: string; // ISO string avatar_url: string | null; } // Components need this interface DisplayUser { id: string; fullName: string; email: string; memberSince: string; // "Jan 2024" avatarUrl: string; // With fallback } ``` #### Functional Approach ```typescript const formatDate = (isoString: string): string => { const date = new Date(isoString); return date.toLocaleDateString('en-US', { month: 'short', year: 'numeric' }); }; const DEFAULT_AVATAR = 'https://example.com/default-avatar.png'; const toDisplayUser = (apiUser: ApiUser): DisplayUser => ({ id: apiUser.user_id, fullName: `${apiUser.first_name} ${apiUser.last_name}`, email: apiUser.email_address, memberSince: formatDate(apiUser.created_at), avatarUrl: apiUser.avatar_url ?? DEFAULT_AVATAR, }); // Transform array of users const toDisplayUsers = (apiUsers: ApiUser[]): DisplayUser[] => apiUsers.map(toDisplayUser); ``` --- #### Imported: 4. Grouping and Aggregation Grouping and aggregating data is essential for reports, dashboards, and analytics. ### GroupBy: Organize by Key **The Task**: Group orders by customer. #### Imperative Approach ```typescript interface Order { id: string; customerId: string; total: number; date: string; } function groupByCustomer(orders: Order[]): Record { const result: Record = {}; for (const order of orders) { if (!result[order.customerId]) { result[order.customerId] = []; } result[order.customerId].push(order); } return result; } ``` #### Functional Approach ```typescript // Generic groupBy utility const groupBy = ( getKey: (item: T) => K ) => (items: T[]): Record => items.reduce( (groups, item) => { const key = getKey(item); return { ...groups, [key]: [...(groups[key] || []), item], }; }, {} as Record ); // Usage const groupByCustomer = groupBy(order => order.customerId); const ordersByCustomer = groupByCustomer(orders); // Or inline const ordersByStatus = groupBy((order: Order) => order.status)(orders); ``` **Using fp-ts NonEmptyArray.groupBy**: ```typescript import * as NEA from 'fp-ts/NonEmptyArray'; import { pipe } from 'fp-ts/function'; // NEA.groupBy guarantees non-empty arrays in result const ordersByCustomer = pipe( orders as NEA.NonEmptyArray, // Must be non-empty NEA.groupBy(order => order.customerId) ); // Record> ``` ### CountBy: Count Occurrences **The Task**: Count orders by status. #### Imperative Approach ```typescript function countByStatus(orders: Order[]): Record { const counts: Record = {}; for (const order of orders) { counts[order.status] = (counts[order.status] || 0) + 1; } return counts; } ``` #### Functional Approach ```typescript // Generic countBy utility const countBy = ( getKey: (item: T) => K ) => (items: T[]): Record => items.reduce( (counts, item) => { const key = getKey(item); return { ...counts, [key]: (counts[key] || 0) + 1, }; }, {} as Record ); // Usage const orderCountByStatus = countBy((order: Order) => order.status)(orders); // { pending: 5, shipped: 12, delivered: 8 } ``` ### SumBy: Aggregate Numeric Values **The Task**: Calculate total revenue per product category. #### Imperative Approach ```typescript interface Sale { productId: string; category: string; amount: number; } function sumByCategory(sales: Sale[]): Record { const totals: Record = {}; for (const sale of sales) { totals[sale.category] = (totals[sale.category] || 0) + sale.amount; } return totals; } ``` #### Functional Approach ```typescript // Generic sumBy utility const sumBy = ( getKey: (item: T) => K, getValue: (item: T) => number ) => (items: T[]): Record => items.reduce( (totals, item) => { const key = getKey(item); return { ...totals, [key]: (totals[key] || 0) + getValue(item), }; }, {} as Record ); // Usage const revenueByCategory = sumBy( (sale: Sale) => sale.category, (sale: Sale) => sale.amount )(sales); // { electronics: 15000, clothing: 8500, books: 3200 } ``` ### Complex Aggregation Example **The Task**: Calculate totals from line items with quantity and unit price. ```typescript interface LineItem { productId: string; productName: string; quantity: number; unitPrice: number; } interface Invoice { id: string; lineItems: LineItem[]; taxRate: number; } ``` #### Functional Approach ```typescript const lineTotal = (item: LineItem): number => item.quantity * item.unitPrice; const subtotal = (items: LineItem[]): number => items.reduce((sum, item) => sum + lineTotal(item), 0); const calculateTax = (amount: number, rate: number): number => amount * rate; const calculateInvoiceTotal = (invoice: Invoice): { subtotal: number; tax: number; total: number; } => { const sub = subtotal(invoice.lineItems); const tax = calculateTax(sub, invoice.taxRate); return { subtotal: sub, tax, total: sub + tax, }; }; // With fp-ts pipe for clarity import { pipe } from 'fp-ts/function'; const calculateInvoiceTotal = (invoice: Invoice) => { const sub = pipe( invoice.lineItems, A.map(lineTotal), A.reduce(0, (a, b) => a + b) ); return { subtotal: sub, tax: sub * invoice.taxRate, total: sub * (1 + invoice.taxRate), }; }; ``` --- #### Imported: 5. Null-Safe Access Stop writing `if (x && x.y && x.y.z)`. Safely navigate nested structures without runtime errors. ### The Problem ```typescript interface Config { database?: { connection?: { host?: string; port?: number; }; pool?: { max?: number; }; }; features?: { experimental?: { enabled?: boolean; }; }; } ``` #### Imperative (Verbose) Approach ```typescript function getDatabaseHost(config: Config): string { if ( config.database && config.database.connection && config.database.connection.host ) { return config.database.connection.host; } return 'localhost'; } ``` #### Optional Chaining (Modern TypeScript) ```typescript const getDatabaseHost = (config: Config): string => config.database?.connection?.host ?? 'localhost'; ``` **Honest assessment**: For simple access patterns, optional chaining (`?.`) is perfect. It's built into the language and very readable. Use fp-ts Option when you need to compose operations on potentially missing values. ### When to Use Option Instead Use fp-ts Option when: - You need to chain multiple operations on potentially missing values - You want to distinguish "missing" from other falsy values - You're building a pipeline of transformations ```typescript import * as O from 'fp-ts/Option'; import { pipe } from 'fp-ts/function'; // Safe property access that returns Option const prop = (key: K) => (obj: T | null | undefined): O.Option => obj != null && key in obj ? O.some(obj[key] as T[K]) : O.none; // Chain accesses with flatMap const getDatabaseHost = (config: Config): O.Option => pipe( O.some(config), O.flatMap(prop('database')), O.flatMap(prop('connection')), O.flatMap(prop('host')) ); // Extract with default const host = pipe( getDatabaseHost(config), O.getOrElse(() => 'localhost') ); ``` ### Safe Array Access ```typescript import * as A from 'fp-ts/Array'; import * as O from 'fp-ts/Option'; import { pipe } from 'fp-ts/function'; // Imperative: throws if array is empty const first = items[0]; // Could be undefined! // Safe: returns Option const first = A.head(items); // Option // Get first item's name, or default const firstName = pipe( items, A.head, O.map(item => item.name), O.getOrElse(() => 'No items') ); // Safe lookup by index const third = pipe( items, A.lookup(2), O.map(item => item.name), O.getOrElse(() => 'Not found') ); ``` ### Safe Record/Dictionary Access ```typescript import * as R from 'fp-ts/Record'; import * as O from 'fp-ts/Option'; import { pipe } from 'fp-ts/function'; const users: Record = { 'user-1': { name: 'Alice', email: 'alice@example.com' }, 'user-2': { name: 'Bob', email: 'bob@example.com' }, }; // Imperative: could be undefined const user = users['user-3']; // User | undefined // Safe: returns Option const user = R.lookup('user-3')(users); // Option // Get user email or default const email = pipe( users, R.lookup('user-3'), O.map(u => u.email), O.getOrElse(() => 'unknown@example.com') ); ``` ### Combining Multiple Optional Values **The Task**: Get a user's display name, which requires both first and last name. ```typescript interface Profile { firstName?: string; lastName?: string; nickname?: string; } // Imperative function getDisplayName(profile: Profile): string { if (profile.firstName && profile.lastName) { return `${profile.firstName} ${profile.lastName}`; } if (profile.nickname) { return profile.nickname; } return 'Anonymous'; } // Functional with Option import * as O from 'fp-ts/Option'; import { pipe } from 'fp-ts/function'; const getDisplayName = (profile: Profile): string => pipe( // Try full name first O.Do, O.bind('first', () => O.fromNullable(profile.firstName)), O.bind('last', () => O.fromNullable(profile.lastName)), O.map(({ first, last }) => `${first} ${last}`), // Fall back to nickname O.alt(() => O.fromNullable(profile.nickname)), // Finally, default to Anonymous O.getOrElse(() => 'Anonymous') ); ``` --- #### Imported: Limitations - Use this skill only when the task clearly matches the scope described above. - Do not treat the output as a substitute for environment-specific validation, testing, or expert review. - Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.