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 1 commit
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
98 changes: 96 additions & 2 deletions src/Bounded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @since 2.0.0
*/
import { Ord, ordNumber } from './Ord'
import * as Ord from './Ord'

// -------------------------------------------------------------------------------------
// model
Expand All @@ -17,11 +17,105 @@ 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.0.0
*/
export const top = <T>(b: Bounded<T>) => b.top

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

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

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

/**
* Test that top >= bottom
*
* @category guards
* @since 2.0.0
*/
export const isValid = <T>(bound: Bounded<T>) =>
Ord.geq(bound)(bound.bottom, bound.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.0.0
*/
export const fromRange = <T>(ord: Ord.Ord<T>) => (b: T) => (t: T): Bounded<T> =>
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved
({ ...ord, bottom: b, top: t })

/**
* Creates an instance of Bounded from the tuple [bottom, top].
*
* @category constructors
* @since 2.0.0
*/
export const fromTuple = <T>(ord: Ord.Ord<T>) => ([b, t]: [T, T]): Bounded<T> =>
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved

fromRange(ord)(b)(t)

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

/**
* Clamp a value between bottom and top values.
*
* @category utils
* @since 2.0.0
*/
export const clamp = <T>(bound: Bounded<T>) =>
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved
Ord.clamp(bound)(bound.bottom, bound.top)

/**
* Tests if two bounds are overlapping
*
* @category utils
* @since 2.0.0
*/
export const isOverlapping = <T>(a: Bounded<T>) => (b: Bounded<T>) =>
Ord.leq(a)(a.bottom, b.top) && Ord.geq(a)(a.top, b.bottom)

/**
* Tests whether a value lies between the top and bottom values of bound.
*
* @category util
* s
* @since 2.0.0
*/
export const isWithin = <T>(bound: Bounded<T>) =>
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved
Ord.between(bound)(bound.bottom, bound.top)

// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions src/Eq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ export const Contravariant: Contravariant1<URI> = {
// deprecated
// -------------------------------------------------------------------------------------

/**
* @category utils
* @since 2.11.0
*/
export const isEqual = <T>(eq: Eq<T>) => (a: T) => (b: T) =>
eq.equals(a, b)
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved

// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------

/**
* Use [`tuple`](#tuple) instead.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ export const or = <A>(second: Predicate<A>) => (first: Predicate<A>): Predicate<
* @since 2.11.0
*/
export const and = <A>(second: Predicate<A>) => (first: Predicate<A>): Predicate<A> => (a) => first(a) && second(a)

/**
* @since 2.11.0
*/
export const implies = <A>(first: Predicate<A>, second: Predicate<A>): Predicate<A> =>
not(and(first)(not(second)))
jessekelly881 marked this conversation as resolved.
Show resolved Hide resolved
65 changes: 65 additions & 0 deletions test/Bounded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { fromRange, clamp, top, bottom, isWithin, isOverlapping, toTuple, isValid } from '../src/Bounded'
import * as U from './util'
import * as n from '../src/number'
import { pipe } from 'fp-ts/function'
import fc from 'fast-check'
import * as Eq from '../src/Eq'

describe('Bounded', () => {
const numBound = fromRange(n.Ord)

it('top', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (b, t) =>
pipe(numBound(b)(t), top, Eq.isEqual(n.Eq)(t))
))
})

it('bottom', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (b, t) =>
pipe(numBound(b)(t), bottom, Eq.isEqual(n.Eq)(b))
))
})

it('isValid', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (b, t) =>
b <= t && pipe(numBound(b)(t), isValid)
))
})

it('toTuple', () => {
fc.assert(fc.property(fc.integer(), fc.integer(), (b, t) => {
pipe(numBound(b)(t), toTuple, Eq.isEqual(Eq.tuple(n.Eq, n.Eq))([b, t]))
}))
})

it('clamp', () => {
const clampNum = pipe(numBound(1)(10), clamp)

U.deepStrictEqual(clampNum(2), 2)
U.deepStrictEqual(clampNum(10), 10)
U.deepStrictEqual(clampNum(20), 10)
U.deepStrictEqual(clampNum(1), 1)
U.deepStrictEqual(clampNum(-10), 0)
})

it('isWithin', () => {
const withinRange = pipe(numBound(1)(10), isWithin)

U.deepStrictEqual(withinRange(2), true)
U.deepStrictEqual(withinRange(10), true)
U.deepStrictEqual(withinRange(20), false)
U.deepStrictEqual(withinRange(1), true)
U.deepStrictEqual(withinRange(-10), false)
})

it('isOverlapping', () => {
const boundOverlaps = pipe(numBound(0)(10), isOverlapping)

U.deepStrictEqual(boundOverlaps(numBound(1)(5)), true)
U.deepStrictEqual(boundOverlaps(numBound(10)(11)), true)
U.deepStrictEqual(boundOverlaps(numBound(-1)(0)), true)
U.deepStrictEqual(boundOverlaps(numBound(-2)(-1)), false)
U.deepStrictEqual(boundOverlaps(numBound(11)(12)), false)
})

})
10 changes: 10 additions & 0 deletions test/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ describe('Predicate', () => {
U.deepStrictEqual(predicate(-1), true)
})

it('implies', () => {
const implies = (a: boolean, b: boolean) => _.implies(() => a, () => b)(true)

// Truth table.
U.deepStrictEqual(implies(true, true), true)
U.deepStrictEqual(implies(true, false), false)
U.deepStrictEqual(implies(false, true), true)
U.deepStrictEqual(implies(false, false), true)
})

it('getMonoidAny', () => {
const M = _.getMonoidAny<number>()
const predicate = M.concat(isPositive, isNegative)
Expand Down
Loading