Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sanctuary-js/sanctuary-descending

:rainbow: Fantasy Land -compliant Descending type
https://github.com/sanctuary-js/sanctuary-descending

fantasy-land sanctuary

Last synced: about 2 months ago
JSON representation

:rainbow: Fantasy Land -compliant Descending type

Awesome Lists containing this project

README

        

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.

```javascript
> 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:

```javascript
> 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][].

```javascript
> Descending (42)
Descending (42)
```

#### `Descending.fantasy-land/of :: a -⁠> Descending a`

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

```javascript
> S.of (Descending) (42)
Descending (42)
```

#### `Descending.fantasy-land/chainRec :: ((a -⁠> c, b -⁠> c, a) -⁠> Descending c, a) -⁠> Descending b`

```javascript
> 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) + ')'`.

```javascript
> 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`][].

```javascript
> 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`).

```javascript
> 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))`.

```javascript
> 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))`.

```javascript
> 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))`.

```javascript
> 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)`.

```javascript
> 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)`.

```javascript
> 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))`.

```javascript
> 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)))`.

```javascript
> 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`.

```javascript
> S.extract (Descending (42))
42
```

[Fantasy Land]: https://github.com/fantasyland/fantasy-land/tree/v4.0.1
[Identity]: https://github.com/sanctuary-js/sanctuary-identity
[`Z.equals`]: https://github.com/sanctuary-js/sanctuary-type-classes/tree/v12.1.0#equals
[`Z.lte`]: https://github.com/sanctuary-js/sanctuary-type-classes/tree/v12.1.0#lte
[iff]: https://en.wikipedia.org/wiki/If_and_only_if
[type representative]: https://github.com/fantasyland/fantasy-land/tree/v4.0.1#type-representatives