Add exercice on sequence
This commit is contained in:
parent
51e335067d
commit
94af9b7821
2 changed files with 55 additions and 1 deletions
|
@ -5,6 +5,8 @@ import {
|
||||||
giveCurrencyOfCountryToUser,
|
giveCurrencyOfCountryToUser,
|
||||||
performAsyncComputationInParallel,
|
performAsyncComputationInParallel,
|
||||||
performAsyncComputationInSequence,
|
performAsyncComputationInSequence,
|
||||||
|
sequenceOptionArray,
|
||||||
|
sequenceOptionTask,
|
||||||
} from './exo5';
|
} from './exo5';
|
||||||
|
|
||||||
describe('exo5', () => {
|
describe('exo5', () => {
|
||||||
|
@ -73,4 +75,27 @@ describe('exo5', () => {
|
||||||
expect(result).toStrictEqual([1, 2, 3]);
|
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']));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -146,7 +146,7 @@ export const getCountryCodeOfCountryNames = (
|
||||||
|
|
||||||
export const getValidCountryCodeOfCountryNames: (
|
export const getValidCountryCodeOfCountryNames: (
|
||||||
countryNames: ReadonlyArray<string>,
|
countryNames: ReadonlyArray<string>,
|
||||||
) => Option<ReadonlyArray<CountryCode>> = unimplemented();
|
) => Option<ReadonlyArray<CountryCode>> = unimplemented;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// TRAVERSING ARRAYS ASYNCHRONOUSLY //
|
// TRAVERSING ARRAYS ASYNCHRONOUSLY //
|
||||||
|
@ -197,3 +197,32 @@ export const simulatedAsyncMethodForSequence = createSimulatedAsyncMethod();
|
||||||
export const performAsyncComputationInSequence: (
|
export const performAsyncComputationInSequence: (
|
||||||
numbers: ReadonlyArray<number>,
|
numbers: ReadonlyArray<number>,
|
||||||
) => Task<ReadonlyArray<number>> = unimplementedAsync;
|
) => 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
|
||||||
|
|
Reference in a new issue