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

extended Bounded type #1544

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 122 additions & 4 deletions src/Bounded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
*
* @since 2.0.0
*/
import { Ord, ordNumber } from './Ord'
import * as Ord from './Ord'
import { Option, fromPredicate } from './Option'
import * as n from './number'
import { pipe } from './function'

// -------------------------------------------------------------------------------------
// model
Expand All @@ -17,11 +20,126 @@ import { Ord, ordNumber } from './Ord'
* @category type classes
* @since 2.0.0
*/
export interface Bounded<A> extends Ord<A> {
export interface Bounded<A> extends Ord.Ord<A> {
readonly top: A
readonly bottom: A
}

// -------------------------------------------------------------------------------------
// deconstructors
// -------------------------------------------------------------------------------------

/**
* @category deconstructors
* @since 2.12.0
*/
export const top = <T>(B: Bounded<T>) => B.top

/**
* @category deconstructors
* @since 2.12.0
*/
export const bottom = <T>(B: Bounded<T>) => B.bottom

/**
* Returns the tuple [bottom, top].
*
* @category deconstructors
* @since 2.12.0
*/
export const toTuple = <T>(B: Bounded<T>): [T, T] =>
[B.bottom, B.top]

// -------------------------------------------------------------------------------------
// guards
// -------------------------------------------------------------------------------------

/**
* Test that top >= bottom
*
* @category guards
* @since 2.12.0
*/
export const isValid = <T>(B: Bounded<T>) =>
Ord.leq(B)(B.bottom, B.top)

// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------

/**
* Returns an instance of Bounded from a range of values.
* Returns none if bottom > top and some if top >= bottom.
*
* @category constructors
* @since 2.12.0
*/
export const fromRange = <T>(O: Ord.Ord<T>) => (b: T) => (t: T): Option<Bounded<T>> =>
pipe({ ...O, top: t, bottom: b }, fromPredicate(isValid))

/**
* Creates an instance of Bounded from the tuple [bottom, top].
* Returns none if fst > snd and some if snd >= fst.
*
* @category constructors
* @since 2.12.0
*/
export const fromTuple = <T>(O: Ord.Ord<T>) => ([b, t]: [T, T]) =>
fromRange(O)(b)(t)

/**
* Returns a valid instance of Bounded given two values where top is the greater of
* the two values and bottom is set to the smaller of the values.
*
* @category constructors
* @since 2.12.0
*/
export const coerceBound = <T>(O: Ord.Ord<T>) => (b: T) => (t: T): Bounded<T> =>
Ord.leq(O)(b, t) ? ({ ...O, bottom: b, top: t }) : ({ ...O, bottom: t, top: b })

// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------

/**
* Clamp a value between bottom and top values.
*
* @category utils
* @since 2.12.0
*/
export const clamp = <T>(B: Bounded<T>) =>
Ord.clamp(B)(B.bottom, B.top)

/**
* Tests whether a value lies between the top and bottom values of bound.
*
* @category utils
* @since 2.12.0
*/
export const isWithin = <T>(B: Bounded<T>) =>
Ord.between(B)(B.bottom, B.top)

/**
* Reverses the Ord of a bound and swaps top and bottom values.
*
* @category utils
* @since 2.12.0
*/
export const reverse = <T>(B: Bounded<T>): Bounded<T> => ({
...Ord.reverse(B),
top: B.bottom,
bottom: B.top
})

/**
* Tests whether the bounded range only contains a single value.
* I.e. if top == bottom under the bounds instance of equality.
*
* @category utils
* @since 2.12.0
*/
export const isSingular = <T>(B: Bounded<T>) => B.equals(B.bottom, B.top)

// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
Expand All @@ -36,8 +154,8 @@ export interface Bounded<A> extends Ord<A> {
* @deprecated
*/
export const boundedNumber: Bounded<number> = {
equals: ordNumber.equals,
compare: ordNumber.compare,
equals: n.Ord.equals,
compare: n.Ord.compare,
top: Infinity,
bottom: -Infinity
}
115 changes: 115 additions & 0 deletions test/Bounded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { reverse, isSingular, coerceBound, fromRange, fromTuple, clamp, top, bottom, isWithin, toTuple, isValid } from '../src/Bounded'
import { BooleanAlgebra as b } from '../src/boolean'
import * as U from './util'
import * as n from '../src/number'
import { pipe } from '../src/function'
import fc from 'fast-check'
import * as Eq from '../src/Eq'

describe('Bounded', () => {

it('top', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (b, t) =>
pipe({ ...n.Ord, bottom: b, top: t }, top, val => n.Eq.equals(t, val))
))
})

it('bottom', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (b, t) =>
pipe({ ...n.Ord, bottom: b, top: t }, bottom, val => n.Eq.equals(b, val))))
})

