about summary refs log tree commit diff
path: root/kaakaa/lens.scm
diff options
context:
space:
mode:
authorArun Isaac2026-04-03 19:49:52 +0100
committerArun Isaac2026-04-03 19:49:52 +0100
commitbe549e815698cf633354447d41bec368b121b523 (patch)
treec60d2bf6e341f85ffa0d2c734cc06793c16eb508 /kaakaa/lens.scm
downloadkaagum-be549e815698cf633354447d41bec368b121b523.tar.gz
kaagum-be549e815698cf633354447d41bec368b121b523.tar.lz
kaagum-be549e815698cf633354447d41bec368b121b523.zip
Initial commit
Diffstat (limited to 'kaakaa/lens.scm')
-rw-r--r--kaakaa/lens.scm62
1 files changed, 62 insertions, 0 deletions
diff --git a/kaakaa/lens.scm b/kaakaa/lens.scm
new file mode 100644
index 0000000..f5c9370
--- /dev/null
+++ b/kaakaa/lens.scm
@@ -0,0 +1,62 @@
+;;; kaakaa --- Tiny, security-focused AI agent in Guile
+;;; Copyright © 2026 Arun Isaac <arunisaac@systemreboot.net>
+;;;
+;;; This file is part of kaakaa.
+;;;
+;;; kaakaa is free software: you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation, either version 3 of the License, or
+;;; (at your option) any later version.
+;;;
+;;; kaakaa is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with kaakaa.  If not, see <https://www.gnu.org/licenses/>.
+
+(define-module (kaakaa lens)
+  #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-26)
+  #:use-module (srfi srfi-43)
+  #:use-module (lens)
+  #:export (vector-nth
+            in-json
+            push
+            prepend-over
+            alist-delete-over))
+
+(define (vector-nth n)
+  "Like @code{nth}, but for vectors."
+  (lens (cut vector-ref <> n)
+        (lambda (vec proc)
+          (vector-append (vector-copy vec 0 n)
+                         (vector (proc (vector-ref vec n)))
+                         (vector-copy vec (1+ n))))))
+
+(define in-json
+  (case-lambda
+    "Like @code{in}, but also allow integer components so that it is
+possible to traverse JSON trees."
+    (() (id))
+    ((key . tail)
+     (compose (apply in-json tail)
+              (if (string? key)
+                  (key-ref key)
+                  (vector-nth key))))))
+
+(define (push lens x object)
+  "Cons @var{x} onto the part of @var{object} that @var{lens} focuses
+on."
+  (over lens (cut cons x <>) object))
+
+(define (prepend-over lens lst object)
+  "Prepend @var{lst} to the part of @var{object} that @var{lens} focuses
+on."
+  (over lens (cut append lst <>) object))
+
+(define (alist-delete-over lens key object)
+  "Delete @var{key} from the association list in @var{object} that
+@var{lens} focuses on."
+  (over lens (cut alist-delete key <>) object))