Re: Usecase: chaining operations after "optionals"
Marc Nieper-Wißkirchen 09 Jun 2020 17:48 UTC
And this is the canonical R7RS-small solution:
(cond ((assoc 'foo '((foo . bar))) => (lambda (x) (symbol->string (cdr
x)))) (else #f))
(Not that your examples are all missing a `cdr', I think.)
As we can see, R7RS-small already provides all we need. :) And it's
even quite idiomatic.
Am Di., 9. Juni 2020 um 19:24 Uhr schrieb Adam Nelson <xxxxxx@nels.onl>:
>
> On 6/9/20 1:18 PM, Arne Babenhauserheide wrote:
>
> Hi,
>
> I’ve long had a usecase without a good solution: chaining an operation
> after assoc.
>
> Would this work?
>
> (and-> (assoc 'foo '((foo . bar)))
> symbol->string)
>
> The result would be "bar" in this case, but in the other case
>
> (and-> (assoc 'foo '((something . else)))
> symbol->string)
>
> the result would be #f without ever calling symbol->string.
>
> Best wishes,
> Arne
>
> Yes, this seems like an ideal use case for this this macro, and should probably be in the document.
>
> This seems like a good opportunity to spell out this expression in some of the syntaxes we're thinking about right now, to see how they compare.
>
> This is how it would be done right now in SRFI 2:
>
> (and-let* ((x (assoc 'foo '((foo . bar))))) (symbol->string x))
>
> And here are some of the syntaxes we're considering:
>
> (and=> (assoc 'foo '((foo . bar))) (symbol->string))
>
> (and-chain (assoc 'foo '((foo . bar))) (symbol->string))
>
> (and-chain (assoc 'foo '((foo . bar))) (symbol->string <>))
>
> (and-bind (assoc 'foo '((foo . bar))) (cut symbol->string <>))
>
> There's actually not as much difference between these as I was expecting.