sanctuary-descending
v2.1.0
Published
Fantasy Land -compliant Descending type
Downloads
499
Readme
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 :: a -> Descending a
Descending's sole data constructor. Additionally, it serves as the Descending type representative.
> Descending (42)
Descending (42)
Descending.fantasy-land/of :: a -> Descending a
of (Descending) (x)
is equivalent to Descending (x)
.
> S.of (Descending) (42)
Descending (42)
Descending.fantasy-land/chainRec :: ((a -> c, b -> c, a) -> Descending c, a) -> Descending b
> 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)
Descending#@@show :: Showable a => Descending a ~> () -> String
show (Descending (x))
is equivalent to
'Descending (' + show (x) + ')'
.
> show (Descending (['foo', 'bar', 'baz']))
'Descending (["foo", "bar", "baz"])'
Descending#fantasy-land/equals :: Setoid a => Descending a ~> Descending a -> Boolean
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#fantasy-land/lte :: Ord a => Descending a ~> Descending a -> Boolean
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)]
Descending#fantasy-land/concat :: Semigroup a => Descending a ~> Descending a -> Descending a
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])
Descending#fantasy-land/map :: Descending a ~> (a -> b) -> Descending b
map (f) (Descending (x))
is equivalent to Descending (f (x))
.
> S.map (Math.sqrt) (Descending (64))
Descending (8)
Descending#fantasy-land/ap :: Descending a ~> Descending (a -> b) -> Descending b
ap (Descending (f)) (Descending (x))
is equivalent to
Descending (f (x))
.
> S.ap (Descending (Math.sqrt)) (Descending (64))
Descending (8)
Descending#fantasy-land/chain :: Descending a ~> (a -> Descending b) -> Descending b
chain (f) (Descending (x))
is equivalent to f (x)
.
> S.chain (n => Descending (n + 1)) (Descending (99))
Descending (100)
Descending#fantasy-land/reduce :: Descending a ~> ((b, a) -> b, b) -> b
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]
Descending#fantasy-land/traverse :: Applicative f => Descending a ~> (TypeRep f, a -> f b) -> f (Descending b)
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)]
Descending#fantasy-land/extend :: Descending a ~> (Descending a -> b) -> Descending b
extend (f) (Descending (x))
is equivalent to
Descending (f (Descending (x)))
.
> S.extend (S.reduce (S.add) (1)) (Descending (99))
Descending (100)
Descending#fantasy-land/extract :: Descending a ~> () -> a
extract (Descending (x))
is equivalent to x
.
> S.extract (Descending (42))
42