Skip to content

Commit 13b7bbb

Browse files
committed
feat: add base-string and improve string
By building strings up with streams, we can yield the preferrable `(simple-array character (*))` type instead of a `vector`.
1 parent e854506 commit 13b7bbb

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
SEXP strings.
99
- Reducer: `quantities` for counting unique occurrences of streamed items.
1010
- Reducer: `partition` for splitting the stream results.
11+
- Reducer: `base-string` for reducing into a `simple-base-string`.
1112

1213
#### Changed
1314

14-
- The `vector`, `string`, and `average` reducers are now faster and use less memory.
15+
- Reducer: `vector` and `average` are now faster and use less memory.
16+
- Reducer: `string` is now more efficient and yields the specialized
17+
`(simple-array character (*))` type.
1518
- `comp` is now a macro and uses less memory for long composition chains.
1619

1720
#### Fixed

transducers/package.lisp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(defpackage transducers
22
(:use :cl)
33
(:shadow #:map #:concatenate #:log #:step #:split
4-
#:cons #:count #:first #:last #:max #:min #:find #:string #:vector #:hash-table
4+
#:string #:base-string #:vector #:hash-table
5+
#:cons #:count #:first #:last #:max #:min #:find
56
#:random)
67
;; --- Entry Points --- ;;
78
(:export #:transduce)

transducers/reducers.lisp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,27 @@ reversal."
1616
((and a? (not i?)) acc)
1717
(t '())))
1818

19-
(declaim (ftype (function (&optional (or cl:string null) character) cl:string) string))
2019
(defun string (&optional (acc nil a?) (input #\z i?))
21-
"Reducer: Collect a stream of characters into to a single string."
22-
(cond ((and a? i?) (vector-push-extend input acc) acc)
23-
((and a? (not i?)) acc)
24-
(t (make-array 16 :element-type 'character :adjustable t :fill-pointer 0))))
20+
"Reducer: Collect a stream of characters into to a `simple-string'."
21+
(cond ((and a? i?)
22+
(write-char input acc)
23+
acc)
24+
((and a? (not i?)) (get-output-stream-string acc))
25+
(t (make-string-output-stream :element-type 'character))))
26+
27+
#+nil
28+
(transduce (map #'char-upcase) #'string "hello")
29+
30+
(defun base-string (&optional (acc nil a?) (input #\z i?))
31+
"Reducer: Collect a stream of characters into to a single string of `base-char'."
32+
(cond ((and a? i?)
33+
(write-char input acc)
34+
acc)
35+
((and a? (not i?)) (get-output-stream-string acc))
36+
(t (make-string-output-stream :element-type 'base-char))))
2537

2638
#+nil
27-
(string-transduce (map #'char-upcase) #'string "hello")
39+
(transduce (map #'char-upcase) #'base-string "hello")
2840

2941
(declaim (ftype (function (&optional (or cl:vector null) t) cl:vector) vector))
3042
(defun vector (&optional (acc nil a?) (input nil i?))

0 commit comments

Comments
 (0)