(fn unfold [condition func iterate-update seed]
"1. apply `func` to `seed`.
2. insert return value of `func` into list.
3. update `seed` with `iterate-update`
4. ..."
(if (condition seed)
[]
(pull (func seed) (unfold condition func iterate-update (iterate-update seed)))))
(fn car [x ...]
x)
(fn cdr [x ...]
[...])
(fn cons [x ...]
[x ...])
(fn first [lst]
"use as car"
(car (unpack lst)))
(fn rest [lst]
"use as cdr"
(cdr (unpack lst)))
(fn pull [x xs]
"use as cons"
(cons x (unpack xs)))
(fn unfold-iter [seed object finish]
" unfold the iterator
example:
(local p (io.popen ))
(unfold-iter (p.lines p) p (lambda [x] (io.close x))))"
(let [v (seed)]
(if (= nil v)
(do
(finish object)
[])
(pull v (unfold-iter seed object finish)))))
(fn dirlookup [dir depth]
"file only, depth 1
find . -maxdepth 1 -type f"
(local p (io.popen (.. "find \"" dir "\" -maxdepth " depth " -type f")))
(unfold-iter (p.lines p) p (lambda [x] (io.close x))))
(fn execute-cmd [cmd]
"Execute command and return output as list."
(local p (io.popen cmd))
(unfold-iter (p.lines p) p (lambda [x] (io.close x))))