it('isValid', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (bottom, top) =>
b.implies(
bottom <= top,
isValid({ ...n.Ord, bottom, top })
)))
})

it('isSingular', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (bottom, top) =>
b.implies(bottom === top, isSingular({ ...n.Ord, bottom, top }) &&
b.implies(bottom !== top, !isSingular({ ...n.Ord, bottom, top })
)))
)
})

it('reverse', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (x, y) => {
const bound = { ...n.Ord, bottom: x, top: y }
const reverseBound = reverse(bound)

return top(bound) === bottom(reverseBound) && bottom(bound) === top(reverseBound) &&
bound.compare(x, y) === reverseBound.compare(y, x) &&
b.implies(isValid(bound), isValid(reverseBound))

}))
})

it('coerceBound', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (x, y) => {
const bound = coerceBound(n.Ord)(x)(y)

return isValid(bound) &&
b.implies(x <= y, top(bound) === y && bottom(bound) === x) &&
b.implies(y <= x, top(bound) === x && bottom(bound) === y)

}))
})

it('toTuple', () => {
fc.assert(fc.property(fc.integer(), (bottom) => {
const top = bottom + 100

return pipe(
{ ...n.Ord, bottom, top },
toTuple,
val => Eq.tuple(n.Eq, n.Eq).equals([bottom, top], val))
}))
})

it('isWithin', () => {
const inRange = isWithin({ ...n.Ord, bottom: 0, top: 10 })

U.deepStrictEqual(inRange(2), true)
U.deepStrictEqual(inRange(10), true)
U.deepStrictEqual(inRange(0), true)
U.deepStrictEqual(inRange(20), false)
U.deepStrictEqual(inRange(-10), false)
})

it('fromRange', () => {
const numRange = fromRange(n.Ord)

U.deepStrictEqual(numRange(0)(10)._tag, 'Some')
U.deepStrictEqual(numRange(0)(0)._tag, 'Some')
U.deepStrictEqual(numRange(-1)(0)._tag, 'Some')
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved
U.deepStrictEqual(numRange(-1)(-2)._tag, 'None')
U.deepStrictEqual(numRange(1)(0)._tag, 'None')
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved
})

it('fromTuple', () => {
const numRange = fromTuple(n.Ord)

U.deepStrictEqual(numRange([0, 10])._tag, 'Some')
U.deepStrictEqual(numRange([0,0])._tag, 'Some')
U.deepStrictEqual(numRange([-1, 0])._tag, 'Some')
U.deepStrictEqual(numRange([-1, -2])._tag, 'None')
U.deepStrictEqual(numRange([1, 0])._tag, 'None')
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved
})

it('clamp', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (bottom, val) => {
const top = bottom + 100
const bound = { ...n.Ord, bottom, top }

const clampedValue = clamp(bound)(val)

return b.implies(isWithin(bound)(val), clampedValue === val) &&
b.implies(val < bottom, clampedValue === bottom) &&
b.implies(val > top, clampedValue === top)
}))
})

})