Got it! You prefer a more functional, abstraction-oriented style rather than the classic OOP “repository/service” pattern. That's totally valid (and a great style for many domains)!
Why not just use List<T>?
Actually, you can! Many functional programmers do exactly that. They model data structures as immutable, generic, and reusable components—like lists, trees, monads, functors.
Here’s an example of a purely functional, immutable generic List<T> in TypeScript that you can use to manage users (or anything):
// Immutable linked list type
type List<T> = Nil | Cons<T>;
interface Nil {
readonly _tag: "Nil";
}
interface Cons<T> {
readonly _tag: "Cons";
readonly head: T;
readonly tail: List<T>;
}
const Nil: Nil = { _tag: "Nil" };
const Cons = <T>(head: T, tail: List<T> = Nil): Cons<T> => ({ _tag: "Cons", head, tail });
// Functional helpers:
// fold (reduce) over List
const fold = <T, R>(list: List<T>, init: R, f: (acc: R, val: T) => R): R => {
switch (list._tag) {
case "Nil":
return init;
case "Cons":
return fold(list.tail, f(init, list.head), f);
}
};
// map over List
const map = <T, U>(list: List<T>, f: (val: T) => U): List<U> => {
switch (list._tag) {
case "Nil":
return Nil;
case "Cons":
return Cons(f(list.head), map(list.tail, f));
}
};
// filter over List
const filter = <T>(list: List<T>, predicate: (val: T) => boolean): List<T> => {
switch (list._tag) {
case "Nil":
return Nil;
case "Cons":
const filteredTail = filter(list.tail, predicate);
return predicate(list.head) ? Cons(list.head, filteredTail) : filteredTail;
}
};
// Example usage:
interface User {
id: string;
name: string;
email: string;
}
// Create empty list of users
let users: List<User> = Nil;
// Add a new user (prepend immutably)
const addUser = (list: List<User>, user: User): List<User> => Cons(user, list);
// Find user by id
const findUserById = (list: List<User>, id: string): User | null =>
fold(
list,
null as User | null,
(acc, user) => (acc !== null ? acc : user.id === id ? user : null),
);
// Example
users = addUser(users, { id: "1", name: "Alice", email: "alice@example.com" });
users = addUser(users, { id: "2", name: "Bob", email: "bob@example.com" });
console.log(findUserById(users, "2")); // prints bob's user
// Map example: uppercase names
const shoutUsers = map(users, (u) => ({ ...u, name: u.name.toUpperCase() }));
console.log(shoutUsers);
Why is this more functional?
- Immutability: No in-place mutations; each operation returns a new list without modifying the old one.
- Recursive algebraic data types:
List<T> uses the classic Nil and Cons pattern.
- Higher-order functions:
map, fold, filter enable elegant transformations.
- Generic: Works with any type, not just users.
- Simple: No classes or services, just pure functions and types.
What about performance/scalability?
- This style favors clarity and composability.
- For large lists and performance-sensitive domains, you'd use more optimized persistent structures (like Finger Trees, or libraries like Immutable.js, Mori, or
fp-ts).
- But this pattern is foundational for understanding functional data structures and patterns.
Bonus: Functor instance for List
You can also express that this List<T> is a Functor by implementing a map method:
const functorList = {
map,
};
// Usage
const uppercased = functorList.map(users, u => ({ ...u, name: u.name.toUpperCase() }));
This aligns fully with functional programming abstractions.
If you want, I can help you build more functor, monad, or applicative abstractions in this style to manage users or any other data purely functionally — just let me know!