This repository contains a comprehensive workshop introducing functional programming concepts using TypeScript. Learn essential FP principles through hands-on exercises covering pure functions, immutability, higher-order functions, and functional array methods.
- ✅ Pure Functions vs Impure Functions
- ✅ Immutability principles and practices
- ✅ Higher-Order Functions and function composition
- ✅ Functional Array Methods (
map,filter,reduce) - ✅ Advanced Array Operations (
find,some,every,flatMap) - ✅ Real-world Applications of functional programming concepts
- Node.js v18+ installed
- TypeScript v5.9+ installed
- Basic JavaScript/TypeScript knowledge
- Code editor (VS Code recommended)
# Clone the repository
git clone https://github.com/yourusername/functional-programming-workshop.git
cd functional-programming-workshop
# Install dependencies
npm install
# Run TypeScript exercises
npx ts-node src/tp-fonctionnels.ts
# Or compile and run
npx tsc src/tp-fonctionnels.ts && node src/tp-fonctionnels.jsfunctional-programming-workshop/
├── 📁 src/
│ ├── 📄 index.ts # Main entry point
│ ├── 📄 tp-fonctionnels.ts # Workshop exercises (TypeScript)
│ └── 📄 tp-fonctionnels.js # Compiled JavaScript version
├── 📄 package.json # Project configuration
└── 📄 README.md # You are here
Objective: Understand the fundamental difference between pure and impure functions.
let counter = 0;
// Pure function - predictable, no side effects
function add(a: number, b: number): number {
return a + b;
}
// Impure function - modifies external state
function increment(): number {
counter++;
return counter;
}Key Questions:
- Why does
add(2, 3)always return5? - Why is
increment()unpredictable?
Objective: Learn to update data without mutation.
const student = { name: "Alex", grade: 85 };
// TODO: Write updateGrade function that returns new object
// Hint: Use spread operator {...student, grade: newGrade}Challenge: Verify the original object remains unchanged.
Objective: Create functions that operate on other functions.
function applyNTimes(fn: (x: number) => number, times: number, value: number): number {
// TODO: Apply fn to value, times number of times
}
// Example usage:
const double = (x: number) => x * 2;
console.log(applyNTimes(double, 3, 2)); // Should output: 16Dataset:
const numbers = [1, 2, 3, 4, 5, 6];Create a one-liner that:
- Filters even numbers
- Doubles each number
- Sums the result
Expected Result: 24
Implement using reduce:
sum(array)- Calculate totalaverage(array)- Calculate meanproduct(array)- Multiply all elements
Dataset:
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 17 },
{ name: "Charlie", age: 30 },
{ name: "Diana", age: 19 }
];Tasks:
- 5.1
find- Locate first adult user (age ≥ 18) - 5.2
some/every- Check if any/all users meet criteria - 5.3
includes- Search for specific names - 5.4
flatMap- Flatten nested hobby arrays - 5.5
sort/slice- Sort by age and get top 2
Dataset:
type User = { name: string; age: number; country: string };
const users: User[] = [
{ name: "Alice", age: 25, country: "France" },
{ name: "Bob", age: 17, country: "France" },
{ name: "Charlie", age: 30, country: "Spain" },
{ name: "Diana", age: 22, country: "France" }
];Mission: Build a data pipeline that:
- Filters French adults (age ≥ 18)
- Extracts their names
- Sorts by age (descending)
- Calculates average age
Constraint: Use only functional methods - no for loops!
| Concept | Description | Example |
|---|---|---|
| Pure Function | Predictable output, no side effects | (a, b) => a + b |
| Immutability | Create new data instead of modifying | {...obj, newProp} |
| Higher-Order | Functions that use other functions | array.map(fn) |
| Composition | Chain operations together | filter().map().reduce() |
# Run with TypeScript directly
npx ts-node src/tp-fonctionnels.ts
# Compile to JavaScript
npx tsc src/tp-fonctionnels.ts
# Run compiled JavaScript
node src/tp-fonctionnels.js
# Install TypeScript globally (optional)
npm install -g typescript ts-nodeWe welcome contributions! Here's how you can help:
- 🐛 Report bugs via GitHub Issues
- 💡 Suggest improvements or new exercises
- 📝 Improve documentation
- ✨ Add more examples or test cases
- 📖 TypeScript Handbook
- 🌐 MDN Array Methods
- 🎯 Functional Programming Guide
- 📚 You Don't Know JS - Functional Programming
This project is licensed under the ISC License - see the LICENSE file for details.
🎉 Happy Coding! Let's explore the power of functional programming! 🎉
Made with ❤️ for learning functional programming