sschwarzer
2021-12-29 16:34:28

Is there a way to pick only a particular value from several? For example, I want to use https://docs.racket-lang.org/reference/Manipulating_Paths.html#%28def._%28%28quote._~23~25kernel%29._split-path%29%29\|split-path , which returns three values, but I’m only interested in the second value. Of course, I could write (define-values (base name must-be-dir?) (split-path original-path)) and use only name , but I wonder if there’s a more straightforward way.


sorawee
2021-12-29 16:37:45

Use match-define-values


sschwarzer
2021-12-29 16:46:07

Thanks, I’ll use (match-define-values (_ base-name _) (split-path ...))


sschwarzer
2021-12-29 16:47:45

For the record, I also found https://docs.racket-lang.org/reference/let.html#%28form._%28%28quote._~23~25kernel%29._let-values%29%29\|let-values , which is similar, but I think match-define-values is better for my use case.


sorawee
2021-12-29 16:48:15

There’s also match-let-values, FWIW


sorawee
2021-12-29 16:52:37

But if you don’t want to bind the value to a variable immediately, another possibility is:

(call-with-values (lambda () <expr-here>) (lambda xs (list-ref xs i))) This is more useful when <expr-here> results in a lot of values, and you don’t want to type _ _ _ _


ben.knoble
2021-12-29 17:24:49

Using qi: (define name (~> (original-path) split-path 2>)) Depending on what happens with them next, you may be able to continue the flow (edit: you only needed one value, so simplified)


massung
2021-12-29 17:25:39

I don’t know if Racket has an equivalent (a quick scan of the docs doesn’t seem to reveal one), but CL has nth-value, so maybe.