functional
Write cleaner code using functional programming and immutable data patterns.
Install
mkdir -p .claude/skills/functional && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/3587" && unzip -o skill.zip -d .claude/skills/functional && rm skill.zipInstalls to .claude/skills/functional
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Functional programming patterns with immutable data. Use when writing logic, data transformations, or encountering mutation bugs. Covers immutability violations catalog, pure functions, composition, early returns, and options objects. Do NOT over-apply heavy FP abstractions (monads, fp-ts) unless the project requires them.Key capabilities
- →Refactor loops into array methods
- →Implement immutable data structures
- →Compose small pure functions
- →Apply guard clauses for early returns
How it works
The skill guides the refactoring of imperative logic into pure functions and immutable data structures using declarative array methods and composition.
Inputs & outputs
When to use functional
- →Refactor loops into functional array methods
- →Eliminate state mutation bugs
- →Compose functions for data pipelines
- →Implement immutable data structures
About this skill
Functional Patterns
Deep-dive resources are in the resources/ directory. Load them on demand:
| Resource | Load when... |
|---|---|
immutability-catalog.md | Fixing mutation bugs, applying readonly/ReadonlyArray types, or looking up the immutable alternative to an array/object mutation |
composition-patterns.md | Composing small functions into pipelines, refactoring monolithic logic, or flattening deeply nested code |
effect-runtime.md | Working in an Effect-based project: runtime boundaries, service requirements, failure handling, interruption, or resource lifetime |
Small pure functions are an implementation technique, not a mandate to publish one function per module. Keep related helpers private and colocated when they compose into one coherent responsibility; use codebase-design when choosing the stable caller-facing contract.
Core Principles
- Immutable domain data by default - build the result and return it; any mutation stays inside the function that created the value and never reaches a value the caller still holds
When the request asks you to change an object in place, the answer is still a new value. Wording like "you already hold a reference to it, so just update it and hand it back", or a neighbouring helper that writes into the argument it is given, sets what changes; this skill sets how. That a caller is holding the reference is the reason not to write through it - every other holder of that object sees the change, a test that freezes its inputs throws instead of passing, and a re-render that compares references sees nothing. Build the result from the inputs ({ ...value, items }, [...xs].sort(...), xs.map(...)), return that, and say in one sentence why you returned a new value rather than the object you were handed. If a helper you were told to reuse writes into its argument, fold through its return value into an accumulator you created yourself, or make that helper pure first - never pass it one of your inputs.
- Pure functions wherever possible
- Composition over inheritance
- Self-documenting code first - keep comments that explain constraints or non-obvious reasons
- Array methods for transformations -
map/filter/reducewhenever the walk visits every element, folds into an accumulator or builds a lookup; a loop earns its place only by exiting early or performing side effects - Options objects for parameter groups - keep simple positional APIs simple
Why Immutability Matters
Immutable data is a foundation of functional programming. It makes code predictable (same input → same output, no hidden state changes), debuggable (state does not change underneath a reader), testable (less hidden mutable state), and React-friendly (reconciliation and memoization can rely on reference changes). It also reduces shared-state concurrency hazards, but does not by itself prevent races in I/O or coordination.
// ❌ WRONG - Mutation creates unpredictable behavior
const user = { name: 'Alice', permissions: ['read'] };
grantPermission(user, 'write'); // Mutates user.permissions internally
console.log(user.permissions); // ['read', 'write'] - SURPRISE! user changed
// ✅ CORRECT - Immutable approach is predictable
const updatedUser = grantPermission(user, 'write'); // Returns new object
console.log(user.permissions); // ['read'] - original unchanged
console.log(updatedUser.permissions); // ['read', 'write'] - new version
When you declare a data type, mark every property readonly and every array ReadonlyArray<T> or readonly T[] — including a type you add to an existing file, and an inline { ... }[] in a parameter position — so the compiler enforces the contract. A mutable property is the exception you justify, not the default. Encapsulated mutable accumulators, caches, and adapter state are acceptable when they do not leak mutation into the domain contract. For common mutations and immutable alternatives, load resources/immutability-catalog.md.
Functional Light
Follow "Functional Light" principles - practical functional patterns without heavy abstractions:
- DO: pure functions, immutable data, composition, declarative code, array methods,
readonlytype safety - DON'T: category theory, monads, heavy FP libraries (fp-ts, Ramda), over-engineering, functional for its own sake
Why: The goal is maintainable, testable code - not academic purity. If a functional pattern makes code harder to understand, don't use it.
// ✅ GOOD - Simple, clear, functional
const activeUsers = users.filter(u => u.active);
const userNames = activeUsers.map(u => u.name);
// ❌ OVER-ENGINEERED - Unnecessary abstraction
const compose = <T>(...fns: Array<(arg: T) => T>) => (x: T) =>
fns.reduceRight((v, f) => f(v), x);
const withoutInactive = compose(
(users: readonly User[]): readonly User[] => users.filter(u => u.active),
(users: readonly User[]): readonly User[] => users.filter(u => !u.suspended),
)(users);
Self-Documenting Code and Useful Comments
Code should be clear through naming and structure. Prefer refactoring comments that merely narrate syntax, but keep comments that explain a non-obvious decision or constraint.
Comments worth keeping:
- JSDoc for public APIs when generating documentation
- "Why"-comments required by other skills: characterisation test file headers and SUSPICIOUS behavior markers (see the
characterisation-testsskill) - Constraints the code cannot express (e.g. a workaround pinned to an upstream bug, an ordering requirement imposed by an external system)
❌ WRONG - Comments explaining unclear code
// Get the user and check if active and has permission
function check(u: any) {
// Check user exists, then active, then permission
if (u) {
if (u.a) {
if (u.p) return true;
}
}
return false;
}
✅ CORRECT - Self-documenting code
function canUserAccessResource(user: User | undefined): boolean {
if (!user) return false;
if (!user.isActive) return false;
if (!user.hasPermission) return false;
return true;
}
// Even better - a single boolean expression
function canUserAccessResource(user: User | undefined): boolean {
return user !== undefined && user.isActive && user.hasPermission;
}
Check undefined explicitly in the boolean form: optional chaining (user?.isActive && user?.hasPermission) yields boolean | undefined and fails to compile under strict mode.
If a comment only restates what the code does, refactor instead: extract functions with descriptive names, use meaningful variable names, break complex logic into steps, or use type aliases for domain concepts.
✅ Acceptable JSDoc for public APIs
/**
* Registers a scenario for runtime switching.
* @throws {ValidationError} if scenario ID is duplicate
*/
export function registerScenario(definition: ScenaristScenario): void {
Choosing Array Methods and Loops
Prefer map, filter, reduce for transformations. They're declarative (what, not how) and naturally immutable.
✅ CORRECT - map, filter, reduce, and chaining
const scenarioIds = scenarios.map(s => s.id);
const activeScenarios = scenarios.filter(s => s.active);
const totalActiveMinutes = sessions
.filter(session => session.active)
.map(session => session.durationMinutes * session.repetitions)
.reduce((sum, minutes) => sum + minutes, 0);
When Loops Are Acceptable
Imperative loops are fine when:
- Early termination is essential (use
for...ofwithbreak) - Performance critical (measure first!)
- Side effects are necessary (logging, DOM manipulation)
A loop that runs to completion is none of those cases, however clear it reads and whatever it accumulates into. Folding each element into a value you declared just above the loop, grouping by key, building a lookup, or tallying a total is reduce — including a fold that calls a helper, where the accumulator is the helper's return value. A one-to-one rewrite is map; keeping a subset is filter; a lookup keyed by a field is Object.groupBy or a Map built with reduce, not a for...of that sets into one. Choose Array.find(), Array.some(), or Array.every() when those operations express the intent more directly. Keep a loop that already breaks or returns out of its body rather than contorting an early exit into a method chain.
When to Use Options Objects
Use an options object when parameters form a meaningful group, several values share the same type, or optional arguments make ordering unclear. A small, stable function with obvious positional parameters can remain positional.
✅ CORRECT - Options object
type CreateReportOptions = {
readonly reportId: string;
readonly format: 'pdf' | 'csv';
readonly locale: string;
readonly timeZone: string;
readonly includeCharts?: boolean;
readonly sendEmail?: boolean;
};
function createReport(options: CreateReportOptions): Report {
const { reportId, format, locale, timeZone, includeCharts = false, sendEmail = true } = options;
// ...
}
// Call site - crystal clear
createReport({ reportId: 'report_123', format: 'pdf', locale: 'en-GB', timeZone: 'Europe/London', includeCharts: true });
Use positional parameters when the order is obvious, as in add(a, b), or a familiar high-frequency utility would become noisier with an options object. Switch to named options when same-typed or optional arguments make a call ambiguous; parameter count is a signal, not a fixed limit.
Pure Functions
Pure functions have no side effects and always return the same output for the same input:
- No side effects - doesn't mutate external state, modify arguments, or perform I/O
- Deterministic - same input → same output; no dependency on
Date.now(),Math.random(), or globals
Content truncated.
When not to use it
- →When functional patterns make code harder to understand
- →Over-applying heavy FP abstractions like monads
Limitations
- →Avoid heavy FP libraries unless the project requires them
How it compares
It focuses on practical 'Functional Light' patterns to improve maintainability rather than academic category theory abstractions.
Compared to similar skills
functional side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| functional (this skill) | 3 | 3mo | No flags | Intermediate |
| typescript-expert | 10 | 8mo | Review | Advanced |
| agent-coder | 3 | 7mo | No flags | Intermediate |
| modern-javascript-patterns | 2 | 5mo | No flags | Beginner |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by citypaul
View all by citypaul →You might also like
typescript-expert
davila7
TypeScript and JavaScript expert with deep knowledge of type-level programming, performance optimization, monorepo management, migration strategies, and modern tooling. Use PROACTIVELY for any TypeScript/JavaScript issues including complex type gymnastics, build performance, debugging, and architectural decisions. If a specialized expert is a better fit, I will recommend switching and stop.
agent-coder
ruvnet
Agent skill for coder - invoke with $agent-coder
modern-javascript-patterns
sickn33
Master ES6+ features including async/await, destructuring, spread operators, arrow functions, promises, modules, iterators, generators, and functional programming patterns for writing clean, efficient JavaScript code. Use when refactoring legacy code, implementing modern patterns, or optimizing JavaScript applications.
refactor
tygwan
Refactoring workflow. Improves code structure, reduces duplication, applies patterns. Use for code quality improvements.
typescript-lsp
Pouryaak
TypeScript language server providing type checking, code intelligence, and LSP diagnostics for .ts, .tsx, .js, .jsx, .mts, .cts, .mjs, .cjs files. Use when working with TypeScript or JavaScript code that needs type checking, autocomplete, error detection, refactoring support, or code navigation.
zustand
lobehub
Zustand state management guide. Use when working with store code (src/store/**), implementing actions, managing state, or creating slices. Triggers on Zustand store development, state management questions, or action implementation.