Skip to content

This lab introduces the principles of functional programming.

Mehdji/Lab-Introduction-to-Functional-Programming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Functional Programming Workshop - TypeScript

TypeScript Node.js License: ISC

� Overview

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.

🎯 What You'll Learn

  • 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

� Prerequisites

  • Node.js v18+ installed
  • TypeScript v5.9+ installed
  • Basic JavaScript/TypeScript knowledge
  • Code editor (VS Code recommended)

⚡ Quick Start

# 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.js

� Project Structure

functional-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

🎓 Workshop Exercises

🟢 Exercise 1: Pure vs Impure Functions

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 return 5?
  • Why is increment() unpredictable?

🔵 Exercise 2: Immutability in Practice

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.


🟡 Exercise 3: Higher-Order Functions

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: 16

🟠 Exercise 4: Functional Array Methods

Dataset:

const numbers = [1, 2, 3, 4, 5, 6];

4.1 Chain Operations

Create a one-liner that:

  1. Filters even numbers
  2. Doubles each number
  3. Sums the result

Expected Result: 24

4.2 Utility Functions

Implement using reduce:

  • sum(array) - Calculate total
  • average(array) - Calculate mean
  • product(array) - Multiply all elements

🔴 Exercise 5: Advanced Array Operations

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

🏆 Bonus Challenge: Real-World Scenario

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:

  1. Filters French adults (age ≥ 18)
  2. Extracts their names
  3. Sorts by age (descending)
  4. Calculates average age

Constraint: Use only functional methods - no for loops!

� Core Concepts Reference

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()

🛠️ Development Commands

# 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-node

🤝 Contributing

We 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

📚 Further Reading

📄 License

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

About

This lab introduces the principles of functional programming.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published