index.es.js 5.3 KB

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