PageRenderTime 29ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/coffeegrains/src/com/syntazo/coffeegrains/Iteration.java

http://sourcebox.googlecode.com/
Java | 1037 lines | 637 code | 150 blank | 250 comment | 62 complexity | a66293db333ea7c62721f4edf49db077 MD5 | raw file
  1. package com.syntazo.coffeegrains;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6. import org.python.core.PyFile;
  7. public class Iteration {
  8. public final static Iterator<Object> Null = new Iterator<Object>() {
  9. public boolean hasNext() {
  10. return false;
  11. }
  12. public Object next() {
  13. return null;
  14. }
  15. public void remove() {
  16. }
  17. };
  18. public static abstract interface Lambda {
  19. public Object process(final Object... args);
  20. }
  21. public static class Tuple {
  22. public Object[] values;
  23. public Tuple(Object... args) {
  24. values = args;
  25. }
  26. @Override
  27. public String toString() {
  28. return "(" + str(values) + ")";
  29. }
  30. }
  31. public static class Enum {
  32. final public int index;
  33. final public Object value;
  34. public Enum(int index, Object value) {
  35. this.index = index;
  36. this.value = value;
  37. }
  38. @Override
  39. public String toString() {
  40. return "(" + index + "," + value + ")";
  41. }
  42. }
  43. public static Object[] tuple(final Object... args) {
  44. return args;
  45. }
  46. public static interface IteratorMaker<T> {
  47. public Iterator<T> iterator();
  48. }
  49. public static Iterable<Object> buildObjectIterable(
  50. final IteratorMaker<Object> iteratorMaker) {
  51. return new Iterable<Object>() {
  52. public Iterator<Object> iterator() {
  53. return iteratorMaker.iterator();
  54. }
  55. @Override
  56. public String toString() {
  57. return "(" + str(this) + ")";
  58. }
  59. };
  60. }
  61. public static Iterable<Integer> buildIntegerIterable(
  62. final IteratorMaker<Integer> iteratorMaker) {
  63. return new Iterable<Integer>() {
  64. public Iterator<Integer> iterator() {
  65. return iteratorMaker.iterator();
  66. }
  67. @Override
  68. public String toString() {
  69. return "(" + str(this) + ")";
  70. }
  71. };
  72. }
  73. /**
  74. * Returns an <code>Iterable</code> object from the <code>varargs</code> or a given array.
  75. * Should an array be passed, it will be used as the underlying.
  76. *
  77. * @param elements a vararg of objects or types.
  78. * @return an iterable.
  79. */
  80. public static Iterable iterable(final Object... elements) {
  81. return buildObjectIterable(new IteratorMaker<Object>() {
  82. public Iterator<Object> iterator() {
  83. return new Iterator<Object>() {
  84. int index = 0;
  85. public boolean hasNext() {
  86. return index < elements.length;
  87. }
  88. public Object next() {
  89. return elements[index++];
  90. }
  91. public void remove() {
  92. }
  93. };
  94. }
  95. });
  96. }
  97. /**
  98. * Returns an <code>Iterable</code> of Integers in the given the range from (0..stop] where the step is 1.
  99. * There is no underlying.
  100. *
  101. * @param stop, an integer.
  102. * @return an <code>Iterable</code> of Integers.
  103. *
  104. * @see range(start, stop, step)
  105. */
  106. public static Iterable<Integer> range(final int stop) {
  107. return range(0, stop);
  108. }
  109. /**
  110. * Returns an <code>Iterable</code> of Integers in the given the range from (start..stop] where the step is 1.
  111. * There is no underlying.
  112. *
  113. * @param start, an integer.
  114. * @param stop, an integer.
  115. * @return an <code>Iterable</code> of Integers.
  116. *
  117. * @see range(start, stop, step)
  118. */
  119. public static Iterable<Integer> range(final int start, final int stop) {
  120. return range(start, stop, 1);
  121. }
  122. /**
  123. * Returns an <code>Iterable</code> of Integers in the given the range from (start..stop] where the step is given.
  124. * There is no underlying.
  125. *
  126. * @param start, an integer.
  127. * @param stop, an integer.
  128. * @return an <code>Iterable</code> of Integers.
  129. *
  130. * @see range(start, stop, step)
  131. */
  132. public static Iterable<Integer> range(final int start, final int stop,
  133. final int step) {
  134. return buildIntegerIterable(new IteratorMaker<Integer>() {
  135. public Iterator<Integer> iterator() {
  136. return new Iterator<Integer>() {
  137. int index = start;
  138. public boolean hasNext() {
  139. return index < stop;
  140. }
  141. public Integer next() {
  142. int i = index;
  143. index += step;
  144. return i;
  145. }
  146. public void remove() {
  147. }
  148. };
  149. }
  150. });
  151. }
  152. /**
  153. * Returns an <code>Iterable</code> of <code>Enums</code> given an Iterable.
  154. *
  155. * @param iterable, an <code>Iterable</code>
  156. * @return an <code>Iterable</code> of <code>Enums</code>.
  157. *
  158. * @see <code>Enums</code>.
  159. */
  160. public static Iterable<Enum> enumerate(final Iterable<?> iterable) {
  161. return new Iterable<Enum>() {
  162. public Iterator<Enum> iterator() {
  163. return new Iterator<Enum>() {
  164. int index = 0;
  165. Iterator<?> iterator = iterable != null ? iterable
  166. .iterator() : Null;
  167. public boolean hasNext() {
  168. return iterator.hasNext();
  169. }
  170. public Enum next() {
  171. return new Enum(index++, iterator.next());
  172. }
  173. public void remove() {
  174. }
  175. };
  176. }
  177. @Override
  178. public String toString() {
  179. return "(" + str(this) + ")";
  180. }
  181. };
  182. }
  183. /**
  184. * Makes an iterable that returns selected elements from the underlying iterable.
  185. * If stop is -1, then iteration continues until the iterator is exhausted,
  186. * if at all; otherwise, it stops at the specified position.
  187. * Step = 1.
  188. *
  189. * @param iterable, an <code>Iterable</code>
  190. * @param stop.
  191. * @return an <code>Iterable</code>.
  192. *
  193. * @see <code>slice</code>.
  194. */
  195. public static Iterable<Object> slice(Iterable<?> iterable, int stop) {
  196. return slice(iterable, 0, stop);
  197. }
  198. /**
  199. * Makes an iterable that returns selected elements from the underlying iterable.
  200. * If start is non-zero, then elements from the iterable are skipped until start
  201. * is reached. Afterward, elements are returned consecutively unless step is set
  202. * higher than one which results in items being skipped.
  203. * If stop is -1, then iteration continues until the iterator is exhausted,
  204. * if at all; otherwise, it stops at the specified position.
  205. *
  206. * @param iterable, an <code>Iterable</code>
  207. * @param start.
  208. * @param stop.
  209. * @return an <code>Iterable</code>.
  210. *
  211. * @see <code>slice</code>.
  212. */
  213. public static Iterable<Object> slice(Iterable<?> iterable, int start,
  214. int stop) {
  215. return slice(iterable, start, stop, 1);
  216. }
  217. /**
  218. * Makes an iterable that returns selected elements from the underlying iterable.
  219. * If start is non-zero, then elements from the iterable are skipped until start
  220. * is reached. Afterward, elements are returned consecutively unless step is set
  221. * higher than one which results in items being skipped. *
  222. * @param iterable, an <code>Iterable</code>
  223. * @param start.
  224. * @param stop.
  225. * @param step.
  226. * @return an <code>Iterable</code>.
  227. *
  228. * @see <code>slice</code>.
  229. */
  230. public static Iterable<Object> slice(final Iterable<?> iterable,
  231. final int start, final int stop, final int step) {
  232. return buildObjectIterable(new IteratorMaker<Object>() {
  233. public Iterator<Object> iterator() {
  234. return new Iterator<Object>() {
  235. Iterator<Enum> iterator = enumerate(iterable).iterator();
  236. Iterator<Integer> range = range(start, stop, step)
  237. .iterator();
  238. public boolean hasNext() {
  239. return range.hasNext() && iterator.hasNext();
  240. }
  241. public Object next() {
  242. int i = range.next();
  243. do {
  244. Enum tuple = iterator.next();
  245. if (i == tuple.index)
  246. return tuple.value;
  247. } while (iterator.hasNext());
  248. throw new NoSuchElementException();
  249. }
  250. public void remove() {
  251. }
  252. };
  253. }
  254. });
  255. }
  256. /**
  257. * return an Iterable whose iterator will with every call to next apply lambda whose arguments will be an item from each given iterable.
  258. * Map will stops when the shortest iterable is exhausted.
  259. * @param lambda, an <code>Lamda</code>
  260. * @param iterable, an <code>Iterable</code>
  261. * @return an <code>Iterable</code>.
  262. * <pre>
  263. * -----------------------------------------------------
  264. * Collection<Object> a = toCollection(new ArrayList<Object>(), 1, 2, 3, 4, 5, 6, 3, 3, 10);
  265. * Iterable<Integer> b = range(2, 10, 3);
  266. * System.out.println("a = " + str(a));
  267. * System.out.println("b = " + b);
  268. *
  269. * System.out.println("testing map(lambda x: x*10, b): "
  270. * + map(new Lambda() {
  271. * public Object process(Object... args) {
  272. * return ((Integer) args[0]) * 10;
  273. * }
  274. * }, b));
  275. *
  276. * System.out.println("testing map(lambda x,y: x*10+y, a, b): "
  277. * + map(new Lambda() {
  278. * public Object process(Object... args) {
  279. * int x = (Integer) args[0];
  280. * int y = (Integer) args[1];
  281. * return x+y;
  282. * }
  283. * }, a, b));
  284. * .....................................................
  285. * a = 1,2,3,4,5,6,3,3,10
  286. * b = (2,5,8)
  287. * testing map(lambda x: x*10, b): (20,50,80)
  288. * testing map(lambda x,y: x*10+y, a, b): (12,25,38)
  289. * -----------------------------------------------------
  290. * <pre>
  291. */
  292. public static Iterable<Object> map(final Lambda lambda,
  293. final Iterable<?>... iterables) {
  294. final Iterator<?>[] iterators = new Iterator[iterables.length];
  295. for (int i = 0; i < iterables.length; i++)
  296. iterators[i] = iterables[i] != null ? iterables[i].iterator()
  297. : Null;
  298. return buildObjectIterable(new IteratorMaker<Object>() {
  299. public Iterator<Object> iterator() {
  300. return new Iterator<Object>() {
  301. public boolean hasNext() {
  302. for (Iterator<?> iterator : iterators) {
  303. if (iterator.hasNext() == false)
  304. return false;
  305. }
  306. return true;
  307. }
  308. public Object next() {
  309. return lambda.process(map(iterators, new Lambda() {
  310. public Object process(Object... args) {
  311. return ((Iterator<?>) args[0]).next();
  312. }
  313. }));
  314. }
  315. public void remove() {
  316. }
  317. };
  318. }
  319. });
  320. }
  321. /**
  322. * This function returns a Iterable of Iterable, where the i-th Iterable
  323. * contains the i-th element from each of the argument iterables.
  324. * The returned Iterable is truncated in length to the length of the shortest
  325. * argument Iterable.
  326. *
  327. * @param iterables, an number of <code>Iterable</code>s
  328. * @return an <code>Iterable</code>.
  329. *
  330. * @see <code>map</code>.
  331. */
  332. public static Iterable<Object> zip(final Iterable<?>... iterables) {
  333. final Iterator<?>[] iterators = new Iterator[iterables.length];
  334. for (int i = 0; i < iterables.length; i++)
  335. iterators[i] = iterables[i] != null ? iterables[i].iterator()
  336. : Null;
  337. return buildObjectIterable(new IteratorMaker<Object>() {
  338. public Iterator<Object> iterator() {
  339. return new Iterator<Object>() {
  340. public boolean hasNext() {
  341. for (Iterator<?> iterator : iterators) {
  342. if (iterator.hasNext() == false)
  343. return false;
  344. }
  345. return true;
  346. }
  347. public Object next() {
  348. return iterable(map(iterators, new Lambda() {
  349. public Object process(Object... args) {
  350. return ((Iterator<?>) args[0]).next();
  351. }
  352. }));
  353. }
  354. public void remove() {
  355. }
  356. };
  357. }
  358. });
  359. }
  360. /**
  361. * This function returns a Iterable from those elements of the given iterable for which
  362. * predicate returns not null.
  363. *
  364. * @param predicate, a Lambda
  365. * @param iterable
  366. * @return an <code>Iterable</code>.
  367. *
  368. * @see #map(com.syntazo.coffeegrains.Iteration.Lambda, Iterable...)
  369. */
  370. public static Iterable<Object> filter(final Lambda predicate,
  371. final Iterable<?> iterable) {
  372. return buildObjectIterable(new IteratorMaker<Object>() {
  373. public Iterator<Object> iterator() {
  374. return new Iterator<Object>() {
  375. Iterator<?> iterator = iterable != null ? iterable
  376. .iterator() : Null;
  377. Object nextObj = null;
  378. public boolean hasNext() {
  379. nextObj = getNext(nextObj);
  380. return nextObj != null;
  381. }
  382. public Object getNext(Object obj) {
  383. while (obj == null && iterator.hasNext()) {
  384. obj = predicate.process(iterator.next());
  385. }
  386. return obj;
  387. }
  388. public void remove() {
  389. iterator.remove();
  390. }
  391. public Object next() {
  392. Object obj = getNext(nextObj);
  393. nextObj = null;
  394. return obj;
  395. }
  396. };
  397. }
  398. });
  399. }
  400. /**
  401. * This function makes a Iterable whose iterator whose iterator
  402. * returns elements from the given iterable as
  403. * long as the predicate is true.
  404. *
  405. * @param predicate, a Lambda
  406. * @param iterable
  407. * @return an <code>Iterable</code>.
  408. *
  409. * @see #filter(com.syntazo.coffeegrains.Iteration.Lambda, Iterable)
  410. * @see #dropwhile(com.syntazo.coffeegrains.Iteration.Lambda, Iterable)
  411. */
  412. public static Iterable<Object> takewhile(final Lambda lambda,
  413. final Iterable<?> iterable) {
  414. return buildObjectIterable(new IteratorMaker<Object>() {
  415. public Iterator<Object> iterator() {
  416. return new Iterator<Object>() {
  417. Iterator<?> iterator = iterable != null ? iterable
  418. .iterator() : Null;
  419. Object nextObj = null;
  420. public boolean hasNext() {
  421. nextObj = getNext(nextObj);
  422. return nextObj != null;
  423. }
  424. public Object getNext(Object obj) {
  425. if (obj == null && iterator.hasNext()) {
  426. obj = lambda.process(iterator.next());
  427. }
  428. return obj;
  429. }
  430. public void remove() {
  431. iterator.remove();
  432. }
  433. public Object next() {
  434. Object obj = getNext(nextObj);
  435. nextObj = null;
  436. return obj;
  437. }
  438. };
  439. }
  440. });
  441. }
  442. /**
  443. * This function makes a Iterable whose iterator drops elements from the
  444. * given iterable as long as the predicate is true.
  445. * Afterwards it returns every element.
  446. *
  447. * Note, the iterator does not produce any output until the predicate
  448. * is true, so it may have a lengthy start-up time
  449. *
  450. * @param predicate, a Lambda
  451. * @param iterable
  452. * @return an <code>Iterable</code>.
  453. *
  454. * @see <code>filter, takewhile</code>.
  455. */
  456. public static Iterable<Object> dropwhile(final Lambda lambda,
  457. final Iterable<?> iterable) {
  458. return buildObjectIterable(new IteratorMaker<Object>() {
  459. public Iterator<Object> iterator() {
  460. return new Iterator<Object>() {
  461. Iterator<?> iterator = iterable != null ? iterable
  462. .iterator() : Null;
  463. boolean dropZone = true;
  464. public boolean hasNext() {
  465. return iterator.hasNext();
  466. }
  467. public void remove() {
  468. iterator.remove();
  469. }
  470. public Object next() {
  471. Object obj = iterator.next();
  472. while (dropZone && iterator.hasNext()) {
  473. dropZone = (Boolean) lambda.process(obj);
  474. if (dropZone)
  475. obj = iterator.next();
  476. }
  477. dropZone = false;
  478. return obj;
  479. }
  480. };
  481. }
  482. });
  483. }
  484. /**
  485. * Makes an Iterable whoe iterator applies the lambda function of two arguments
  486. * cumulatively to the items of the goven iterable,
  487. * from left to right, so as to reduce the iterable to a single value.
  488. *
  489. * The example the code below calculates ((((1+2)+3)+4)+5).
  490. * <pre>
  491. * -----------------------------------------------------
  492. *
  493. * System.out.println("testing reduce(lambda x,y: x+y, a,0): "
  494. * + reduce(new Lambda() {
  495. * public Object process(Object... args) {
  496. * int x = (Integer) args[0];
  497. * int y = (Integer) args[1];
  498. * return x+y;
  499. * }
  500. * }, iterable(1,2,3,4,5), 0));
  501. *
  502. * .....................................................
  503. * testing reduce(lambda x,y: x+y, a,0): 15
  504. * -----------------------------------------------------
  505. * </pre>
  506. *
  507. * @param predicate, a Lambda
  508. * @param iterable
  509. * @param start, The initial start value.
  510. * @return an <code>Iterable</code>.
  511. *
  512. * @see <code>filter, takewhile</code>.
  513. */
  514. public static Object reduce(Lambda lambda, final Iterable<Object> iterable,
  515. Object start) {
  516. Object result = start;
  517. for (Object obj : iterable) {
  518. result = lambda.process(result, obj);
  519. }
  520. return result;
  521. }
  522. /**
  523. * Make an iterator that returns elements from the first iterable until it
  524. * is exhausted, then proceeds to the next iterable, until all of the
  525. * iterables are exhausted. Used for treating consecutive sequences as a
  526. * single sequence.
  527. *
  528. * @param iterables, a varialble number of iterables to chain through.
  529. * @return an <code>Iterable</code>.
  530. */
  531. public static Iterable<Object> chain(final Iterable<?>... iterables) {
  532. return buildObjectIterable(new IteratorMaker<Object>() {
  533. public Iterator<Object> iterator() {
  534. return new Iterator<Object>() {
  535. int iterableIndex = 0;
  536. Iterator<?> iterator = iterables[0] != null ? iterables[0]
  537. .iterator() : Null;
  538. public boolean hasNext() {
  539. boolean more = iterator.hasNext();
  540. while (more == false
  541. && iterableIndex < iterables.length - 1) {
  542. iterator = iterables[iterableIndex++] == null ? Null
  543. : iterables[iterableIndex].iterator();
  544. more = iterator.hasNext();
  545. }
  546. return more;
  547. }
  548. public Object next() {
  549. hasNext();
  550. return iterator.next();
  551. }
  552. public void remove() {
  553. iterator.remove();
  554. }
  555. };
  556. }
  557. });
  558. }
  559. public static Iterable<Object> iterate(final int count,
  560. final Iterable<?> iterable) {
  561. return buildObjectIterable(new IteratorMaker<Object>() {
  562. public Iterator<Object> iterator() {
  563. return new Iterator<Object>() {
  564. int number = 0;
  565. Iterator<?> iterator = iterable != null ? iterable
  566. .iterator() : Null;
  567. public boolean hasNext() {
  568. return number < count && iterator.hasNext();
  569. }
  570. public Object next() {
  571. number++;
  572. return iterator.next();
  573. }
  574. public void remove() {
  575. iterator.remove();
  576. }
  577. };
  578. }
  579. });
  580. }
  581. /**
  582. * Make an Iterable whose iterator returns consecutive integers starting with start.
  583. *
  584. * @param start.
  585. * @return an <code>Iterable</code>.
  586. */
  587. public static Iterable<Integer> count(final int start) {
  588. return buildIntegerIterable(new IteratorMaker<Integer>() {
  589. public Iterator<Integer> iterator() {
  590. return new Iterator<Integer>() {
  591. int iterableIndex = start;
  592. public boolean hasNext() {
  593. return true;
  594. }
  595. public Integer next() {
  596. return iterableIndex++;
  597. }
  598. public void remove() {
  599. }
  600. };
  601. }
  602. });
  603. }
  604. /**
  605. * Make an Iterable whose iterator endlessly returning elements from the given iterable.
  606. *
  607. * @param iterable.
  608. * @return an <code>Iterable</code>.
  609. */
  610. public static Iterable<Object> cycle(final Iterable<?> iterable) {
  611. return buildObjectIterable(new IteratorMaker<Object>() {
  612. public Iterator<Object> iterator() {
  613. return new Iterator<Object>() {
  614. Iterator<?> iterator = iterable != null ? iterable
  615. .iterator() : Null;
  616. public boolean hasNext() {
  617. return true;
  618. }
  619. public Object next() {
  620. if (iterator.hasNext() == false)
  621. iterator = iterable.iterator();
  622. return iterator.next();
  623. }
  624. public void remove() {
  625. iterator.remove();
  626. }
  627. };
  628. }
  629. });
  630. }
  631. /**
  632. * Make an Iterable whose iterator endlessly returning elements from the given iterable.
  633. *
  634. * @param iterable.
  635. * @return an <code>Iterable</code>.
  636. */
  637. public static Iterable<Object> repeat(final Object object) {
  638. return buildObjectIterable(new IteratorMaker<Object>() {
  639. public Iterator<Object> iterator() {
  640. return new Iterator<Object>() {
  641. public boolean hasNext() {
  642. return true;
  643. }
  644. public Object next() {
  645. return object;
  646. }
  647. public void remove() {
  648. }
  649. };
  650. }
  651. });
  652. }
  653. /**
  654. * Make an Iterable whose iterator returns an iterable of the given objects repeating, count, number of times.
  655. *
  656. * @param count, the number of times to repeat.
  657. * @param objects, a variable number of Objects.
  658. * @return an <code>Iterable</code>.
  659. */
  660. public static Iterable<Object> repeat(int count, Object... objects) {
  661. return repeat(count, iterable(objects));
  662. }
  663. public static Iterable<Object> repeat(final int count,
  664. final Iterable<?> iterable) {
  665. return buildObjectIterable(new IteratorMaker<Object>() {
  666. public Iterator<Object> iterator() {
  667. return new Iterator<Object>() {
  668. Iterator<?> iterator = iterable != null ? iterable
  669. .iterator() : Null;
  670. int cycleNo = 0;
  671. public boolean hasNext() {
  672. if (iterator.hasNext() == false) {
  673. iterator = iterable.iterator();
  674. cycleNo++;
  675. }
  676. return cycleNo < count;
  677. }
  678. public Object next() {
  679. return iterator.next();
  680. }
  681. public void remove() {
  682. iterator.remove();
  683. }
  684. };
  685. }
  686. });
  687. }
  688. /**
  689. * A test method that return true if the predicate evaluates true for all elements
  690. * returned by the iterable, otherwise false.
  691. *
  692. * @param predicate.
  693. * @param iterable.
  694. * @return boolean, true if the predicate evaluates true for all elements
  695. * returned by the iterable, otherwise false.
  696. */
  697. public static boolean all(Lambda predicate, Iterable<?> iterable) {
  698. for (Object obj : iterable) {
  699. if (predicate.process(obj) == null)
  700. return false;
  701. }
  702. return true;
  703. }
  704. /**
  705. * A test method that return true if the predicate evaluates true for any element
  706. * returned by the iterable, otherwise false.
  707. *
  708. * @param predicate.
  709. * @param iterable.
  710. * @return boolean, true if the predicate evaluates true for any element
  711. * returned by the iterable, otherwise false.
  712. */
  713. public static boolean any(Lambda lambda, Iterable<?> iterable) {
  714. for (Object obj : iterable) {
  715. if (lambda.process(obj) == null)
  716. return true;
  717. }
  718. return false;
  719. }
  720. private static Object[] map(Object[] array, Lambda lambda) {
  721. return map(array, lambda, 0, array.length);
  722. }
  723. private static Object[] map(Object[] array, Lambda lambda, int start,
  724. int stop) {
  725. Object[] result = new Object[stop - start];
  726. for (int i = start; i < stop; i++)
  727. result[i] = lambda.process(array[i]);
  728. return result;
  729. }
  730. public static boolean any(Object[] array, Lambda lambda) throws Exception {
  731. for (Object obj : array) {
  732. if ((Boolean) lambda.process(obj) == false)
  733. return true;
  734. }
  735. return false;
  736. }
  737. public static String str(Iterable<?> parts) {
  738. return join(parts, ",");
  739. }
  740. public static String str(Object[] parts) {
  741. return join(parts, ",");
  742. }
  743. public static String join(Iterable<?> parts, String del) {
  744. StringBuffer result = new StringBuffer();
  745. int i = 0;
  746. for (Object part : parts) {
  747. result.append(part);
  748. result.append(del);
  749. i++;
  750. }
  751. return i > 0 ? result.substring(0, result.length() - del.length())
  752. : result.toString();
  753. }
  754. public static String join(Object[] parts, String del) {
  755. StringBuffer result = new StringBuffer();
  756. int i = 0;
  757. for (Object part : parts) {
  758. result.append(part);
  759. result.append(del);
  760. i++;
  761. }
  762. return i > 0 ? result.substring(0, result.length() - del.length())
  763. : result.toString();
  764. }
  765. public static Collection<Object> toCollection(Collection<Object> container,
  766. Object... elements) {
  767. for (Object obj : elements)
  768. container.add(obj);
  769. return container;
  770. }
  771. public static Collection<Object> toCollection(Collection<Object> container,
  772. Iterable<?> iterable) {
  773. for (Object obj : iterable)
  774. container.add(obj);
  775. return container;
  776. }
  777. public static void main(String[] args) {
  778. (new PyFile("c:\\Extract-7.txx", "w", 4000)).write((new PyFile("c:\\Extract-7.txt", "r", 4000)).read());
  779. Collection<Object> a = toCollection(new ArrayList<Object>(), 1, 2, 3,
  780. 4, 5, 6, 3, 3, 10);
  781. System.out
  782. .println("Collection<Object> a = toCollection(new ArrayList<Object>(), 1,2,3,4,5,6,3,3,10): "
  783. + str(a));
  784. Iterable<Integer> b = range(2, 10, 3);
  785. System.out.println("Iterable<Integer> b = irange(2,10,3): " + b);
  786. Iterable<Object> c = iterable(1, 2, 3, 4);
  787. System.out.println("Iterable c = iterable(1,2,3): " + c);
  788. Iterable<Object> d = iterable(new int[] { 1, 2, 3, 4 });
  789. System.out.println("Iterable d = iterable(new Integer []{1,2,3,4}): "
  790. + c);
  791. System.out.println("testing ienum(b): " + enumerate(c));
  792. Iterator forever = repeat(4).iterator();
  793. System.out.println("Iterable forever = repeat(4).iterator()");
  794. System.out.println("testing forever.next(), forever.next(): "
  795. + forever.next() + "," + forever.next());
  796. System.out.println("testing repeat(3, iterable(1,2,3)): "
  797. + repeat(3, iterable(1, 2, 3)));
  798. System.out.println("testing repeat(3, iterable(iterable(1,2,3))): "
  799. + repeat(3, iterable(iterable(1, 2, 3))));
  800. System.out.println("testing map(lambda x: x*10, b): "
  801. + map(new Lambda() {
  802. public Object process(Object... args) {
  803. return ((Integer) args[0]) * 10;
  804. }
  805. }, b));
  806. System.out.println("testing map(lambda x,y: x*10+y, a, b): "
  807. + map(new Lambda() {
  808. public Object process(Object... args) {
  809. return ((Integer) args[0]) * 10 + ((Integer) args[1]);
  810. }
  811. }, a, b));
  812. System.out.println("testing filter(lambda x: x%2==0, a): "
  813. + filter(new Lambda() {
  814. public Object process(Object... args) {
  815. return ((Integer) args[0]) % 2 == 0 ? args[0] : null;
  816. }
  817. }, a));
  818. System.out.println("testing reduce(lambda x,y: x+y, a,0): "
  819. + reduce(new Lambda() {
  820. public Object process(Object... args) {
  821. int x = (Integer) args[0];
  822. int y = (Integer) args[1];
  823. return x+y;
  824. }
  825. }, iterable(1,2,3,4,5), 0));
  826. System.out.println("testing slice(" + str(a) + "\n\t, 1,10,3): "
  827. + slice(a, 1, 10, 3));
  828. System.out.println("testing chain(a,b): " + chain(a, b));
  829. System.out.println("testing chain(a,b,a): " + chain(a, b, a));
  830. System.out.println("testing iterate(10, count(20)): "
  831. + iterate(10, count(20)));
  832. System.out.println("testing iterate(10, cycle(b)): "
  833. + iterate(10, cycle(b)));
  834. System.out.println("testing all(lambda x: x==2, " + b + ": "
  835. + all(new Lambda() {
  836. public Object process(Object... args) {
  837. return (Integer) args[0] == 2;
  838. }
  839. }, b));
  840. System.out.println("testing any(lambda x: x==2, " + b + ": "
  841. + any(new Lambda() {
  842. public Object process(Object... args) {
  843. return (Integer) args[0] == 2;
  844. }
  845. }, b));
  846. System.out
  847. .println("testing any(lambda x: x==2, repeat(10, iterable(1)): "
  848. + any(new Lambda() {
  849. public Object process(Object... args) {
  850. return (Integer) args[0] == 2;
  851. }
  852. }, repeat(10, iterable(1))));
  853. System.out
  854. .println("testing all(lambda x: x==1, repeat(10, iterable(1)): "
  855. + all(new Lambda() {
  856. public Object process(Object... args) {
  857. return (Integer) args[0] == 2;
  858. }
  859. }, repeat(10, iterable(1))));
  860. System.out.println("testing zip(a,b,c): " + zip(a, b, c));
  861. System.out.println("testing takewhile(lambda x: x< 4, a): "
  862. + takewhile(new Lambda() {
  863. public Object process(Object... args) {
  864. return (Integer) args[0] < 4 ? args[0] : null;
  865. }
  866. }, a));
  867. System.out.println("testing dropwhile(lambda x: x< 4, a): "
  868. + dropwhile(new Lambda() {
  869. public Object process(Object... args) {
  870. return (Integer) args[0] < 4;
  871. }
  872. }, a));
  873. System.out.println("example enumerate = zip(count().iterator(), a): "
  874. + zip(count(0), a));
  875. System.out.println("example dot product of (1,2,3,4) * (4,5,6,7): "
  876. + reduce(new Lambda() {
  877. public Object process(Object... args) {
  878. return (Integer) args[0] + (Integer) args[1];
  879. }
  880. }, map(new Lambda() {
  881. public Object process(Object... args) {
  882. return (Integer) args[0] * (Integer) args[1];
  883. }
  884. }, iterable(1, 2, 3, 4), iterable(4, 5, 6, 7)), 0));
  885. }
  886. }