all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Help with map match
@ 2018-11-29 23:13 swedebugia
  2018-11-30  2:45 ` Ricardo Wurmus
  2018-11-30 13:34 ` Adam Van Ymeren
  0 siblings, 2 replies; 4+ messages in thread
From: swedebugia @ 2018-11-29 23:13 UTC (permalink / raw)
  To: guix-devel

Hi

I'm trying to learn how to check in guile if a node package matches one 
of the items in my blacklist.

First version was with (member pkg-name blacklist) and it worked but 
forces me to write down all the blacklisted packages which is tedious 
when I can just regex match if it begins with the same name.

Now i'm trying to do the same with match and I could not find any 
examples or guides on the subject that I understood. :-/

blacklist
$1 = ("matcha" "webpack" "rollup-plugin-node-resolve" "browserify" 
"electron" "statsd" "vega" "grunt-release" "lineman" "lineman-angular")

I want the procedure to return #t if a match is found and I want it to 
match if it begins with the same e.g. "rollup-plugin" should match 
"rollup-plugin-node-resolve" and return #t
Else #f

This did not work:
(use-modules (ice-9 match))

(define x
         '("ts" "test"))
(map (match x
         ("test") #t)
         (else #f) x)

Any ideas?
-- 
Cheers
Swedebugia

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

* Re: Help with map match
  2018-11-29 23:13 Help with map match swedebugia
@ 2018-11-30  2:45 ` Ricardo Wurmus
  2018-11-30 13:34 ` Adam Van Ymeren
  1 sibling, 0 replies; 4+ messages in thread
From: Ricardo Wurmus @ 2018-11-30  2:45 UTC (permalink / raw)
  To: swedebugia; +Cc: guix-devel


Hi,

> I want the procedure to return #t if a match is found and I want it to
> match if it begins with the same e.g. "rollup-plugin" should match
> "rollup-plugin-node-resolve" and return #t
> Else #f

> This did not work:
> (use-modules (ice-9 match))
>
> (define x
>         '("ts" "test"))
> (map (match x
>         ("test") #t)
>         (else #f) x)

“map” requires a procedure and a list as its arguments, applies the
procedure to every element and then returns a new list with the results.

Since you just want a single boolean as the return value you may want to
use “any” instead, which tells you if a procedure returned #t for “any”
of the items in a list.

--8<---------------cut here---------------start------------->8---
(use-modules (srfi srfi-1))

(define (blacklisted? pkg-name)
  (string-prefix? "rollup-" pkg-name))

(any blacklisted?
 '("rollup-plugin" "rollup-plugin-foo")) ; => #t

(any blacklisted?
 '("all" "is" "good")) ; => #f
--8<---------------cut here---------------end--------------->8---

“blacklisted?” can then implement whatever logic you need.

--
Ricardo

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

* Re: Help with map match
  2018-11-29 23:13 Help with map match swedebugia
  2018-11-30  2:45 ` Ricardo Wurmus
@ 2018-11-30 13:34 ` Adam Van Ymeren
  2018-11-30 13:36   ` Adam Van Ymeren
  1 sibling, 1 reply; 4+ messages in thread
From: Adam Van Ymeren @ 2018-11-30 13:34 UTC (permalink / raw)
  To: guix-devel, swedebugia

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

I believe match is a macro but you need to pass map a lambda.

Try (map (lambda (a) (match a ("test" #t) ...) x) 

Or (map (match-lambda ("test" #t) ...) x) as match-lambda is a macro that expands to (lambda (a) (match a ...))


On November 29, 2018 6:13:26 PM EST, swedebugia <swedebugia@riseup.net> wrote:
>Hi
>
>I'm trying to learn how to check in guile if a node package matches one
>
>of the items in my blacklist.
>
>First version was with (member pkg-name blacklist) and it worked but 
>forces me to write down all the blacklisted packages which is tedious 
>when I can just regex match if it begins with the same name.
>
>Now i'm trying to do the same with match and I could not find any 
>examples or guides on the subject that I understood. :-/
>
>blacklist
>$1 = ("matcha" "webpack" "rollup-plugin-node-resolve" "browserify" 
>"electron" "statsd" "vega" "grunt-release" "lineman" "lineman-angular")
>
>I want the procedure to return #t if a match is found and I want it to 
>match if it begins with the same e.g. "rollup-plugin" should match 
>"rollup-plugin-node-resolve" and return #t
>Else #f
>
>This did not work:
>(use-modules (ice-9 match))
>
>(define x
>         '("ts" "test"))
>(map (match x
>         ("test") #t)
>         (else #f) x)
>
>Any ideas?
>-- 
>Cheers
>Swedebugia

[-- Attachment #2: Type: text/html, Size: 1595 bytes --]

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

* Re: Help with map match
  2018-11-30 13:34 ` Adam Van Ymeren
@ 2018-11-30 13:36   ` Adam Van Ymeren
  0 siblings, 0 replies; 4+ messages in thread
From: Adam Van Ymeren @ 2018-11-30 13:36 UTC (permalink / raw)
  To: guix-devel, swedebugia

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

Just saw Ricardo's message. Sorry for the redundancy!

On November 30, 2018 8:34:46 AM EST, Adam Van Ymeren <adam@vany.ca> wrote:
>I believe match is a macro but you need to pass map a lambda.
>
>Try (map (lambda (a) (match a ("test" #t) ...) x) 
>
>Or (map (match-lambda ("test" #t) ...) x) as match-lambda is a macro
>that expands to (lambda (a) (match a ...))
>
>
>On November 29, 2018 6:13:26 PM EST, swedebugia <swedebugia@riseup.net>
>wrote:
>>Hi
>>
>>I'm trying to learn how to check in guile if a node package matches
>one
>>
>>of the items in my blacklist.
>>
>>First version was with (member pkg-name blacklist) and it worked but 
>>forces me to write down all the blacklisted packages which is tedious 
>>when I can just regex match if it begins with the same name.
>>
>>Now i'm trying to do the same with match and I could not find any 
>>examples or guides on the subject that I understood. :-/
>>
>>blacklist
>>$1 = ("matcha" "webpack" "rollup-plugin-node-resolve" "browserify" 
>>"electron" "statsd" "vega" "grunt-release" "lineman"
>"lineman-angular")
>>
>>I want the procedure to return #t if a match is found and I want it to
>
>>match if it begins with the same e.g. "rollup-plugin" should match 
>>"rollup-plugin-node-resolve" and return #t
>>Else #f
>>
>>This did not work:
>>(use-modules (ice-9 match))
>>
>>(define x
>>         '("ts" "test"))
>>(map (match x
>>         ("test") #t)
>>         (else #f) x)
>>
>>Any ideas?
>>-- 
>>Cheers
>>Swedebugia

[-- Attachment #2: Type: text/html, Size: 1911 bytes --]

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

end of thread, other threads:[~2018-11-30 13:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-11-29 23:13 Help with map match swedebugia
2018-11-30  2:45 ` Ricardo Wurmus
2018-11-30 13:34 ` Adam Van Ymeren
2018-11-30 13:36   ` Adam Van Ymeren

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.