index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (factory((global.mostPrelude = {})));
  5. }(this, (function (exports) { 'use strict';
  6. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  7. // Non-mutating array operations
  8. // cons :: a -> [a] -> [a]
  9. // a with x prepended
  10. function cons(x, a) {
  11. var l = a.length;
  12. var b = new Array(l + 1);
  13. b[0] = x;
  14. for (var i = 0; i < l; ++i) {
  15. b[i + 1] = a[i];
  16. }
  17. return b;
  18. }
  19. // append :: a -> [a] -> [a]
  20. // a with x appended
  21. function append(x, a) {
  22. var l = a.length;
  23. var b = new Array(l + 1);
  24. for (var i = 0; i < l; ++i) {
  25. b[i] = a[i];
  26. }
  27. b[l] = x;
  28. return b;
  29. }
  30. // drop :: Int -> [a] -> [a]
  31. // drop first n elements
  32. function drop(n, a) {
  33. // eslint-disable-line complexity
  34. if (n < 0) {
  35. throw new TypeError('n must be >= 0');
  36. }
  37. var l = a.length;
  38. if (n === 0 || l === 0) {
  39. return a;
  40. }
  41. if (n >= l) {
  42. return [];
  43. }
  44. return unsafeDrop(n, a, l - n);
  45. }
  46. // unsafeDrop :: Int -> [a] -> Int -> [a]
  47. // Internal helper for drop
  48. function unsafeDrop(n, a, l) {
  49. var b = new Array(l);
  50. for (var i = 0; i < l; ++i) {
  51. b[i] = a[n + i];
  52. }
  53. return b;
  54. }
  55. // tail :: [a] -> [a]
  56. // drop head element
  57. function tail(a) {
  58. return drop(1, a);
  59. }
  60. // copy :: [a] -> [a]
  61. // duplicate a (shallow duplication)
  62. function copy(a) {
  63. var l = a.length;
  64. var b = new Array(l);
  65. for (var i = 0; i < l; ++i) {
  66. b[i] = a[i];
  67. }
  68. return b;
  69. }
  70. // map :: (a -> b) -> [a] -> [b]
  71. // transform each element with f
  72. function map(f, a) {
  73. var l = a.length;
  74. var b = new Array(l);
  75. for (var i = 0; i < l; ++i) {
  76. b[i] = f(a[i]);
  77. }
  78. return b;
  79. }
  80. // reduce :: (a -> b -> a) -> a -> [b] -> a
  81. // accumulate via left-fold
  82. function reduce(f, z, a) {
  83. var r = z;
  84. for (var i = 0, l = a.length; i < l; ++i) {
  85. r = f(r, a[i], i);
  86. }
  87. return r;
  88. }
  89. // replace :: a -> Int -> [a]
  90. // replace element at index
  91. function replace(x, i, a) {
  92. // eslint-disable-line complexity
  93. if (i < 0) {
  94. throw new TypeError('i must be >= 0');
  95. }
  96. var l = a.length;
  97. var b = new Array(l);
  98. for (var j = 0; j < l; ++j) {
  99. b[j] = i === j ? x : a[j];
  100. }
  101. return b;
  102. }
  103. // remove :: Int -> [a] -> [a]
  104. // remove element at index
  105. function remove(i, a) {
  106. // eslint-disable-line complexity
  107. if (i < 0) {
  108. throw new TypeError('i must be >= 0');
  109. }
  110. var l = a.length;
  111. if (l === 0 || i >= l) {
  112. // exit early if index beyond end of array
  113. return a;
  114. }
  115. if (l === 1) {
  116. // exit early if index in bounds and length === 1
  117. return [];
  118. }
  119. return unsafeRemove(i, a, l - 1);
  120. }
  121. // unsafeRemove :: Int -> [a] -> Int -> [a]
  122. // Internal helper to remove element at index
  123. function unsafeRemove(i, a, l) {
  124. var b = new Array(l);
  125. var j = void 0;
  126. for (j = 0; j < i; ++j) {
  127. b[j] = a[j];
  128. }
  129. for (j = i; j < l; ++j) {
  130. b[j] = a[j + 1];
  131. }
  132. return b;
  133. }
  134. // removeAll :: (a -> boolean) -> [a] -> [a]
  135. // remove all elements matching a predicate
  136. // @deprecated
  137. function removeAll(f, a) {
  138. var l = a.length;
  139. var b = new Array(l);
  140. var j = 0;
  141. for (var x, i = 0; i < l; ++i) {
  142. x = a[i];
  143. if (!f(x)) {
  144. b[j] = x;
  145. ++j;
  146. }
  147. }
  148. b.length = j;
  149. return b;
  150. }
  151. // findIndex :: a -> [a] -> Int
  152. // find index of x in a, from the left
  153. function findIndex(x, a) {
  154. for (var i = 0, l = a.length; i < l; ++i) {
  155. if (x === a[i]) {
  156. return i;
  157. }
  158. }
  159. return -1;
  160. }
  161. // isArrayLike :: * -> boolean
  162. // Return true iff x is array-like
  163. function isArrayLike(x) {
  164. return x != null && typeof x.length === 'number' && typeof x !== 'function';
  165. }
  166. /** @license MIT License (c) copyright 2010-2016 original author or authors */
  167. // id :: a -> a
  168. var id = function id(x) {
  169. return x;
  170. };
  171. // compose :: (b -> c) -> (a -> b) -> (a -> c)
  172. var compose = function compose(f, g) {
  173. return function (x) {
  174. return f(g(x));
  175. };
  176. };
  177. // apply :: (a -> b) -> a -> b
  178. var apply = function apply(f, x) {
  179. return f(x);
  180. };
  181. // curry2 :: ((a, b) -> c) -> (a -> b -> c)
  182. function curry2(f) {
  183. function curried(a, b) {
  184. switch (arguments.length) {
  185. case 0:
  186. return curried;
  187. case 1:
  188. return function (b) {
  189. return f(a, b);
  190. };
  191. default:
  192. return f(a, b);
  193. }
  194. }
  195. return curried;
  196. }
  197. // curry3 :: ((a, b, c) -> d) -> (a -> b -> c -> d)
  198. function curry3(f) {
  199. function curried(a, b, c) {
  200. // eslint-disable-line complexity
  201. switch (arguments.length) {
  202. case 0:
  203. return curried;
  204. case 1:
  205. return curry2(function (b, c) {
  206. return f(a, b, c);
  207. });
  208. case 2:
  209. return function (c) {
  210. return f(a, b, c);
  211. };
  212. default:
  213. return f(a, b, c);
  214. }
  215. }
  216. return curried;
  217. }
  218. // curry4 :: ((a, b, c, d) -> e) -> (a -> b -> c -> d -> e)
  219. function curry4(f) {
  220. function curried(a, b, c, d) {
  221. // eslint-disable-line complexity
  222. switch (arguments.length) {
  223. case 0:
  224. return curried;
  225. case 1:
  226. return curry3(function (b, c, d) {
  227. return f(a, b, c, d);
  228. });
  229. case 2:
  230. return curry2(function (c, d) {
  231. return f(a, b, c, d);
  232. });
  233. case 3:
  234. return function (d) {
  235. return f(a, b, c, d);
  236. };
  237. default:
  238. return f(a, b, c, d);
  239. }
  240. }
  241. return curried;
  242. }
  243. /** @license MIT License (c) copyright 2016 original author or authors */
  244. exports.cons = cons;
  245. exports.append = append;
  246. exports.drop = drop;
  247. exports.tail = tail;
  248. exports.copy = copy;
  249. exports.map = map;
  250. exports.reduce = reduce;
  251. exports.replace = replace;
  252. exports.remove = remove;
  253. exports.removeAll = removeAll;
  254. exports.findIndex = findIndex;
  255. exports.isArrayLike = isArrayLike;
  256. exports.id = id;
  257. exports.compose = compose;
  258. exports.apply = apply;
  259. exports.curry2 = curry2;
  260. exports.curry3 = curry3;
  261. exports.curry4 = curry4;
  262. Object.defineProperty(exports, '__esModule', { value: true });
  263. })));
  264. //# sourceMappingURL=index.js.map