Skip to content

Commit

Permalink
Add minimum, maximum to Array
Browse files Browse the repository at this point in the history
  • Loading branch information
samhh committed Jan 25, 2021
1 parent 26bb4c0 commit 4cb4fba
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This project adheres to semantic versioning.

## 0.9.0 (_Unreleased_)

- Add `minimum` and `maximum` functions to the `Array` module.

## 0.8.0 (2021-01-20)

- Add `reduceWhile` and `reduceRightWhile` functions to the `Array` module.
Expand Down
52 changes: 52 additions & 0 deletions docs/modules/Array.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ Added in v0.1.0
- [insertMany](#insertmany)
- [join](#join)
- [length](#length)
- [maximum](#maximum)
- [mean](#mean)
- [median](#median)
- [minimum](#minimum)
- [moveFrom](#movefrom)
- [moveTo](#moveto)
- [none](#none)
Expand Down Expand Up @@ -414,6 +416,31 @@ assert.strictEqual(length(['a', 'b', 'c']), 3)

Added in v0.1.0

## maximum

Obtain the maximum value from a non-empty array.

**Signature**

```ts
export declare const maximum: <A>(ord: Ord<A>) => (xs: NonEmptyArray<A>) => A
```
```hs
maximum :: Ord a -> NonEmptyArray a -> a
```

**Example**

```ts
import { maximum } from 'fp-ts-std/Array'
import { ordNumber } from 'fp-ts/Ord'

assert.strictEqual(maximum(ordNumber)([2, 3, 1, 5, 4]), 5)
```

Added in v0.9.0

## mean

Calculate the mean of an array of numbers.
Expand Down Expand Up @@ -463,6 +490,31 @@ assert.deepStrictEqual(median([7, 2, 10, 9]), 8)

Added in v0.7.0

## minimum

Obtain the minimum value from a non-empty array.

**Signature**

```ts
export declare const minimum: <A>(ord: Ord<A>) => (xs: NonEmptyArray<A>) => A
```
```hs
minimum :: Ord a -> NonEmptyArray a -> a
```

**Example**

```ts
import { minimum } from 'fp-ts-std/Array'
import { ordNumber } from 'fp-ts/Ord'

assert.strictEqual(minimum(ordNumber)([2, 3, 1, 5, 4]), 1)
```

Added in v0.9.0

## moveFrom

Move an item at index `from` to index `to`. See also `moveTo`.
Expand Down
33 changes: 33 additions & 0 deletions src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as O from "fp-ts/Option"
import * as B from "fp-ts/boolean"
import { reduceM } from "fp-ts/Foldable"
import { fold, monoidProduct, monoidSum } from "fp-ts/Monoid"
import { getJoinSemigroup, getMeetSemigroup } from "fp-ts/Semigroup"
import { flip } from "./Function"

/**
Expand Down Expand Up @@ -704,3 +705,35 @@ export const reduceWhile = <A>(p: Predicate<A>) => <B>(
export const reduceRightWhile = <A>(p: Predicate<A>) => <B>(
f: (x: A) => (y: B) => B,
) => (x: B): ((ys: Array<A>) => B) => flow(A.reverse, reduceWhile(p)(f)(x))

/**
* Obtain the minimum value from a non-empty array.
*
* @example
* import { minimum } from 'fp-ts-std/Array';
* import { ordNumber } from 'fp-ts/Ord';
*
* assert.strictEqual(minimum(ordNumber)([2, 3, 1, 5, 4]), 1);
*
* @since 0.9.0
*/
export const minimum: <A>(ord: Ord<A>) => (xs: NonEmptyArray<A>) => A = flow(
getMeetSemigroup,
NEA.fold,
)

/**
* Obtain the maximum value from a non-empty array.
*
* @example
* import { maximum } from 'fp-ts-std/Array';
* import { ordNumber } from 'fp-ts/Ord';
*
* assert.strictEqual(maximum(ordNumber)([2, 3, 1, 5, 4]), 5);
*
* @since 0.9.0
*/
export const maximum: <A>(ord: Ord<A>) => (xs: NonEmptyArray<A>) => A = flow(
getJoinSemigroup,
NEA.fold,
)
40 changes: 40 additions & 0 deletions test/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
symmetricDifference,
reduceWhile,
reduceRightWhile,
minimum,
maximum,
} from "../src/Array"
import * as O from "fp-ts/Option"
import * as A from "fp-ts/Array"
Expand Down Expand Up @@ -916,4 +918,42 @@ describe("Array", () => {
expect(f<number>(constFalse)(add)(0)([1, 2, 3, 4])).toBe(0)
})
})

describe("minimum", () => {
const f = minimum(ordNumber)

it("returns identity on singleton non-empty array", () => {
fc.assert(fc.property(fc.integer(), n => f([n]) === n))
})

it("returns the smallest value", () => {
expect(f([3, 1, 2])).toBe(1)

fc.assert(
fc.property(
fc.integer(),
n => f([n, n + 1]) === n && f([n + 1, n]) === n,
),
)
})
})

describe("maximum", () => {
const f = maximum(ordNumber)

it("returns identity on singleton non-empty array", () => {
fc.assert(fc.property(fc.integer(), n => f([n]) === n))
})

it("returns the largest value", () => {
expect(f([1, 3, 2])).toBe(3)

fc.assert(
fc.property(
fc.integer(),
n => f([n, n + 1]) === n + 1 && f([n + 1, n]) === n + 1,
),
)
})
})
})

0 comments on commit 4cb4fba

Please sign in to comment.