fork download
  1. @groovy.transform.TypeChecked
  2.  
  3. class Unfoldr<A,B> implements java.util.Iterator<A>
  4. {
  5. public Unfoldr(Closure<Object[]> f, B init)
  6. {
  7. this.f = f;
  8. this.state = f(init);
  9. }
  10.  
  11. public synchronized A next() throws java.util.NoSuchElementException
  12. {
  13. if (hasNext())
  14. {
  15. A curr = state[0];
  16. state = f(state[1]);
  17. return curr;
  18. }
  19. else
  20. {
  21. throw new java.util.NoSuchElementException();
  22. }
  23. }
  24.  
  25. public synchronized boolean hasNext()
  26. {
  27. return state != null;
  28. }
  29.  
  30. public void remove() { throw UnsupportedOperationException; }
  31.  
  32. private Closure<Object[]> f;
  33.  
  34. private Object[] state;
  35. }
  36.  
  37. def currify(fn) {
  38. { Object... args ->
  39. if (args.size() == fn.maximumNumberOfParameters) {
  40. fn(*args)
  41. } else {
  42. currify(fn.curry(*args))
  43. }
  44. }
  45. };
  46.  
  47. def unfoldr = currify { f, init -> new Unfoldr(f, init) };
  48.  
  49. def map = currify { f, l -> unfoldr({ l2 -> if (l2.hasNext()) { def e = l2.next(); return [f(e), l2]} else { return null; } } , l.iterator())}
  50.  
  51. def splitlist = currify {
  52. n, l ->
  53. unfoldr(
  54. {
  55. l2 ->
  56. try
  57. {
  58. def a = new Object[n];
  59. for (i in 0..(n-1))
  60. {
  61. a[i] = l2.next();
  62. }
  63. return [a, l2];
  64. }
  65. catch (java.util.NoSuchElementException e)
  66. {
  67. return null;
  68. }
  69. },
  70. l
  71. )
  72. };
  73.  
  74. Iterator.metaClass.rightShift = { PrintStream os -> delegate.each({ x -> os.println(x) }) }
  75. Iterator.metaClass.rightShift = { Closure f -> f(delegate) }
  76.  
  77. id = { x -> x }
  78. f = currify { x -> x }
  79.  
  80. println "A: "
  81. [[1,2],[3,4]].iterator() >> System.out
  82. println "B: "
  83. [1,2,3,4].iterator() >> splitlist(2) >> System.out
  84. println "C: "
  85. [[1,2],[3,4]].iterator() >> map(id) >> System.out
  86. println "D: "
  87. [1,2,3,4].iterator() >> splitlist(2) >> map(id) >> System.out
  88. println "E: "
  89. [[1,2],[3,4]].iterator() >> map(f) >> System.out
  90. println "F: "
  91. [1,2,3,4].iterator() >> splitlist(2) >> map(f) >> System.out
  92.  
Runtime error #stdin #stdout #stderr 1.7s 388224KB
stdin
Standard input is empty
stdout
A: 
[1, 2]
[3, 4]
B: 
[1, 2]
[3, 4]
C: 
[1, 2]
[3, 4]
D: 
[1, 2]
[3, 4]
E: 
[1, 2]
[3, 4]
F: 
stderr
Caught: java.lang.IllegalArgumentException: Can't curry 2 arguments for a closure with 1 parameters.
java.lang.IllegalArgumentException: Can't curry 2 arguments for a closure with 1 parameters.
	at prog$_currify_closure8.doCall(prog.groovy:42)
	at prog$_run_closure2_closure9.doCall(prog.groovy:49)
	at Unfoldr.<init>(prog.groovy:8)
	at prog$_run_closure1.doCall(prog.groovy:47)
	at prog$_currify_closure8.doCall(prog.groovy:40)
	at prog$_run_closure2.doCall(prog.groovy:49)
	at prog$_currify_closure8.doCall(prog.groovy:40)
	at prog$_run_closure5.doCall(prog.groovy:75)
	at prog.run(prog.groovy:91)