unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: guile-devel@gnu.org
Subject: Re: Functional record "setters", a different approach
Date: Thu, 12 Apr 2012 00:27:46 +0200	[thread overview]
Message-ID: <87d37dhpjx.fsf@gnu.org> (raw)
In-Reply-To: 87398aadcp.fsf@netris.org

Mark H Weaver <mhw@netris.org> skribis:

> +        ((_ check? struct-expr ((getter . rest) expr) ...)
> +         ;;
> +         ;; FIXME: Improve compile-time error reporting:
> +         ;;   1. report an error if any getter-path is a
> +         ;;      prefix of any other getter-path.
> +         ;;   2. report an error if the initial getters
> +         ;;      do not all belong to the same record type.
> +         ;;
> +         ;; forest : (tree ...)
> +         ;;   tree : (getter (rest . expr) ...)
> +         (let ((forest
> +                (fold (lambda (g r e forest)
> +                        (cond ((find (lambda (tree)
> +                                       (free-identifier=? g (car tree)))
> +                                     forest)
> +                               => (lambda (tree)
> +                                    (cons (cons g (cons (cons r e)
> +                                                        (cdr tree)))
> +                                          (delq tree forest))))
> +                              (else (cons (list g (cons r e))
> +                                          forest))))
> +                      '()
> +                      #'(getter ...)
> +                      #'(rest ...)
> +                      #'(expr ...))))

BTW this will need some more comments ;-), and perhaps splitting in
several functions for clarity.  Using SRFI-1 alists and ‘match’ may help
as well.  WDYT?

(I often find myself avoiding occurrences of ‘car’, ‘cdr’ & co. in my
code these days.)

FWIW I was using this approach to represent the tree of accessors:

    (define (field-tree fields)
      ;; Given FIELDS, a list of field-accessor-lists, return a tree
      ;; that groups together FIELDS by prefix.  Example:
      ;;   FIELDS:  ((f1 f2 f3) (f1 f4))
      ;;   RESULT:  ((f1 (f2 (f3)) (f4)))
      (define (insert obj tree)
        (match obj
          ((head tail ...)
           (let ((sub (or (assoc-ref tree head) '())))
             (cons (cons head (insert tail sub))
                   (alist-delete head tree))))
          (()
           tree)))

      (fold-right insert '() fields))

Thanks,
Ludo’.




  reply	other threads:[~2012-04-11 22:27 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-11  6:59 Functional record "setters", a different approach Mark H Weaver
2012-04-11  7:57 ` Mark H Weaver
2012-04-11  8:20   ` Mark H Weaver
2012-04-11 22:27     ` Ludovic Courtès [this message]
2012-04-11 22:22 ` Ludovic Courtès
2012-04-12 15:04   ` Mark H Weaver
2012-04-12 16:45     ` Thien-Thi Nguyen
2012-04-12 19:58     ` Ludovic Courtès
2012-04-13  1:58       ` Mark H Weaver
2012-04-13 15:41         ` Ludovic Courtès
2012-04-13 17:26           ` Mark H Weaver
2012-05-07 16:34         ` Ludovic Courtès
2012-05-14 22:25           ` Mark H Weaver
2012-05-15 21:23             ` Ludovic Courtès
2012-11-07 20:04 ` Mark H Weaver
2012-11-08  5:15   ` Mark H Weaver
2012-11-08 19:09     ` Ludovic Courtès
2012-11-09  3:54       ` Mark H Weaver
2012-11-10 16:28         ` Ludovic Courtès
2012-11-10 19:03           ` Mark H Weaver
2012-11-10 21:40             ` Ludovic Courtès
2012-11-10  4:13       ` Mark H Weaver

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d37dhpjx.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).