Created
February 4, 2022 08:52
-
-
Save aminnairi/b2c75c251b1fec00ed6573a11e8e2718 to your computer and use it in GitHub Desktop.
Correction
This file contains hidden or 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
// For these excercises : | |
// - Only use recursion (or direct recursion) | |
// - Array.prototype methods are prohibited | |
// - for, while & do while loops are prohibited | |
// - Array.prototype.length is accepted | |
/** | |
* @description Update every item of an array | |
* @param {<Type>(item: Type) => Type} update A function that will update one item | |
* @param {Array<Type>} items An array of items to update | |
* @return {Array<Type>} An array of all of the items updated | |
* @example | |
* const increment = value => value + 1; | |
* const items = [1, 2, 3, 4, 5]; | |
* map(increment, items); // [increment(2), increment(3), increment(4), increment(5), increment(6)] | |
* map(incrment, []); // [] | |
*/ | |
export const map = (update, items) => { | |
if (items.length === 0) { | |
return []; | |
} | |
const [item, ...remainingItems] = items; | |
return [update(item), ...update(update, remainingItems)]; | |
}; | |
/** | |
* @description Remove all items that do not satisfy a condition | |
* @param {<Type>(item: Type) => boolean} accepted A function that will take an item and check if it should be accepted | |
* @param {Array<Type>} items An array of items | |
* @return {Array<Type>} An array of all of the items accepted | |
* @example | |
* const even = value => value % 2 === 0; | |
* const items = [1, 2, 3, 4, 5]; | |
* only(even, items); // [2, 4] | |
*/ | |
export const only = (accepted, items) => { | |
if (items.length === 0) { | |
return []; | |
} | |
const [item, ...remainingItems] = items; | |
if (accepted(item)) { | |
return [item, ...only(accepted, remainingItems)]; | |
} | |
return only(accepted, remainingItems); | |
}; | |
/** | |
* @description Remove all items that satisfy a condition | |
* @param {<Type>(item: Type) => boolean} accepted A function that will take an item and check if it should be removed | |
* @param {Array<Type>} items An array of items | |
* @return {Array<Type>} An array of all of the items accepted | |
* @example | |
* const even = value => value % 2 === 0; | |
* const items = [1, 2, 3, 4, 5]; | |
* except(even, items); // [1, 3, 5] | |
*/ | |
export const except = (accepted, items) => { | |
if (items.length === 0) { | |
return []; | |
} | |
const [item, ...remainingItems] = items; | |
if (accepted(item)) { | |
return except(accepted, remainingItems); | |
} | |
return [item, ...except(accepted, remainingItems)]; | |
}; | |
/** | |
* @description Check if at least one of the items satisfy a condition | |
* @param {<Type>(item: Type) => boolean} accepted A function that will take an item and check if it is accepted | |
* @param {Array<Type>} items An array of items | |
* @return {boolean} True if at least one of the items satisfy a condition, false otherwise | |
* @example | |
* const even = value => value % 2 === 0; | |
* const highNumber = value => value > 10; | |
* const items = [1, 2, 3, 4, 5]; | |
* some(even, items); // true | |
* some(highNumber, items); // false | |
*/ | |
export const some = (accepted, items) => { | |
if (items.length === 0) { | |
return false; | |
} | |
const [item, ...remainingItems] = items; | |
if (accepted(item)) { | |
return true; | |
} | |
return some(accepted, remainingItems); | |
}; | |
/** | |
* @description Check if all of the items satisfy a condition | |
* @param {<Type>(item: Type) => boolean} accepted A function that will take an item and check if it is accepted | |
* @param {Array<Type>} items An array of items | |
* @return {boolean} True if all of the items satisfy a condition, false otherwise | |
* @example | |
* const even = value => value % 2 === 0; | |
* const lowNumber = value => value < 10; | |
* const items = [1, 2, 3, 4, 5]; | |
* every(even, items); // false | |
* every(lowNumber, items); // true | |
*/ | |
export const every = (accepted, items) => { | |
if (items.length === 0) { | |
return true; | |
} | |
const [item, ...remainingItems] = items; | |
if (accepted(item)) { | |
return every(accepted, remainingItems); | |
} | |
return false; | |
}; | |
/** | |
* @description Attempt at finding an element that satisfy a condition | |
* @param {<Type>(item: Type) => boolean} found A function that will take an item and check if it is found | |
* @param {Array<Type>} items An array of items to search for | |
* @return {Type | null} The item if found, or null if not | |
* @example | |
* const even = value => value % 2 === 0; | |
* const highNumber = value => value > 10; | |
* const items = [1, 3, 4, 5, 7]; | |
* find(even, items); // 4 | |
* find(highNumber, items); // null | |
*/ | |
export const find = (found, items) => { | |
if (items.length === 0) { | |
return null; | |
} | |
const [item, ...remainingItems] = items; | |
if (found(item)) { | |
return item; | |
} | |
return find(found, remainingItems); | |
}; | |
/** | |
* @description Attempt at finding the index of an element that satisfy a condition | |
* @param {<Type>(item: Type) => boolean} found A function that will take an item and check if it is found | |
* @param {Array<Type>} items An array of items to search for | |
* @return {number} The index of the found item, or -1 | |
* @example | |
* const even = value => value % 2 === 0; | |
* const highNumber = value => value > 10; | |
* const items = [1, 3, 4, 5, 7]; | |
* findIndex(even, items); // 2 | |
* findIndex(highNumber, items); // -1 | |
*/ | |
export const findIndex = (found, items, index = 0) => { | |
if (items.length === 0) { | |
return -1; | |
} | |
const [item, ...remainingItems] = items; | |
if (found(item)) { | |
return index; // 0 | |
} | |
return findIndex(found, remainingItems, index + 1); | |
}; | |
/** | |
* @description Transform an array of items into something else | |
* @param {<AnotherType>(accumulator: AnotherType, item: Type) => AnotherType} reducer A transformer function that has access to the previous accumulation and the current iterated item | |
* @param {AnotherType} state The initial value that the reducer function should take as accumulation | |
* @param {Array<Type>} items An array of elements | |
* @return {AnotherType} The transformation of the array into something else | |
* @example | |
* const initialOccurrences = {}; | |
* const computeOccurrences = (previousOccurrences, letter) => letter in occurrences ? ({...occurrences, [letter]: occurrences[letter] + 1}) : ({...occurrences, [letter]: 1}); | |
* fold(computeOccurrences, initialOccurrences, ["b", "a", "b", "c", "b", "a", "b"]); // {"a": 2, "b": 3, "c": 1} | |
*/ | |
export const fold = (reducer, state, items) => { | |
if (items.length === 0) { | |
return state; | |
} | |
const [item, ...remainingItems] = items; | |
return fold(reducer, reducer(state, item), remainingItems); | |
}; | |
/** | |
* @summary Niveau Avancé | |
* @description Implémenter toutes les fonctions listées à l'URL ci-dessous sauf celle déjà faites ci-dessus et les fonctions qui acceptent/renvoient un Maybe | |
* @see https://package.elm-lang.org/packages/elm/core/latest/List | |
*/ | |
/** | |
* @summary Niveau Expert | |
* @description Implémenter toutes les fonctions listées à l'URL ci-dessous sauf celle déjà faites ci-dessus et les fonctions qui acceptent/renvoient un Maybe | |
* @see https://package.elm-lang.org/packages/elm-community/list-extra/latest/List-Extra | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment