Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lauren fp ts exercises #151

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
76 changes: 60 additions & 16 deletions src/exo3 - Sort with Ord/exo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
// Sort things out with `Ord`

import { Option } from 'fp-ts/Option';
import { unimplemented } from '../utils';
import { number, option, ord, readonlyArray, string } from 'fp-ts';
import { pipe } from 'fp-ts/lib/function';



/******* I relied heavily on the fp-ts-cheatsheet to solve these problems **********/



// Have you ever looked at the methods provided by `fp-ts` own `Array` and
// `ReadonlyArray` modules? They expose a load of functions to manipulate
Expand Down Expand Up @@ -34,13 +41,19 @@ import { unimplemented } from '../utils';
// expose some pre constructed instances of `Ord<T>` for said primitives such as
// `string.Ord: Ord<string>` or `number.Ord: Ord<number>`.

export const sortStrings: (
export const sortStrings = (
lauren-inato marked this conversation as resolved.
Show resolved Hide resolved
strings: ReadonlyArray<string>,
) => ReadonlyArray<string> = unimplemented;
): ReadonlyArray<string> => pipe(
strings,
readonlyArray.sort(string.Ord)
);

export const sortNumbers: (
export const sortNumbers = (
numbers: ReadonlyArray<number>,
) => ReadonlyArray<number> = unimplemented;
): ReadonlyArray<number> => pipe (
numbers,
readonlyArray.sort(number.Ord)
);

///////////////////////////////////////////////////////////////////////////////
// REVERSE SORT //
Expand All @@ -55,9 +68,12 @@ export const sortNumbers: (
//
// HINT: Any ordering can be reversed with a simple function `ord.reverse`.

export const sortNumbersDescending: (
export const sortNumbersDescending = (
numbers: ReadonlyArray<number>,
) => ReadonlyArray<number> = unimplemented;
): ReadonlyArray<number> => pipe (
numbers,
readonlyArray.sort(ord.reverse(number.Ord))
);

///////////////////////////////////////////////////////////////////////////////
// SORT OPTIONAL VALUES //
Expand All @@ -73,9 +89,12 @@ export const sortNumbersDescending: (
// of building an `Ord` instance for their qualified inner type. You may want
// to take a look at `option.getOrd`.

export const sortOptionalNumbers: (
export const sortOptionalNumbers = (
optionalNumbers: ReadonlyArray<Option<number>>,
) => ReadonlyArray<Option<number>> = unimplemented;
): ReadonlyArray<Option<number>> => pipe (
optionalNumbers,
readonlyArray.sort(option.getOrd(number.Ord))
);

///////////////////////////////////////////////////////////////////////////////
// SORT COMPLEX OBJECTS //
Expand All @@ -99,13 +118,25 @@ export interface Person {
readonly age: Option<number>;
}

export const sortPersonsByName: (
export const sortPersonsByName = (
persons: ReadonlyArray<Person>,
) => ReadonlyArray<Person> = unimplemented;

export const sortPersonsByAge: (
): ReadonlyArray<Person> => pipe (
persons,
readonlyArray.sortBy([pipe(
string.Ord,
ord.contramap((person: Person) => person.name)
)])
);

export const sortPersonsByAge = (
persons: ReadonlyArray<Person>,
) => ReadonlyArray<Person> = unimplemented;
): ReadonlyArray<Person> => pipe (
persons,
readonlyArray.sortBy( [pipe (
option.getOrd(number.Ord),
ord.contramap((person: Person) => person.age)
)])
);

///////////////////////////////////////////////////////////////////////////////
// COMBINE SORTING SCHEMES //
Expand All @@ -116,6 +147,19 @@ export const sortPersonsByAge: (
//
// HINT: Take a look at `readonlyArray.sortBy`

export const sortPersonsByAgeThenByName: (
const byAge = pipe (
option.getOrd(number.Ord),
ord.contramap((person: Person) => person.age)
)

const byName = pipe (
string.Ord,
ord.contramap((person: Person) => person.name)
)

export const sortPersonsByAgeThenByName = (
persons: ReadonlyArray<Person>,
) => ReadonlyArray<Person> = unimplemented;
): ReadonlyArray<Person> => pipe (
persons,
readonlyArray.sortBy([byAge, byName])
);