diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-02-13 00:14:52 +0100 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-02-13 00:14:52 +0100 |
commit | a58f2f7d7021ef56419c40d3169e536ca978cca3 (patch) | |
tree | ac39b904458a9dbd6902358181671108fc7432d4 | |
parent | de23bd8fa5db84424faed6326c9a57c35c20a955 (diff) |
par: piece-table: some utilities for functional approach
-rw-r--r-- | par/piece-table.scm | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/par/piece-table.scm b/par/piece-table.scm index 54cb9a9..f5f58da 100644 --- a/par/piece-table.scm +++ b/par/piece-table.scm @@ -213,6 +213,10 @@ (values piece rem)) (else (loop (piece-next piece) (- rem (piece-length piece))))))) + +(define (piece-table-last-piece piece-table) + (piece-prev (piece-prev (piece-table-piece piece-table)))) + @@ -231,10 +235,25 @@ (+ acc (piece-length piece)) (loop (piece-next piece) (+ (piece-length piece) acc))))) +(define (%piece-fold-from piece f identity) + (let loop ((piece piece) + (acc identity)) + (unless (sentinel-piece? piece) + (loop (piece-next piece) (f piece acc))))) + +(define (%piece-fold-from-right piece f identity) + (let loop ((piece piece) + (acc identity)) + (unless (sentinel-piece? piece) + (loop (piece-prev piece) (f piece acc))))) + + (define (piece-table-for-each piece-table f from to) ;; TODO: default from and to ;; TODO: maybe combine with the `piece-table-index->piece+index` to find the ;; `from` + ;; TODO: make this lower level, searching from a piece, not from the table + ;; TODO: implement using `%piece-fold-from` "Calls `f` through the characters of the piece-table, like `string-for-each` would do, but the `f` call also includes the piece (to expose extra data) and the index of the character" @@ -265,6 +284,7 @@ "-right version of piece-table-for-each, it finds the tail and goes backwards" ;; TODO: not implemented yet + ;; TODO: make this lower level, searching from a piece, not from the table ) @@ -330,6 +350,7 @@ (define (piece-table-find piece-table char from) ;; TODO: generalize the from + ;; TODO: Implement the backwards one too (call/cc (lambda (cont) (piece-table-for-each piece-table |