Tested (and fixed) `map-lines()`. It works now.

Fortune for cl-utils's current commit: Small blessing − 小吉
master
Avril 3 years ago
parent b0c7e4bc4a
commit c90a17fd4d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -138,12 +138,12 @@ To use a custom line reader function, set `read-line' to a function that takes a
To stop on a specific line, `continue' can be set to a function that receives the line string; and if `nil' is returned from that function, the iteration stops. To stop on a specific line, `continue' can be set to a function that receives the line string; and if `nil' is returned from that function, the iteration stops.
The default behaviour (with `mapper' being `mapcar' and `applicator' being `list') works just like `mapcar'(). To emulate the behaviour of functions like `mapcan'(), set `applicator' to `nconc'(); then set `mapper' to `maplist'() for `mapcon'() behaviour." The default behaviour (with `mapper' being `mapcar' and `applicator' being `list') works just like `mapcar'(). To emulate the behaviour of functions like `mapcan'(), set `applicator' to `nconc'(); then set `mapper' to `maplist'() for `mapcon'() behaviour."
;(with-open-file (stream location :direction :input) ;(with-open-file (stream location :direction :input)
(let ((filter-single-line (switch ignore (let ((filter-single-line (cond
;; Specific `ignore' values ;; Specific `ignore' values
(:blank (lambda (line) (> (length line) 0))) ((eql ignore :blank) (lambda (line) (> (length line) 0)))
(:whitespace-only (lambda (line) (not (cl-ppcre:scan "^\s*$" line)))) ((eql ignore :whitespace-only) (lambda (line) (not (cl-ppcre:scan "^\\s*$" line))))
;; Otherwise, the ignore function ;; Otherwise, the ignore function (or pass all, if nil)
ignore))) (t (or ignore (lambda (--n) (declare (ignore --n)) t))))))
(apply applicator ; apply `applicator' to the result of each iteration in `mapper'. (apply applicator ; apply `applicator' to the result of each iteration in `mapper'.
(funcall mapper func ; call the mapping function with `func' and the list of transformed and filtered lines (funcall mapper func ; call the mapping function with `func' and the list of transformed and filtered lines
(mapcan (lambda (n) (when n (list n))) ; outputs a list of the lines (mapcan (lambda (n) (when n (list n))) ; outputs a list of the lines
@ -156,8 +156,7 @@ The default behaviour (with `mapper' being `mapcar' and `applicator' being `list
(let ((stream (gensym))) (let ((stream (gensym)))
`(with-open-file (,stream ,location :direction :input) `(with-open-file (,stream ,location :direction :input)
,(cons 'map-lines (append `(,stream ,func) kvs))))) ,(cons 'map-lines (append `(,stream ,func) kvs)))))
;TODO: test map-lines -- (map-file-lines "file" #'identity :applicator #'nconc :ignore :blank)
(defun mapcan-lines (stream func &rest kvs &key &allow-other-keys) (defun mapcan-lines (stream func &rest kvs &key &allow-other-keys)
"See `map-lines'(): Uses `nconc'() as the applicator and `mapcar'() as the mapper, which produces an output you'd expect from `mapcan'() The other key arguments can be specified according to the signature of `map-lines'()." "See `map-lines'(): Uses `nconc'() as the applicator and `mapcar'() as the mapper, which produces an output you'd expect from `mapcan'() The other key arguments can be specified according to the signature of `map-lines'()."
(apply #'map-lines (append (list stream func :applicator #'nconc :mapper #'mapcar) kvs))) (apply #'map-lines (append (list stream func :applicator #'nconc :mapper #'mapcar) kvs)))

Loading…
Cancel
Save