unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Some reader syntax for data structures
@ 2019-03-25 20:55 Ivan Kupalov
  2019-03-30 22:24 ` Alex Vong
  0 siblings, 1 reply; 3+ messages in thread
From: Ivan Kupalov @ 2019-03-25 20:55 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 759 bytes --]

Hello everyone!

I wanted to learn some Scheme  before starting one project and it seemed to me that it might be a nice exercise to try and extend syntax for hash tables, a-la Clojure/Rackjure (and only later I've learned about Racket's `#hash()`).
I think it's really not optimized and not even really elegant but it works! (readers don't have much documentation attached to them unlike macros so it was kinda difficult). I later extended it to vhashes and vlists because I already had some understanding on what is required from me.

(also here's a link in case something doesn't work, I didn't use mailing lists much before: https://gitlab.com/snippets/1838863) <https://gitlab.com/snippets/1838863>

So I just wanted to share. Feel free to send feedback!

[-- Attachment #2: collection-readers.scm --]
[-- Type: text/x-scheme, Size: 2032 bytes --]

(use-modules (ice-9 match))
(use-modules (ice-9 vlist))
(use-modules (srfi srfi-1))

(eval-when (expand load eval)

  (define (read-hash-table-2 datum)
    (let ((insertions (map (match-lambda
                             ((key value)
                              `(hash-set! table ,key ,value)))
                           datum)))
      (append
       `(let ((table (make-hash-table ,(length insertions)))))
       insertions
       '(table))))


  (define (read-vhash-table datum)
    (let ((datum (reverse datum)))
      (fold (lambda (el acc)
              (match el
                ((key value)
                 `(vhash-consq ,key ,value ,acc))
                ))
            'vlist-null
            datum)))

  (define (read-vlist datum)
    (list 'list->vlist (cons 'list datum)))

  (read-hash-extend #\h (lambda (chr port)
                          (read-hash-table-2 (read port))))

  (read-hash-extend #\v (lambda (chr port)
                          (let ((type (read port))
                                (definition (read port)))
                            (cond
                             ((eq? 'h type) (read-vhash-table definition))
                             ((eq? 'l type) (read-vlist definition))
                             (else (error "woah I don't know this syntax")))))))

(define characters #h(('you "hooman") ('me "schemer")))
(display "I am a ")
(display (hash-ref characters 'me)) ;; => schemer
(newline)

(define demo-table
  #h(
     ('name "Demo name")
     ('version "3.45.5")
     ('dependencies #h(
                       ('hash-map-syntax "0.0.1")))
     ((string-append "computed-" "key") #t)))

(display (hash-ref demo-table "computed-key")) ;; => #t
(newline)

(define demo-vhash #vh(
                       ('vh "vhash")
                       ('vl "vlist")))

(define demo-vlist #vl(
                       "Nice"
                       "Syntax"
                       #vl(
                           "For"
                           (string-append "Efficient" " " "structures"))))

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Some reader syntax for data structures
  2019-03-25 20:55 Some reader syntax for data structures Ivan Kupalov
@ 2019-03-30 22:24 ` Alex Vong
  2019-04-03 20:39   ` Ludovic Courtès
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Vong @ 2019-03-30 22:24 UTC (permalink / raw)
  To: Ivan Kupalov; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 976 bytes --]

Thanks for creating this!

I enjoy the clojure hash-map syntax and I think guile vhash can sort of
fit this place with a good reader syntax.

Ivan Kupalov <charlag@tutanota.com> writes:

> Hello everyone!
>
> I wanted to learn some Scheme before starting one project and it
> seemed to me that it might be a nice exercise to try and extend syntax
> for hash tables, a-la Clojure/Rackjure (and only later I've learned
> about Racket's `#hash()`).
> I think it's really not optimized and not even really elegant but it
> works! (readers don't have much documentation attached to them unlike
> macros so it was kinda difficult). I later extended it to vhashes and
> vlists because I already had some understanding on what is required
> from me.
>
> (also here's a link in case something doesn't work, I didn't use
> mailing lists much before: https://gitlab.com/snippets/1838863)
> <https://gitlab.com/snippets/1838863>
>
> So I just wanted to share. Feel free to send feedback!

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 227 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Some reader syntax for data structures
  2019-03-30 22:24 ` Alex Vong
@ 2019-04-03 20:39   ` Ludovic Courtès
  0 siblings, 0 replies; 3+ messages in thread
From: Ludovic Courtès @ 2019-04-03 20:39 UTC (permalink / raw)
  To: guile-user

Hello,

Alex Vong <alexvong1995@gmail.com> skribis:

> I enjoy the clojure hash-map syntax and I think guile vhash can sort of
> fit this place with a good reader syntax.

I use something like this:

  (define-syntax vhash
    (syntax-rules (=>)
      "Build a vhash with the given key/value mappings."
      ((_)
       vlist-null)
      ((_ (key others ... => value) rest ...)
       (vhash-cons key value
                   (vhash (others ... => value) rest ...)))
      ((_ (=> value) rest ...)
       (vhash rest ...))))

Of course that doesn’t fill the exact same role as a reader syntax since
it doesn’t allow you to transfer a vhash over the wire, but for cases
where you only want to define a literal vhash, I find it “good enough.”

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-04-03 20:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-25 20:55 Some reader syntax for data structures Ivan Kupalov
2019-03-30 22:24 ` Alex Vong
2019-04-03 20:39   ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).