diff --git a/src/exo5/exo5.test.ts b/src/exo5/exo5.test.ts index 9a118d4..277580b 100644 --- a/src/exo5/exo5.test.ts +++ b/src/exo5/exo5.test.ts @@ -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'])); + }); + }); }); diff --git a/src/exo5/exo5.ts b/src/exo5/exo5.ts index 4cfe406..21fcf5d 100644 --- a/src/exo5/exo5.ts +++ b/src/exo5/exo5.ts @@ -146,7 +146,7 @@ export const getCountryCodeOfCountryNames = ( export const getValidCountryCodeOfCountryNames: ( countryNames: ReadonlyArray, -) => Option> = unimplemented(); +) => Option> = unimplemented; /////////////////////////////////////////////////////////////////////////////// // TRAVERSING ARRAYS ASYNCHRONOUSLY // @@ -197,3 +197,32 @@ export const simulatedAsyncMethodForSequence = createSimulatedAsyncMethod(); export const performAsyncComputationInSequence: ( numbers: ReadonlyArray, ) => Task> = 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