[d24f17c] | 1 | import { empty as emptyR } from 'ramda';
|
---|
| 2 |
|
---|
| 3 | import * as fl from './mapping';
|
---|
| 4 | import {
|
---|
| 5 | applyTrait,
|
---|
| 6 | functorTrait,
|
---|
| 7 | setoidTrait,
|
---|
| 8 | semigroupTrait,
|
---|
| 9 | chainTrait,
|
---|
| 10 | ordTrait,
|
---|
| 11 | } from './traits';
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * The simplest {@link https://github.com/fantasyland/fantasy-land|fantasy-land}
|
---|
| 15 | * compatible monad which attaches no information to values.
|
---|
| 16 | *
|
---|
| 17 | * The Identity type is a very simple type that has no interesting side effects and
|
---|
| 18 | * is effectively just a container of some value. So why does it exist ?
|
---|
| 19 | * The Identity type is often used as the base monad of a monad
|
---|
| 20 | * transformer when no other behaviour is required.
|
---|
| 21 | *
|
---|
| 22 | * @memberOf RA
|
---|
| 23 | * @implements
|
---|
| 24 | * {@link https://github.com/fantasyland/fantasy-land#apply|Apply},
|
---|
| 25 | * {@link https://github.com/fantasyland/fantasy-land#applicative|Applicative},
|
---|
| 26 | * {@link https://github.com/fantasyland/fantasy-land#functor|Functor},
|
---|
| 27 | * {@link https://github.com/fantasyland/fantasy-land#setoid|Setoid},
|
---|
| 28 | * {@link https://github.com/fantasyland/fantasy-land#semigroup|Semigroup},
|
---|
| 29 | * {@link https://github.com/fantasyland/fantasy-land#chain|Chain},
|
---|
| 30 | * {@link https://github.com/fantasyland/fantasy-land#monad|Monad},
|
---|
| 31 | * {@link https://github.com/fantasyland/fantasy-land#ord|Ord},
|
---|
| 32 | * {@link https://github.com/fantasyland/fantasy-land#monoid|Monoid*},
|
---|
| 33 | * {@link https://github.com/fantasyland/fantasy-land#contravariant|Contravariant}
|
---|
| 34 | * @since {@link https://char0n.github.io/ramda-adjunct/1.8.0|v1.8.0}
|
---|
| 35 | */
|
---|
| 36 | class Identity {
|
---|
| 37 | /**
|
---|
| 38 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#applicative|Applicative} specification.
|
---|
| 39 | *
|
---|
| 40 | * @static
|
---|
| 41 | * @sig of :: Applicative f => a -> f a
|
---|
| 42 | * @param {*} value
|
---|
| 43 | * @returns {RA.Identity}
|
---|
| 44 | * @example
|
---|
| 45 | *
|
---|
| 46 | * const a = Identity.of(1); //=> Identity(1)
|
---|
| 47 | */
|
---|
| 48 | static [fl.of](value) {
|
---|
| 49 | return new Identity(value);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | static of(value) {
|
---|
| 53 | return new Identity(value);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * @static
|
---|
| 58 | */
|
---|
| 59 | static get ['@@type']() {
|
---|
| 60 | return 'RA/Identity';
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Private constructor. Use {@link RA.Identity.of|Identity.of} instead.
|
---|
| 65 | *
|
---|
| 66 | * @param {*} value
|
---|
| 67 | * @return {RA.Identity}
|
---|
| 68 | */
|
---|
| 69 | constructor(value) {
|
---|
| 70 | this.value = value;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * Catamorphism for a value.
|
---|
| 75 | * @returns {*}
|
---|
| 76 | * @example
|
---|
| 77 | *
|
---|
| 78 | * const a = Identity.of(1);
|
---|
| 79 | * a.get(); //=> 1
|
---|
| 80 | */
|
---|
| 81 | get() {
|
---|
| 82 | return this.value;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#apply|Apply} specification.
|
---|
| 87 | *
|
---|
| 88 | * @sig ap :: Apply f => f a ~> f (a -> b) -> f b
|
---|
| 89 | * @param {RA.Identity} applyWithFn
|
---|
| 90 | * @return {RA.Identity}
|
---|
| 91 | * @example
|
---|
| 92 | *
|
---|
| 93 | * const a = Identity.of(1);
|
---|
| 94 | * const b = Identity.of(1).map(a => b => a + b);
|
---|
| 95 | *
|
---|
| 96 | * a.ap(b); //=> Identity(2)
|
---|
| 97 | */
|
---|
| 98 | [fl.ap](applyWithFn) {
|
---|
| 99 | return applyTrait[fl.ap].call(this, applyWithFn);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | ap(applyWithFn) {
|
---|
| 103 | return this[fl.ap](applyWithFn);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#functor|Functor} specification.
|
---|
| 108 | *
|
---|
| 109 | * @sig map :: Functor f => f a ~> (a -> b) -> f b
|
---|
| 110 | * @param {Function} fn
|
---|
| 111 | * @return {RA.Identity}
|
---|
| 112 | * @example
|
---|
| 113 | *
|
---|
| 114 | * const a = Identity.of(1);
|
---|
| 115 | * a.map(a => a + 1); //=> Identity(2)
|
---|
| 116 | */
|
---|
| 117 | [fl.map](fn) {
|
---|
| 118 | return functorTrait[fl.map].call(this, fn);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | map(fn) {
|
---|
| 122 | return this[fl.map](fn);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#setoid|Setoid} specification.
|
---|
| 127 | *
|
---|
| 128 | * @sig equals :: Setoid a => a ~> a -> Boolean
|
---|
| 129 | * @param {RA.Identity} setoid
|
---|
| 130 | * @return {boolean}
|
---|
| 131 | * @example
|
---|
| 132 | *
|
---|
| 133 | * const a = Identity.of(1);
|
---|
| 134 | * const b = Identity.of(1);
|
---|
| 135 | * const c = Identity.of(2);
|
---|
| 136 | *
|
---|
| 137 | * a.equals(b); //=> true
|
---|
| 138 | * a.equals(c); //=> false
|
---|
| 139 | */
|
---|
| 140 | [fl.equals](setoid) {
|
---|
| 141 | return setoidTrait[fl.equals].call(this, setoid);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | equals(setoid) {
|
---|
| 145 | return this[fl.equals](setoid);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#semigroup|Semigroup} specification.
|
---|
| 150 | *
|
---|
| 151 | * @sig concat :: Semigroup a => a ~> a -> a
|
---|
| 152 | * @param {RA.Identity} semigroup
|
---|
| 153 | * @return {RA.Identity}
|
---|
| 154 | * @example
|
---|
| 155 | *
|
---|
| 156 | * const a = Identity.of(1);
|
---|
| 157 | * const b = Identity.of(1);
|
---|
| 158 | * a.concat(b); //=> 2
|
---|
| 159 | *
|
---|
| 160 | * const c = Identity.of('c');
|
---|
| 161 | * const d = Identity.of('d');
|
---|
| 162 | * c.concat(d); //=> 'cd'
|
---|
| 163 | *
|
---|
| 164 | * const e = Identity.of(['e']);
|
---|
| 165 | * const f = Identity.of(['f']);
|
---|
| 166 | * e.concat(f); //=> ['e', 'f']
|
---|
| 167 | */
|
---|
| 168 | [fl.concat](semigroup) {
|
---|
| 169 | return semigroupTrait[fl.concat].call(this, semigroup);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | concat(semigroup) {
|
---|
| 173 | return this[fl.concat](semigroup);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#chain|Chain} specification.
|
---|
| 178 | *
|
---|
| 179 | * @sig chain :: Chain m => m a ~> (a -> m b) -> m b
|
---|
| 180 | * @param {Function} fn Function returning the value of the same {@link https://github.com/fantasyland/fantasy-land#semigroup|Chain}
|
---|
| 181 | * @return {RA.Identity}
|
---|
| 182 | * @example
|
---|
| 183 | *
|
---|
| 184 | * const a = Identity.of(1);
|
---|
| 185 | * const fn = val => Identity.of(val + 1);
|
---|
| 186 | *
|
---|
| 187 | * a.chain(fn).chain(fn); //=> Identity(3)
|
---|
| 188 | */
|
---|
| 189 | [fl.chain](fn) {
|
---|
| 190 | return chainTrait[fl.chain].call(this, fn);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | chain(fn) {
|
---|
| 194 | return this[fl.chain](fn);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#ord|Ord} specification.
|
---|
| 199 | *
|
---|
| 200 | * @sig lte :: Ord a => a ~> a -> Boolean
|
---|
| 201 | * @param {RA.Identity} ord
|
---|
| 202 | * @return {boolean}
|
---|
| 203 | * @example
|
---|
| 204 | *
|
---|
| 205 | * const a = Identity.of(1);
|
---|
| 206 | * const b = Identity.of(1);
|
---|
| 207 | * const c = Identity.of(2);
|
---|
| 208 | *
|
---|
| 209 | * a.lte(b); //=> true
|
---|
| 210 | * a.lte(c); //=> true
|
---|
| 211 | * c.lte(a); //=> false
|
---|
| 212 | */
|
---|
| 213 | [fl.lte](ord) {
|
---|
| 214 | return ordTrait[fl.lte].call(this, ord);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | lte(ord) {
|
---|
| 218 | return this[fl.lte](ord);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /**
|
---|
| 222 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#monoid|Monoid*} specification.
|
---|
| 223 | * Partial implementation of Monoid specification. `empty` method on instance only, returning
|
---|
| 224 | * identity value of the wrapped type. Using `R.empty` under the hood.
|
---|
| 225 | *
|
---|
| 226 | *
|
---|
| 227 | * @sig empty :: Monoid m => () -> m
|
---|
| 228 | * @return {RA.Identity}
|
---|
| 229 | * @example
|
---|
| 230 | *
|
---|
| 231 | * const a = Identity.of('test');
|
---|
| 232 | * const i = a.empty();
|
---|
| 233 | *
|
---|
| 234 | * a.concat(i); //=> Identity('string');
|
---|
| 235 | * i.concat(a); //=> Identity('string');
|
---|
| 236 | */
|
---|
| 237 | [fl.empty]() {
|
---|
| 238 | return this.constructor.of(emptyR(this.value));
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | empty() {
|
---|
| 242 | return this[fl.empty]();
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
| 246 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#contravariant|Contravariant} specification.
|
---|
| 247 | *
|
---|
| 248 | * @sig contramap :: Contravariant f => f a ~> (b -> a) -> f b
|
---|
| 249 | * @param {Function} fn
|
---|
| 250 | * @return {RA.Identity}
|
---|
| 251 | * @example
|
---|
| 252 | *
|
---|
| 253 | * const identity = a => a;
|
---|
| 254 | * const add1 = a => a + 1;
|
---|
| 255 | * const divide2 = a => a / 2;
|
---|
| 256 | *
|
---|
| 257 | * Identity.of(divide2).contramap(add1).get()(3); //=> 2
|
---|
| 258 | * Identity.of(identity).contramap(divide2).contramap(add1).get()(3); //=> 2
|
---|
| 259 | * Identity.of(identity).contramap(a => divide2(add1(a))).get()(3); //=> 2
|
---|
| 260 | */
|
---|
| 261 | [fl.contramap](fn) {
|
---|
| 262 | return this.constructor.of((value) => this.value(fn(value)));
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | contramap(fn) {
|
---|
| 266 | return this[fl.contramap](fn);
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | export default Identity;
|
---|