Add exercice on sequence

This commit is contained in:
vinassefranche 2022-03-07 15:00:59 +01:00
parent 51e335067d
commit 94af9b7821
2 changed files with 55 additions and 1 deletions

View file

@ -5,6 +5,8 @@ import {
giveCurrencyOfCountryToUser,
performAsyncComputationInParallel,
performAsyncComputationInSequence,
sequenceOptionArray,
sequenceOptionTask,
} from './exo5';
describe('exo5', () => {
@ -73,4 +75,27 @@ describe('exo5', () => {
expect(result).toStrictEqual([1, 2, 3]);
});
});
describe('sequenceOptionTask', () => {
it('should return a None if called with a None', async () => {
const result = await sequenceOptionTask(option.none)();
expect(result).toStrictEqual(option.none);
});
it('should return a Some if called with a Some', async () => {
const result = await sequenceOptionTask(option.some(async () => 'EUR'))();
expect(result).toStrictEqual(option.some('EUR'));
});
});
describe('sequenceOptionArray', () => {
it('should return a None if one of the option in the array is None', () => {
const result = sequenceOptionArray([option.none, option.some('FR')]);
expect(result).toStrictEqual(option.none);
});
it('should return a Some if all the options in the arrat are Some', () => {
const result = sequenceOptionArray([
option.some('FR'),
option.some('SP'),
]);
expect(result).toStrictEqual(option.some(['FR', 'SP']));
});
});
});

View file

@ -146,7 +146,7 @@ export const getCountryCodeOfCountryNames = (
export const getValidCountryCodeOfCountryNames: (
countryNames: ReadonlyArray<string>,
) => Option<ReadonlyArray<CountryCode>> = unimplemented();
) => Option<ReadonlyArray<CountryCode>> = unimplemented;
///////////////////////////////////////////////////////////////////////////////
// TRAVERSING ARRAYS ASYNCHRONOUSLY //
@ -197,3 +197,32 @@ export const simulatedAsyncMethodForSequence = createSimulatedAsyncMethod();
export const performAsyncComputationInSequence: (
numbers: ReadonlyArray<number>,
) => Task<ReadonlyArray<number>> = unimplementedAsync;
///////////////////////////////////////////////////////////////////////////////
// SEQUENCE //
///////////////////////////////////////////////////////////////////////////////
// `traverse` is nice when you need to get the value inside a container (let's
// say `Option`), apply a method to it that return another container type (let's
// say `Task`) and 'invert' the container (to get a `Task<Option>` instead of a
// `Option<Task>` in our example)
// Sometimes, you just have two nested containers that you want to 'invert'. It
// can be because both order of container are meaningful (like `Either<Option>`
// and `Option<Either>`) of because you got them from an external api, as
// examples.
// In that case, what you need is `sequence`, which you can find in the modules
// that have `traverse`.
//
// Use the `sequence` methods from the `option` module to implement the two
// functions below
export const sequenceOptionTask: (
optionOfTask: Option<Task<Currency>>,
) => Task<Option<Currency>> = unimplementedAsync;
export const sequenceOptionArray: (
arrayOfOptions: ReadonlyArray<Option<CountryCode>>,
) => Option<ReadonlyArray<CountryCode>> = unimplemented;
// BONUS: try using these two functions in the exercices 'TRAVERSING OPTIONS'
// and 'TRAVERSING ARRAYS' above