Add exercice number 3 - Ord

This commit is contained in:
Hugo Saracino 2020-09-14 12:00:45 +02:00
parent f3a5b79512
commit c0ca5d234c
3 changed files with 164 additions and 0 deletions

104
src/exo3/exo3.test.ts Normal file
View file

@ -0,0 +1,104 @@
import * as Option from 'fp-ts/lib/Option';
import {
sortStrings,
sortNumbers,
sortNumbersDescending,
sortOptionalNumbers,
sortPersonsByName,
sortPersonsByAge,
sortPersonsByAgeThenByName,
} from './exo3';
describe('exo3', () => {
describe('sortStrings', () => {
it('should return an alphabetically sorted array of strings', () => {
const strings = ['xyz', 'aba', 'ori', 'aab', 'ghl'];
const result = sortStrings(strings);
const expected = ['aab', 'aba', 'ghl', 'ori', 'xyz'];
expect(result).toStrictEqual(expected);
});
});
describe('sortNumbers', () => {
it('should return a sorted array of numbers', () => {
const numbers = [1337, 42, 5701];
const result = sortNumbers(numbers);
const expected = [42, 1337, 5701];
expect(result).toStrictEqual(expected);
});
});
describe('sortNumbersDescending', () => {
it('should return a sorted array of descending numbers', () => {
const numbers = [1337, 42, 5701];
const result = sortNumbersDescending(numbers);
const expected = [5701, 1337, 42];
expect(result).toStrictEqual(expected);
});
});
describe('sortOptionalNumbers', () => {
it('should return a sorted array of optional numbers', () => {
const optionalNumbers = [Option.some(1337), Option.none, Option.some(42)];
const result = sortOptionalNumbers(optionalNumbers);
const expected = [Option.none, Option.some(42), Option.some(1337)];
expect(result).toStrictEqual(expected);
});
});
describe('sortPersonsByName', () => {
it('should return an array of persons alphabetically sorted by their name', () => {
const alice = { name: 'Alice', age: Option.none };
const bob = { name: 'Bob', age: Option.none };
const crystal = { name: 'Crystal', age: Option.none };
const persons = [crystal, alice, bob];
const result = sortPersonsByName(persons);
const expected = [alice, bob, crystal];
expect(result).toStrictEqual(expected);
});
});
describe('sortPersonsByName', () => {
it('should return an array of persons sorted by their age', () => {
const alice = { name: 'Alice', age: Option.some(42) };
const bob = { name: 'Bob', age: Option.none };
const crystal = { name: 'Crystal', age: Option.some(29) };
const persons = [crystal, alice, bob];
const result = sortPersonsByAge(persons);
const expected = [bob, crystal, alice];
expect(result).toStrictEqual(expected);
});
});
describe('sortPersonsByName', () => {
it('should return an array of persons sorted first by age and then by name', () => {
const alice = { name: 'Alice', age: Option.some(42) };
const bob = { name: 'Bob', age: Option.none };
const crystal = { name: 'Crystal', age: Option.some(29) };
const dorian = { name: 'Dorian', age: Option.some(29) };
const edgar = { name: 'Edgar', age: Option.none };
const persons = [dorian, alice, edgar, bob, crystal];
const result = sortPersonsByAgeThenByName(persons);
const expected = [bob, edgar, crystal, dorian, alice];
expect(result).toStrictEqual(expected);
});
});
});

59
src/exo3/exo3.ts Normal file
View file

@ -0,0 +1,59 @@
// `fp-ts` training Exercice 3
// Sort things out with `Ord`
import * as Option from 'fp-ts/lib/Option';
import { unimplemented } from '../utils';
///////////////////////////////////////////////////////////////////////////////
// SORT PRIMITIVE TYPES //
///////////////////////////////////////////////////////////////////////////////
export const sortStrings: (
strings: ReadonlyArray<string>,
) => ReadonlyArray<string> = unimplemented;
export const sortNumbers: (
numbers: ReadonlyArray<number>,
) => ReadonlyArray<number> = unimplemented;
///////////////////////////////////////////////////////////////////////////////
// REVERSE SORT //
///////////////////////////////////////////////////////////////////////////////
export const sortNumbersDescending: (
numbers: ReadonlyArray<number>,
) => ReadonlyArray<number> = unimplemented;
///////////////////////////////////////////////////////////////////////////////
// SORT OPTIONAL VALUES //
///////////////////////////////////////////////////////////////////////////////
export const sortOptionalNumbers: (
optionalNumbers: ReadonlyArray<Option.Option<number>>,
) => ReadonlyArray<Option.Option<number>> = unimplemented;
///////////////////////////////////////////////////////////////////////////////
// SORT COMPLEX OBJECTS //
///////////////////////////////////////////////////////////////////////////////
export interface Person {
readonly name: string;
readonly age: Option.Option<number>;
}
export const sortPersonsByName: (
person: ReadonlyArray<Person>,
) => ReadonlyArray<Person> = unimplemented;
export const sortPersonsByAge: (
person: ReadonlyArray<Person>,
) => ReadonlyArray<Person> = unimplemented;
///////////////////////////////////////////////////////////////////////////////
// COMBINE SORTING SCHEMES //
///////////////////////////////////////////////////////////////////////////////
export const sortPersonsByAgeThenByName: (
person: ReadonlyArray<Person>,
) => ReadonlyArray<Person> = unimplemented;

1
src/exo3/index.ts Normal file
View file

@ -0,0 +1 @@
export * from './exo3';