all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: swedebugia@riseup.net
To: "Ludovic Courtès" <ludo@gnu.org>
Cc: guix-devel@gnu.org,
	Guix-devel <guix-devel-bounces+swedebugia=riseup.net@gnu.org>
Subject: Learning the match-syntax...
Date: Sat, 05 Jan 2019 13:25:40 -0800	[thread overview]
Message-ID: <c446d9551edfc72694c566d7ad90cae0@riseup.net> (raw)
In-Reply-To: <87zhsf2ec6.fsf@gnu.org>

Saluton Ludo' :)

I write this to you for no other reason than I like the code your write
and would love to learn more.

If you don't have time/energy just let me know and I will ask Pierre or
somebody else. :)

On 2019-01-05 18:39, Ludovic Courtès wrote:
> One last thing:
> 
>> +(define (check-source-unstable-tarball package)
>> +  "Emit a warning if PACKAGE's source is an autogenerated tarball."
>> +  (define (check-source-uri uri)
>> +    (when (and (string=? (uri-host (string->uri uri)) "github.com")
>> +               (string=? (third (split-and-decode-uri-path
>> +                                   (uri-path (string->uri uri))))
>> +                     "archive"))
> 
> ‘third’ could fail badly if the list has fewer elements, so I’d suggest
> writing it something like:
> 
>   (when (and …
>              (match (split-and-decode-uri-path …)
>                ((_ _ "archive" _ ...) #t)
>                (_ #f)))
>      …)

This is an elegant rewrite to return a boolean #f in case the URL is bad
in some way.

I'm trying very hard to learn the guile match-syntax.

To make sure I understand the above I would like to explain it and you
can verify that I got it right. Ok?

>              (match (split-and-decode-uri-path …) <- this is the input
>                ((_ _ "archive" _ ...) #t)
                   ^ ^ ^ =third  ^       ^return true if the clause
match
                                 ^fourth & more
                   ^ ^ = anything twice 
>                (_ #f)))
                  ^ =the general case =else return #f.

Correct?

This is a more complicated (nested) match example from Juliens opam
importer which I find is an elegant functional way to find what we need:

(define (metadata-ref file lookup)
  (fold (lambda (record acc)
          (match record
            ((record key val)
             (if (equal? key lookup)
	       (match val
                 (('list-pat . stuff) stuff)
                 (('string-pat stuff) stuff)
                 (('multiline-string stuff) stuff)
                 (('dict records ...) records))
	       acc))))
        #f file))

(define (metadata-ref file lookup)
                      ^ 2 arguments
  (fold (lambda (record acc)
                  ^2 formals, why the acc?
          (match record
                 ^input
            ((record key val)
               ^match "record" "key" "val" in a list?
             (if (equal? key lookup)
                          ^ we continue if the key is right
	       (match val
                       ^input
                 (('list-pat . stuff) stuff)
                     ^ if 'list-pat return stuff
                 (('string-pat stuff) stuff)
                     ^ if 'string-pat return stuff
                 (('multiline-string stuff) stuff)
                 (('dict records ...) records))
                    ^if 'dict return first record
	       acc))))
                ^ this is the else (what is this good for?)
        #f file))
             ^ input to fold
         ^ no initial in the fold

If I understood this correctly I hope I will be able to get something
like this to work in the quicklisp importer :)

-- 
Cheers 
Swedebugia

  reply	other threads:[~2019-01-05 21:26 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-18 20:45 `guix lint' warn of GitHub autogenerated source tarballs Arun Isaac
2018-12-19  8:43 ` Pierre Neidhardt
2018-12-19 14:07 ` Ludovic Courtès
2018-12-19 14:33   ` Efraim Flashner
2018-12-19 17:43     ` Arun Isaac
2018-12-19 19:29       ` Efraim Flashner
2018-12-21 20:50         ` Ludovic Courtès
2018-12-21 21:00           ` swedebugia
2018-12-25 14:32           ` Efraim Flashner
2019-01-05 17:39             ` Ludovic Courtès
2019-01-05 21:25               ` swedebugia [this message]
2019-01-05 22:35                 ` Learning the match-syntax Ricardo Wurmus
2019-01-06 21:36                 ` Chris Marusich
2019-01-07 17:34                   ` swedebugia
2019-01-07 22:18                     ` Ricardo Wurmus
2019-01-08  8:25                       ` swedebugia
2019-01-08 20:32                         ` Ricardo Wurmus

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

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

  git send-email \
    --in-reply-to=c446d9551edfc72694c566d7ad90cae0@riseup.net \
    --to=swedebugia@riseup.net \
    --cc=guix-devel-bounces+swedebugia=riseup.net@gnu.org \
    --cc=guix-devel@gnu.org \
    --cc=ludo@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.