This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BankAccount = { | |
balance: number; | |
}; | |
function createBankAccount(initialBalance: number): BankAccount { | |
return { | |
balance: initialBalance | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createBankAccount() { | |
let balance: number = 0; | |
return { | |
deposit(amount: number) { | |
balance += amount; | |
console.log(`Déposé: ${amount}. Nouveau solde: ${balance}`); | |
}, | |
withdraw(amount: number) { | |
if (amount <= balance) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type WaterGlass = { | |
material: string; | |
quantity: number; | |
size: number[]; | |
substance: string; | |
}; | |
function createWaterGlass(quantity: number, substance: string) { | |
return function create(quantity: number, substance: string) { | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createWaterGlassOperations(initialQuantity: number, initialSubstance: string) { | |
let currentQuantity = initialQuantity; | |
let currentSubstance = initialSubstance; | |
return { | |
drink: function(quantity: number) { | |
currentQuantity -= quantity; | |
console.log(`Drank ${quantity} from a ${currentSubstance} glass, ${currentQuantity} remaining.`); | |
}, | |
fill: function(quantity: number, substance: string) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type WaterGlass = { | |
material: string; | |
quantity: number; | |
size: number[]; | |
substance: string; | |
}; | |
function createWaterGlass(quantity: number, substance: string): WaterGlass { | |
return { | |
material: 'glass', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SecondWaterGlass { | |
material: string | |
quantity: number | |
size: number[] | |
substance: string | |
constructor(quantity: number, substance: string) { | |
this.material = 'glass' | |
this.quantity = quantity | |
this.size = [120, 30] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createCounter(): { increment: () => void } { | |
let count = 0; | |
return function(): void { | |
count++; | |
console.log(count); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createMultiplier(multiplier: number): (arg: number) => number { | |
return function(value: number): number { | |
return value * multiplier; | |
}; | |
} | |
// Création d'une fonction qui multiplie par 3 | |
const triple = createMultiplier(3); | |
// Utilisation de la fonction retournée |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Une fonction simple qui multiplie un nombre par deux | |
function double(x: number): number { | |
return x * 2; | |
} | |
// Une High Order Function qui prend une fonction en argument | |
function applyFunction(arr: number[], func: (arg: number) => number): number[] { | |
return arr.map(func); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Composition pour calculer la moyenne des carrés d'une liste de nombres | |
function averageOfSquares(numbers: number[]): number { | |
const arrayOne = numbers.map(square) | |
const arrayTwo = arrayOne.reduce(sum, 0); | |
console.log(`La valeur du tableau numbers est ${numbers}`) | |
console.log(`La valeur du tableau arrayOne est ${arrayOne}`) | |
console.log(`La valeur du tableau arrayTwo est ${arrayTwo}`) |
NewerOlder