List.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // Generated by LiveScript 1.4.0
  2. var each, map, compact, filter, reject, partition, find, head, first, tail, last, initial, empty, reverse, unique, uniqueBy, fold, foldl, fold1, foldl1, foldr, foldr1, unfoldr, concat, concatMap, flatten, difference, intersection, union, countBy, groupBy, andList, orList, any, all, sort, sortWith, sortBy, sum, product, mean, average, maximum, minimum, maximumBy, minimumBy, scan, scanl, scan1, scanl1, scanr, scanr1, slice, take, drop, splitAt, takeWhile, dropWhile, span, breakList, zip, zipWith, zipAll, zipAllWith, at, elemIndex, elemIndices, findIndex, findIndices, toString$ = {}.toString, slice$ = [].slice;
  3. each = curry$(function(f, xs){
  4. var i$, len$, x;
  5. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  6. x = xs[i$];
  7. f(x);
  8. }
  9. return xs;
  10. });
  11. map = curry$(function(f, xs){
  12. var i$, len$, x, results$ = [];
  13. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  14. x = xs[i$];
  15. results$.push(f(x));
  16. }
  17. return results$;
  18. });
  19. compact = function(xs){
  20. var i$, len$, x, results$ = [];
  21. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  22. x = xs[i$];
  23. if (x) {
  24. results$.push(x);
  25. }
  26. }
  27. return results$;
  28. };
  29. filter = curry$(function(f, xs){
  30. var i$, len$, x, results$ = [];
  31. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  32. x = xs[i$];
  33. if (f(x)) {
  34. results$.push(x);
  35. }
  36. }
  37. return results$;
  38. });
  39. reject = curry$(function(f, xs){
  40. var i$, len$, x, results$ = [];
  41. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  42. x = xs[i$];
  43. if (!f(x)) {
  44. results$.push(x);
  45. }
  46. }
  47. return results$;
  48. });
  49. partition = curry$(function(f, xs){
  50. var passed, failed, i$, len$, x;
  51. passed = [];
  52. failed = [];
  53. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  54. x = xs[i$];
  55. (f(x) ? passed : failed).push(x);
  56. }
  57. return [passed, failed];
  58. });
  59. find = curry$(function(f, xs){
  60. var i$, len$, x;
  61. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  62. x = xs[i$];
  63. if (f(x)) {
  64. return x;
  65. }
  66. }
  67. });
  68. head = first = function(xs){
  69. return xs[0];
  70. };
  71. tail = function(xs){
  72. if (!xs.length) {
  73. return;
  74. }
  75. return xs.slice(1);
  76. };
  77. last = function(xs){
  78. return xs[xs.length - 1];
  79. };
  80. initial = function(xs){
  81. if (!xs.length) {
  82. return;
  83. }
  84. return xs.slice(0, -1);
  85. };
  86. empty = function(xs){
  87. return !xs.length;
  88. };
  89. reverse = function(xs){
  90. return xs.concat().reverse();
  91. };
  92. unique = function(xs){
  93. var result, i$, len$, x;
  94. result = [];
  95. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  96. x = xs[i$];
  97. if (!in$(x, result)) {
  98. result.push(x);
  99. }
  100. }
  101. return result;
  102. };
  103. uniqueBy = curry$(function(f, xs){
  104. var seen, i$, len$, x, val, results$ = [];
  105. seen = [];
  106. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  107. x = xs[i$];
  108. val = f(x);
  109. if (in$(val, seen)) {
  110. continue;
  111. }
  112. seen.push(val);
  113. results$.push(x);
  114. }
  115. return results$;
  116. });
  117. fold = foldl = curry$(function(f, memo, xs){
  118. var i$, len$, x;
  119. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  120. x = xs[i$];
  121. memo = f(memo, x);
  122. }
  123. return memo;
  124. });
  125. fold1 = foldl1 = curry$(function(f, xs){
  126. return fold(f, xs[0], xs.slice(1));
  127. });
  128. foldr = curry$(function(f, memo, xs){
  129. var i$, x;
  130. for (i$ = xs.length - 1; i$ >= 0; --i$) {
  131. x = xs[i$];
  132. memo = f(x, memo);
  133. }
  134. return memo;
  135. });
  136. foldr1 = curry$(function(f, xs){
  137. return foldr(f, xs[xs.length - 1], xs.slice(0, -1));
  138. });
  139. unfoldr = curry$(function(f, b){
  140. var result, x, that;
  141. result = [];
  142. x = b;
  143. while ((that = f(x)) != null) {
  144. result.push(that[0]);
  145. x = that[1];
  146. }
  147. return result;
  148. });
  149. concat = function(xss){
  150. return [].concat.apply([], xss);
  151. };
  152. concatMap = curry$(function(f, xs){
  153. var x;
  154. return [].concat.apply([], (function(){
  155. var i$, ref$, len$, results$ = [];
  156. for (i$ = 0, len$ = (ref$ = xs).length; i$ < len$; ++i$) {
  157. x = ref$[i$];
  158. results$.push(f(x));
  159. }
  160. return results$;
  161. }()));
  162. });
  163. flatten = function(xs){
  164. var x;
  165. return [].concat.apply([], (function(){
  166. var i$, ref$, len$, results$ = [];
  167. for (i$ = 0, len$ = (ref$ = xs).length; i$ < len$; ++i$) {
  168. x = ref$[i$];
  169. if (toString$.call(x).slice(8, -1) === 'Array') {
  170. results$.push(flatten(x));
  171. } else {
  172. results$.push(x);
  173. }
  174. }
  175. return results$;
  176. }()));
  177. };
  178. difference = function(xs){
  179. var yss, results, i$, len$, x, j$, len1$, ys;
  180. yss = slice$.call(arguments, 1);
  181. results = [];
  182. outer: for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  183. x = xs[i$];
  184. for (j$ = 0, len1$ = yss.length; j$ < len1$; ++j$) {
  185. ys = yss[j$];
  186. if (in$(x, ys)) {
  187. continue outer;
  188. }
  189. }
  190. results.push(x);
  191. }
  192. return results;
  193. };
  194. intersection = function(xs){
  195. var yss, results, i$, len$, x, j$, len1$, ys;
  196. yss = slice$.call(arguments, 1);
  197. results = [];
  198. outer: for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  199. x = xs[i$];
  200. for (j$ = 0, len1$ = yss.length; j$ < len1$; ++j$) {
  201. ys = yss[j$];
  202. if (!in$(x, ys)) {
  203. continue outer;
  204. }
  205. }
  206. results.push(x);
  207. }
  208. return results;
  209. };
  210. union = function(){
  211. var xss, results, i$, len$, xs, j$, len1$, x;
  212. xss = slice$.call(arguments);
  213. results = [];
  214. for (i$ = 0, len$ = xss.length; i$ < len$; ++i$) {
  215. xs = xss[i$];
  216. for (j$ = 0, len1$ = xs.length; j$ < len1$; ++j$) {
  217. x = xs[j$];
  218. if (!in$(x, results)) {
  219. results.push(x);
  220. }
  221. }
  222. }
  223. return results;
  224. };
  225. countBy = curry$(function(f, xs){
  226. var results, i$, len$, x, key;
  227. results = {};
  228. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  229. x = xs[i$];
  230. key = f(x);
  231. if (key in results) {
  232. results[key] += 1;
  233. } else {
  234. results[key] = 1;
  235. }
  236. }
  237. return results;
  238. });
  239. groupBy = curry$(function(f, xs){
  240. var results, i$, len$, x, key;
  241. results = {};
  242. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  243. x = xs[i$];
  244. key = f(x);
  245. if (key in results) {
  246. results[key].push(x);
  247. } else {
  248. results[key] = [x];
  249. }
  250. }
  251. return results;
  252. });
  253. andList = function(xs){
  254. var i$, len$, x;
  255. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  256. x = xs[i$];
  257. if (!x) {
  258. return false;
  259. }
  260. }
  261. return true;
  262. };
  263. orList = function(xs){
  264. var i$, len$, x;
  265. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  266. x = xs[i$];
  267. if (x) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. };
  273. any = curry$(function(f, xs){
  274. var i$, len$, x;
  275. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  276. x = xs[i$];
  277. if (f(x)) {
  278. return true;
  279. }
  280. }
  281. return false;
  282. });
  283. all = curry$(function(f, xs){
  284. var i$, len$, x;
  285. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  286. x = xs[i$];
  287. if (!f(x)) {
  288. return false;
  289. }
  290. }
  291. return true;
  292. });
  293. sort = function(xs){
  294. return xs.concat().sort(function(x, y){
  295. if (x > y) {
  296. return 1;
  297. } else if (x < y) {
  298. return -1;
  299. } else {
  300. return 0;
  301. }
  302. });
  303. };
  304. sortWith = curry$(function(f, xs){
  305. return xs.concat().sort(f);
  306. });
  307. sortBy = curry$(function(f, xs){
  308. return xs.concat().sort(function(x, y){
  309. if (f(x) > f(y)) {
  310. return 1;
  311. } else if (f(x) < f(y)) {
  312. return -1;
  313. } else {
  314. return 0;
  315. }
  316. });
  317. });
  318. sum = function(xs){
  319. var result, i$, len$, x;
  320. result = 0;
  321. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  322. x = xs[i$];
  323. result += x;
  324. }
  325. return result;
  326. };
  327. product = function(xs){
  328. var result, i$, len$, x;
  329. result = 1;
  330. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  331. x = xs[i$];
  332. result *= x;
  333. }
  334. return result;
  335. };
  336. mean = average = function(xs){
  337. var sum, i$, len$, x;
  338. sum = 0;
  339. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  340. x = xs[i$];
  341. sum += x;
  342. }
  343. return sum / xs.length;
  344. };
  345. maximum = function(xs){
  346. var max, i$, ref$, len$, x;
  347. max = xs[0];
  348. for (i$ = 0, len$ = (ref$ = xs.slice(1)).length; i$ < len$; ++i$) {
  349. x = ref$[i$];
  350. if (x > max) {
  351. max = x;
  352. }
  353. }
  354. return max;
  355. };
  356. minimum = function(xs){
  357. var min, i$, ref$, len$, x;
  358. min = xs[0];
  359. for (i$ = 0, len$ = (ref$ = xs.slice(1)).length; i$ < len$; ++i$) {
  360. x = ref$[i$];
  361. if (x < min) {
  362. min = x;
  363. }
  364. }
  365. return min;
  366. };
  367. maximumBy = curry$(function(f, xs){
  368. var max, i$, ref$, len$, x;
  369. max = xs[0];
  370. for (i$ = 0, len$ = (ref$ = xs.slice(1)).length; i$ < len$; ++i$) {
  371. x = ref$[i$];
  372. if (f(x) > f(max)) {
  373. max = x;
  374. }
  375. }
  376. return max;
  377. });
  378. minimumBy = curry$(function(f, xs){
  379. var min, i$, ref$, len$, x;
  380. min = xs[0];
  381. for (i$ = 0, len$ = (ref$ = xs.slice(1)).length; i$ < len$; ++i$) {
  382. x = ref$[i$];
  383. if (f(x) < f(min)) {
  384. min = x;
  385. }
  386. }
  387. return min;
  388. });
  389. scan = scanl = curry$(function(f, memo, xs){
  390. var last, x;
  391. last = memo;
  392. return [memo].concat((function(){
  393. var i$, ref$, len$, results$ = [];
  394. for (i$ = 0, len$ = (ref$ = xs).length; i$ < len$; ++i$) {
  395. x = ref$[i$];
  396. results$.push(last = f(last, x));
  397. }
  398. return results$;
  399. }()));
  400. });
  401. scan1 = scanl1 = curry$(function(f, xs){
  402. if (!xs.length) {
  403. return;
  404. }
  405. return scan(f, xs[0], xs.slice(1));
  406. });
  407. scanr = curry$(function(f, memo, xs){
  408. xs = xs.concat().reverse();
  409. return scan(f, memo, xs).reverse();
  410. });
  411. scanr1 = curry$(function(f, xs){
  412. if (!xs.length) {
  413. return;
  414. }
  415. xs = xs.concat().reverse();
  416. return scan(f, xs[0], xs.slice(1)).reverse();
  417. });
  418. slice = curry$(function(x, y, xs){
  419. return xs.slice(x, y);
  420. });
  421. take = curry$(function(n, xs){
  422. if (n <= 0) {
  423. return xs.slice(0, 0);
  424. } else {
  425. return xs.slice(0, n);
  426. }
  427. });
  428. drop = curry$(function(n, xs){
  429. if (n <= 0) {
  430. return xs;
  431. } else {
  432. return xs.slice(n);
  433. }
  434. });
  435. splitAt = curry$(function(n, xs){
  436. return [take(n, xs), drop(n, xs)];
  437. });
  438. takeWhile = curry$(function(p, xs){
  439. var len, i;
  440. len = xs.length;
  441. if (!len) {
  442. return xs;
  443. }
  444. i = 0;
  445. while (i < len && p(xs[i])) {
  446. i += 1;
  447. }
  448. return xs.slice(0, i);
  449. });
  450. dropWhile = curry$(function(p, xs){
  451. var len, i;
  452. len = xs.length;
  453. if (!len) {
  454. return xs;
  455. }
  456. i = 0;
  457. while (i < len && p(xs[i])) {
  458. i += 1;
  459. }
  460. return xs.slice(i);
  461. });
  462. span = curry$(function(p, xs){
  463. return [takeWhile(p, xs), dropWhile(p, xs)];
  464. });
  465. breakList = curry$(function(p, xs){
  466. return span(compose$(p, not$), xs);
  467. });
  468. zip = curry$(function(xs, ys){
  469. var result, len, i$, len$, i, x;
  470. result = [];
  471. len = ys.length;
  472. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  473. i = i$;
  474. x = xs[i$];
  475. if (i === len) {
  476. break;
  477. }
  478. result.push([x, ys[i]]);
  479. }
  480. return result;
  481. });
  482. zipWith = curry$(function(f, xs, ys){
  483. var result, len, i$, len$, i, x;
  484. result = [];
  485. len = ys.length;
  486. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  487. i = i$;
  488. x = xs[i$];
  489. if (i === len) {
  490. break;
  491. }
  492. result.push(f(x, ys[i]));
  493. }
  494. return result;
  495. });
  496. zipAll = function(){
  497. var xss, minLength, i$, len$, xs, ref$, i, lresult$, j$, results$ = [];
  498. xss = slice$.call(arguments);
  499. minLength = undefined;
  500. for (i$ = 0, len$ = xss.length; i$ < len$; ++i$) {
  501. xs = xss[i$];
  502. minLength <= (ref$ = xs.length) || (minLength = ref$);
  503. }
  504. for (i$ = 0; i$ < minLength; ++i$) {
  505. i = i$;
  506. lresult$ = [];
  507. for (j$ = 0, len$ = xss.length; j$ < len$; ++j$) {
  508. xs = xss[j$];
  509. lresult$.push(xs[i]);
  510. }
  511. results$.push(lresult$);
  512. }
  513. return results$;
  514. };
  515. zipAllWith = function(f){
  516. var xss, minLength, i$, len$, xs, ref$, i, results$ = [];
  517. xss = slice$.call(arguments, 1);
  518. minLength = undefined;
  519. for (i$ = 0, len$ = xss.length; i$ < len$; ++i$) {
  520. xs = xss[i$];
  521. minLength <= (ref$ = xs.length) || (minLength = ref$);
  522. }
  523. for (i$ = 0; i$ < minLength; ++i$) {
  524. i = i$;
  525. results$.push(f.apply(null, (fn$())));
  526. }
  527. return results$;
  528. function fn$(){
  529. var i$, ref$, len$, results$ = [];
  530. for (i$ = 0, len$ = (ref$ = xss).length; i$ < len$; ++i$) {
  531. xs = ref$[i$];
  532. results$.push(xs[i]);
  533. }
  534. return results$;
  535. }
  536. };
  537. at = curry$(function(n, xs){
  538. if (n < 0) {
  539. return xs[xs.length + n];
  540. } else {
  541. return xs[n];
  542. }
  543. });
  544. elemIndex = curry$(function(el, xs){
  545. var i$, len$, i, x;
  546. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  547. i = i$;
  548. x = xs[i$];
  549. if (x === el) {
  550. return i;
  551. }
  552. }
  553. });
  554. elemIndices = curry$(function(el, xs){
  555. var i$, len$, i, x, results$ = [];
  556. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  557. i = i$;
  558. x = xs[i$];
  559. if (x === el) {
  560. results$.push(i);
  561. }
  562. }
  563. return results$;
  564. });
  565. findIndex = curry$(function(f, xs){
  566. var i$, len$, i, x;
  567. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  568. i = i$;
  569. x = xs[i$];
  570. if (f(x)) {
  571. return i;
  572. }
  573. }
  574. });
  575. findIndices = curry$(function(f, xs){
  576. var i$, len$, i, x, results$ = [];
  577. for (i$ = 0, len$ = xs.length; i$ < len$; ++i$) {
  578. i = i$;
  579. x = xs[i$];
  580. if (f(x)) {
  581. results$.push(i);
  582. }
  583. }
  584. return results$;
  585. });
  586. module.exports = {
  587. each: each,
  588. map: map,
  589. filter: filter,
  590. compact: compact,
  591. reject: reject,
  592. partition: partition,
  593. find: find,
  594. head: head,
  595. first: first,
  596. tail: tail,
  597. last: last,
  598. initial: initial,
  599. empty: empty,
  600. reverse: reverse,
  601. difference: difference,
  602. intersection: intersection,
  603. union: union,
  604. countBy: countBy,
  605. groupBy: groupBy,
  606. fold: fold,
  607. fold1: fold1,
  608. foldl: foldl,
  609. foldl1: foldl1,
  610. foldr: foldr,
  611. foldr1: foldr1,
  612. unfoldr: unfoldr,
  613. andList: andList,
  614. orList: orList,
  615. any: any,
  616. all: all,
  617. unique: unique,
  618. uniqueBy: uniqueBy,
  619. sort: sort,
  620. sortWith: sortWith,
  621. sortBy: sortBy,
  622. sum: sum,
  623. product: product,
  624. mean: mean,
  625. average: average,
  626. concat: concat,
  627. concatMap: concatMap,
  628. flatten: flatten,
  629. maximum: maximum,
  630. minimum: minimum,
  631. maximumBy: maximumBy,
  632. minimumBy: minimumBy,
  633. scan: scan,
  634. scan1: scan1,
  635. scanl: scanl,
  636. scanl1: scanl1,
  637. scanr: scanr,
  638. scanr1: scanr1,
  639. slice: slice,
  640. take: take,
  641. drop: drop,
  642. splitAt: splitAt,
  643. takeWhile: takeWhile,
  644. dropWhile: dropWhile,
  645. span: span,
  646. breakList: breakList,
  647. zip: zip,
  648. zipWith: zipWith,
  649. zipAll: zipAll,
  650. zipAllWith: zipAllWith,
  651. at: at,
  652. elemIndex: elemIndex,
  653. elemIndices: elemIndices,
  654. findIndex: findIndex,
  655. findIndices: findIndices
  656. };
  657. function curry$(f, bound){
  658. var context,
  659. _curry = function(args) {
  660. return f.length > 1 ? function(){
  661. var params = args ? args.concat() : [];
  662. context = bound ? context || this : this;
  663. return params.push.apply(params, arguments) <
  664. f.length && arguments.length ?
  665. _curry.call(context, params) : f.apply(context, params);
  666. } : f;
  667. };
  668. return _curry();
  669. }
  670. function in$(x, xs){
  671. var i = -1, l = xs.length >>> 0;
  672. while (++i < l) if (x === xs[i]) return true;
  673. return false;
  674. }
  675. function compose$() {
  676. var functions = arguments;
  677. return function() {
  678. var i, result;
  679. result = functions[0].apply(this, arguments);
  680. for (i = 1; i < functions.length; ++i) {
  681. result = functions[i](result);
  682. }
  683. return result;
  684. };
  685. }
  686. function not$(x){ return !x; }