Skip to content

🌈 Fantasy Land -compliant Descending type

License

Notifications You must be signed in to change notification settings

sanctuary-js/sanctuary-descending

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fantasy Land

sanctuary-descending

Descending is a simple container type: a value of type Descending a always contains exactly one value, of type a.

Values of type Descending a sort in the reverse order of values of type a.

Descending differs from Identity only in the behaviour of its fantasy-land/lte method.

> S.sort ([5, 1, 2])
[1, 2, 5]

> S.sort ([Descending (5), Descending (1), Descending (2)])
[Descending (5), Descending (2), Descending (1)]

> S.sortBy (Descending) ([5, 1, 2])
[5, 2, 1]

Descending a satisfies the following Fantasy Land specifications:

> const Useless = require ('sanctuary-useless')

> const isTypeClass = x =>
.   type (x) === 'sanctuary-type-classes/TypeClass@1'

> S.map (k => k + ' '.repeat (16 - k.length) +
.             (Z[k].test (Descending (Useless)) ? '\u2705   ' :
.              Z[k].test (Descending (['foo'])) ? '\u2705 * ' :
.              /* otherwise */                    '\u274C   '))
.       (S.keys (S.unchecked.filter (isTypeClass) (Z)))
[ 'Setoid          ✅ * ',  // if ‘a’ satisfies Setoid
. 'Ord             ✅ * ',  // if ‘a’ satisfies Ord
. 'Semigroupoid    ❌   ',
. 'Category        ❌   ',
. 'Semigroup       ✅ * ',  // if ‘a’ satisfies Semigroup
. 'Monoid          ❌   ',
. 'Group           ❌   ',
. 'Filterable      ❌   ',
. 'Functor         ✅   ',
. 'Bifunctor       ❌   ',
. 'Profunctor      ❌   ',
. 'Apply           ✅   ',
. 'Applicative     ✅   ',
. 'Chain           ✅   ',
. 'ChainRec        ✅   ',
. 'Monad           ✅   ',
. 'Alt             ❌   ',
. 'Plus            ❌   ',
. 'Alternative     ❌   ',
. 'Foldable        ✅   ',
. 'Traversable     ✅   ',
. 'Extend          ✅   ',
. 'Comonad         ✅   ',
. 'Contravariant   ❌   ' ]

Descending's sole data constructor. Additionally, it serves as the Descending type representative.

> Descending (42)
Descending (42)

of (Descending) (x) is equivalent to Descending (x).

> S.of (Descending) (42)
Descending (42)
> Z.chainRec (
.   Descending,
.   (next, done, x) => Descending (x >= 0 ? done (x * x) : next (x + 1)),
.   8
. )
Descending (64)

> Z.chainRec (
.   Descending,
.   (next, done, x) => Descending (x >= 0 ? done (x * x) : next (x + 1)),
.   -8
. )
Descending (0)

show (Descending (x)) is equivalent to 'Descending (' + show (x) + ')'.

> show (Descending (['foo', 'bar', 'baz']))
'Descending (["foo", "bar", "baz"])'

Descending (x) is equal to Descending (y) iff x is equal to y according to Z.equals.

> S.equals (Descending ([1, 2, 3])) (Descending ([1, 2, 3]))
true

> S.equals (Descending ([1, 2, 3])) (Descending ([3, 2, 1]))
false

Descending (x) is less than or equal to Descending (y) iff y is less than or equal to x according to Z.lte (note the transposition of x and y).

> S.sort ([Descending (5), Descending (1), Descending (2)])
[Descending (5), Descending (2), Descending (1)]

concat (Descending (x)) (Descending (y)) is equivalent to Descending (concat (x) (y)).

> S.concat (Descending ([1, 2, 3])) (Descending ([4, 5, 6]))
Descending ([1, 2, 3, 4, 5, 6])

map (f) (Descending (x)) is equivalent to Descending (f (x)).

> S.map (Math.sqrt) (Descending (64))
Descending (8)

ap (Descending (f)) (Descending (x)) is equivalent to Descending (f (x)).

> S.ap (Descending (Math.sqrt)) (Descending (64))
Descending (8)

chain (f) (Descending (x)) is equivalent to f (x).

> S.chain (n => Descending (n + 1)) (Descending (99))
Descending (100)

reduce (f) (x) (Descending (y)) is equivalent to f (x) (y).

> S.reduce (S.concat) ([1, 2, 3]) (Descending ([4, 5, 6]))
[1, 2, 3, 4, 5, 6]

traverse (_) (f) (Descending (x)) is equivalent to map (Descending) (f (x)).

> S.traverse (Array) (x => [x + 1, x + 2, x + 3]) (Descending (100))
[Descending (101), Descending (102), Descending (103)]

extend (f) (Descending (x)) is equivalent to Descending (f (Descending (x))).

> S.extend (S.reduce (S.add) (1)) (Descending (99))
Descending (100)

extract (Descending (x)) is equivalent to x.

> S.extract (Descending (42))
42