Skip to main content Link Search Menu Expand Document (external link)

Boolean overview

This module provides utility functions and type class instances for working with the boolean type in TypeScript. It includes functions for basic boolean operations, as well as type class instances for Equivalence, Order, Semigroup, and Monoid.

Added in v1.0.0


Table of contents


combinators

and

Combines two boolean using AND: self && that.

Signature

export declare const and: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Example

import { and } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(and(true, true), true)
assert.deepStrictEqual(and(true, false), false)
assert.deepStrictEqual(and(false, true), false)
assert.deepStrictEqual(and(false, false), false)

Added in v1.0.0

eqv

Combines two booleans using EQV (aka XNOR): !xor(self, that).

Signature

export declare const eqv: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Example

import { eqv } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(eqv(true, true), true)
assert.deepStrictEqual(eqv(true, false), false)
assert.deepStrictEqual(eqv(false, true), false)
assert.deepStrictEqual(eqv(false, false), true)

Added in v1.0.0

implies

Combines two booleans using an implication: (!self || that).

Signature

export declare const implies: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Example

import { implies } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(implies(true, true), true)
assert.deepStrictEqual(implies(true, false), false)
assert.deepStrictEqual(implies(false, true), true)
assert.deepStrictEqual(implies(false, false), true)

Added in v1.0.0

nand

Combines two boolean using NAND: !(self && that).

Signature

export declare const nand: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Example

import { nand } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(nand(true, true), false)
assert.deepStrictEqual(nand(true, false), true)
assert.deepStrictEqual(nand(false, true), true)
assert.deepStrictEqual(nand(false, false), true)

Added in v1.0.0

nor

Combines two booleans using NOR: !(self || that).

Signature

export declare const nor: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Example

import { nor } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(nor(true, true), false)
assert.deepStrictEqual(nor(true, false), false)
assert.deepStrictEqual(nor(false, true), false)
assert.deepStrictEqual(nor(false, false), true)

Added in v1.0.0

not

Negates the given boolean: !self

Signature

export declare const not: (self: boolean) => boolean

Example

import { not } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(not(true), false)
assert.deepStrictEqual(not(false), true)

Added in v1.0.0

or

Combines two boolean using OR: self || that.

Signature

export declare const or: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Example

import { or } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(or(true, true), true)
assert.deepStrictEqual(or(true, false), true)
assert.deepStrictEqual(or(false, true), true)
assert.deepStrictEqual(or(false, false), false)

Added in v1.0.0

xor

Combines two booleans using XOR: (!self && that) || (self && !that).

Signature

export declare const xor: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Example

import { xor } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(xor(true, true), false)
assert.deepStrictEqual(xor(true, false), true)
assert.deepStrictEqual(xor(false, true), true)
assert.deepStrictEqual(xor(false, false), false)

Added in v1.0.0

guards

isBoolean

Tests if a value is a boolean.

Signature

export declare const isBoolean: (input: unknown) => input is boolean

Example

import { isBoolean } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(isBoolean(true), true)
assert.deepStrictEqual(isBoolean('true'), false)

Added in v1.0.0

instances

Equivalence

Signature

export declare const Equivalence: equivalence.Equivalence<boolean>

Added in v1.0.0

MonoidAll

boolean monoid under conjunction, see also {@link SemigroupAll}.

The empty value is true.

Signature

export declare const MonoidAll: monoid.Monoid<boolean>

Added in v1.0.0

MonoidAny

boolean monoid under disjunction, see also {@link SemigroupAny}.

The empty value is false.

Signature

export declare const MonoidAny: monoid.Monoid<boolean>

Added in v1.0.0

MonoidEqv

boolean monoid under equivalence.

The empty value is true.

Signature

export declare const MonoidEqv: monoid.Monoid<boolean>

Added in v1.0.0

MonoidXor

boolean monoid under exclusive disjunction, see also {@link SemigroupXor}.

The empty value is false.

Signature

export declare const MonoidXor: monoid.Monoid<boolean>

Added in v1.0.0

Order

Signature

export declare const Order: order.Order<boolean>

Added in v1.0.0

SemigroupAll

boolean semigroup under conjunction.

Signature

export declare const SemigroupAll: semigroup.Semigroup<boolean>

Example

import { SemigroupAll } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(SemigroupAll.combine(true, true), true)
assert.deepStrictEqual(SemigroupAll.combine(true, false), false)
assert.deepStrictEqual(SemigroupAll.combine(false, true), false)
assert.deepStrictEqual(SemigroupAll.combine(false, false), false)

Added in v1.0.0

SemigroupAny

boolean semigroup under disjunction.

Signature

export declare const SemigroupAny: semigroup.Semigroup<boolean>

Example

import { SemigroupAny } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(SemigroupAny.combine(true, true), true)
assert.deepStrictEqual(SemigroupAny.combine(true, false), true)
assert.deepStrictEqual(SemigroupAny.combine(false, true), true)
assert.deepStrictEqual(SemigroupAny.combine(false, false), false)

Added in v1.0.0

SemigroupEqv

boolean semigroup under equivalence.

Signature

export declare const SemigroupEqv: semigroup.Semigroup<boolean>

Example

import { SemigroupEqv } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(SemigroupEqv.combine(true, true), true)
assert.deepStrictEqual(SemigroupEqv.combine(true, false), false)
assert.deepStrictEqual(SemigroupEqv.combine(false, true), false)
assert.deepStrictEqual(SemigroupEqv.combine(false, false), true)

Added in v1.0.0

SemigroupXor

boolean semigroup under exclusive disjunction.

Signature

export declare const SemigroupXor: semigroup.Semigroup<boolean>

Example

import { SemigroupXor } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(SemigroupXor.combine(true, true), false)
assert.deepStrictEqual(SemigroupXor.combine(true, false), true)
assert.deepStrictEqual(SemigroupXor.combine(false, true), true)
assert.deepStrictEqual(SemigroupXor.combine(false, false), false)

Added in v1.0.0

pattern matching

match

This function returns the result of either of the given functions depending on the value of the boolean parameter. It is useful when you have to run one of two functions depending on the boolean value.

Signature

export declare const match: {
  <A, B = A>(onFalse: LazyArg<A>, onTrue: LazyArg<B>): (value: boolean) => A | B
  <A, B>(value: boolean, onFalse: LazyArg<A>, onTrue: LazyArg<B>): A | B
}

Example

import * as B from '@fp-ts/core/Boolean'

assert.deepStrictEqual(
  B.match(
    true,
    () => "It's false!",
    () => "It's true!"
  ),
  "It's true!"
)

Added in v1.0.0

utils

all

This utility function is used to check if all the elements in a collection of boolean values are true.

Signature

export declare const all: (collection: Iterable<boolean>) => boolean

Example

import { all } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(all([true, true, true]), true)
assert.deepStrictEqual(all([true, false, true]), false)

Added in v1.0.0

any

This utility function is used to check if at least one of the elements in a collection of boolean values is true.

Signature

export declare const any: (collection: Iterable<boolean>) => boolean

Example

import { any } from '@fp-ts/core/Boolean'

assert.deepStrictEqual(any([true, false, true]), true)
assert.deepStrictEqual(any([false, false, false]), false)

Added in v1.0.0