August 27, 2020
useEffect()
for Side Effects?useReducer()
and Redux uses Immutable objects?useMemo()
& Memoized?
function addOne(value) {
return value + 1;
}
const addOne = (value) => {
return value + 1;
};
const addOne = (oldValue) => { return oldValue + 1; };
const addOne = (oldValue) => oldValue + 1;
// ['Loaf of Bread', 'Loaf of Bread', 'Slab Bacon', 'Whole Tomato', 'Head of Lettuce']
// => 'The sandwich has: slice of Bread, sliced Tomato, Lettuce leaf, slice of Bread'
const makeVeggieSandwich = (ingredients: string[]): string => {
let sandwichIngredients = [];
for (let i = 0; i < ingredients.length; i++) {
if (isVeggieType(ingredients[i])) {
const slicedIngredient = getSlicedIngredient(ingredients[i]);
sandwichIngredients.push(slicedIngredient);
}
}
sortIngredients(sandwichIngredients);
let sandwich = 'The sandwich has: ';
for (let j = 0; j < sandwichIngredients.length; j++) {
sandwich += `${sandwichIngredients[j].value}, `;
}
return sandwich;
};
// ['Loaf of Bread', 'Loaf of Bread', 'Slab Bacon', 'Whole Tomato', 'Head of Lettuce']
// => 'The sandwich has: slice of Bread, sliced Tomato, Lettuce leaf, slice of Bread'
const makeVeggieSandwich = (ingredients: string[]): string => {
const sandwich = ingredients
.filter(isVeggieType)
.map(getSlicedIngredient)
.sort(sortIngredients)
.reduce(
(sandwich, ingredient) => `${sandwich}, ${ingredient.value}`,
'The sandwich has: '
);
return sandwich;
};
let word = 'World';
doSomething(word);
console.log(`Hello ${word}`); // 'Hello World'?
let word = 'World';
const doSomething = (word) => {
a(word); b(word); c(word); d(word); e(word); f(word);
};
doSomething(word);
console.log(`Hello ${word}`); // 'Hello F-Word'?!
// impure
let minimum = 21;
const checkAge = (age) => age >= minimum;
// pure
const checkAge = (age) => {
const minimum = 21;
return age >= minimum;
};
// pure with currying
const checkAge = (age) => (minimum) => age >= minimum;
const isOfAge = checkAge(36)(18);
// 1 => "Luke Skywalker"
const getCharacterName = (id: number): string => {
fetch(`https://swapi.dev/api/people/${id}/`)
.then((response) => {
if (!response.ok) {
throw new Error('HTTP error')
}
response.json()
.then((character) => {
if (!character) {
throw new Error('no character object');
}
if (!character.name) {
throw new Error('No name property');
}
return character.name;
});
})
.catch((error) => {
throw new Error(`Fetch Error: ${error}`);
});
};
module CardGame =
type Suit = Club | Diamond | Spade | Heart
type Rank = Two | Three | Four | Five | ...
type Card = Suit * Rank
type Hand = Card list
type Deck = Card list
type Deal = Deck -> (Deck * Card)
module CardGame =
type Suit = Club | Diamond | Spade | Heart
type Rank = Two | Three | Four | Five | ...
type Card = Suit * Rank
type Hand = Card list
type Deck = Card list
type ShuffledDeck = Card list
type Shuffle = Deck -> ShuffledDeck
type Deal = ShuffledDeck -> (ShuffledDeck * Card)
type Contact = {
FirstName: string
MiddleInitial: string
LastName: string
EmailAddress: string
IsEmailVerified: bool
}
type PersonName = {
FirstName: String50
MiddleInitial: String1 option
LastName: String50
}
type EmailContactInfo = {
EmailAddress: EmailAddress
IsEmailVerified: bool
}
type Contact = {
Name: PersonName
Email: EmailContactInfo
}
type PersonName = {
FirstName: String50
MiddleInitial: String1 option
LastName: String50
}
type VerifiedEmail = VerifiedEmail of EmailAddress
type VerificationService = (EmailAddress * VerificationHash) -> VerifiedEmail
type EmailContactInfo =
| Unverified of EmailAddress
| Verified of VerifiedEmail
type Contact = {
Name: PersonName
Email: EmailContactInfo
}