Re: [racket-users] Passing keywords from a list

2020-11-28 Thread Dimaugh Silvestris
Just in case somebody has a similar issue and finds this:
In the end I used keyword-apply/sort , which doesn't requiere keywords to 
follow the same order they do in the function definition.

On Thursday, 26 November 2020 at 10:42:40 UTC+1 Dimaugh Silvestris wrote:

> Thanks! Your answer led me to find keyword-apply, so I just wrote (I use 
> def as abbreviation for define):
>
> (def (parse-args xs)
>   (def (fn ks kvs vs xs)
> (cond [(empty? xs)  (map reverse (list ks kvs vs))]
>   [(keyword? (car xs))
>(fn (cons (car xs) ks) (cons (cadr xs) kvs) vs (drop xs 2))]
>   [else (fn ks kvs (cons (car xs) vs) (cdr xs))]))
>   (fn '[] '[] '[] xs))
>
> (def (func-args func args)
>   (apply keyword-apply func (parse-args args)))
>
> And now I can do 
> :
> (def (suma a b #:c [c 0]) (+ a b c))
> (func-args suma (list 1 2 #:c 3))
>
> and it works fine.
>
> Thanks again!
> On Thursday, 26 November 2020 at 10:06:26 UTC+1 sorawe...@gmail.com wrote:
>
>> If you are OK with preprocessing the argument list into a dictionary, 
>> then you can use keyword-apply/dict 
>> 
>> .
>>
>> For example:
>>
>> #lang racket
>>
>> (require racket/dict)
>>
>> (define proc (lambda (#:color color #:size size) (display (cons color 
>> size
>> (define args '(#:color "red" #:size 3))
>> (define args*
>>   (for/hash ([chunk (in-slice 2 args)])
>> (values (first chunk) (second chunk
>>
>> (keyword-apply/dict proc args* '())
>>
>>
>> On Thu, Nov 26, 2020 at 12:53 AM Dimaugh Silvestris <
>> dimaughs...@gmail.com> wrote:
>>
>>> Is it possible to reproduce this behavior
>>> ((lambda (#:color color #:size size) (display (cons color size))) 
>>> #:color "red" #:size 3)
>>> when what I have is a list such as '(#:color "red" #:size 3) ?
>>> How can I feed keyword arguments stored in a list as symbols in a way 
>>> that doesn't involve parsing manually?
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to racket-users...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/racket-users/06a1b45b-77ca-4595-b5b6-0b4ce01fe026n%40googlegroups.com
>>>  
>>> 
>>> .
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/432319aa-ae3c-4644-8ac8-1de12f0b6c39n%40googlegroups.com.


Re: [racket-users] Passing keywords from a list

2020-11-26 Thread Dimaugh Silvestris
Thanks! Your answer led me to find keyword-apply, so I just wrote (I use 
def as abbreviation for define):

(def (parse-args xs)
  (def (fn ks kvs vs xs)
(cond [(empty? xs)  (map reverse (list ks kvs vs))]
  [(keyword? (car xs))
   (fn (cons (car xs) ks) (cons (cadr xs) kvs) vs (drop xs 2))]
  [else (fn ks kvs (cons (car xs) vs) (cdr xs))]))
  (fn '[] '[] '[] xs))

(def (func-args func args)
  (apply keyword-apply func (parse-args args)))

And now I can do 
:
(def (suma a b #:c [c 0]) (+ a b c))
(func-args suma (list 1 2 #:c 3))

and it works fine.

Thanks again!
On Thursday, 26 November 2020 at 10:06:26 UTC+1 sorawe...@gmail.com wrote:

> If you are OK with preprocessing the argument list into a dictionary, then 
> you can use keyword-apply/dict 
> 
> .
>
> For example:
>
> #lang racket
>
> (require racket/dict)
>
> (define proc (lambda (#:color color #:size size) (display (cons color size
> (define args '(#:color "red" #:size 3))
> (define args*
>   (for/hash ([chunk (in-slice 2 args)])
> (values (first chunk) (second chunk
>
> (keyword-apply/dict proc args* '())
>
>
> On Thu, Nov 26, 2020 at 12:53 AM Dimaugh Silvestris  
> wrote:
>
>> Is it possible to reproduce this behavior
>> ((lambda (#:color color #:size size) (display (cons color size))) #:color 
>> "red" #:size 3)
>> when what I have is a list such as '(#:color "red" #:size 3) ?
>> How can I feed keyword arguments stored in a list as symbols in a way 
>> that doesn't involve parsing manually?
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/06a1b45b-77ca-4595-b5b6-0b4ce01fe026n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/413ea474-0eac-4c7a-97a4-0c4ee711eb60n%40googlegroups.com.


Re: [racket-users] Passing keywords from a list

2020-11-26 Thread Sorawee Porncharoenwase
If you are OK with preprocessing the argument list into a dictionary, then
you can use keyword-apply/dict

.

For example:

#lang racket

(require racket/dict)

(define proc (lambda (#:color color #:size size) (display (cons color size
(define args '(#:color "red" #:size 3))
(define args*
  (for/hash ([chunk (in-slice 2 args)])
(values (first chunk) (second chunk

(keyword-apply/dict proc args* '())


On Thu, Nov 26, 2020 at 12:53 AM Dimaugh Silvestris <
dimaughsilvest...@gmail.com> wrote:

> Is it possible to reproduce this behavior
> ((lambda (#:color color #:size size) (display (cons color size))) #:color
> "red" #:size 3)
> when what I have is a list such as '(#:color "red" #:size 3) ?
> How can I feed keyword arguments stored in a list as symbols in a way that
> doesn't involve parsing manually?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/06a1b45b-77ca-4595-b5b6-0b4ce01fe026n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegtRx--ZTxg0sNGb3475Pqb9vw_Hun%2BNp-A76SO8xGg1nQ%40mail.gmail.com.


[racket-users] Passing keywords from a list

2020-11-26 Thread Dimaugh Silvestris
Is it possible to reproduce this behavior
((lambda (#:color color #:size size) (display (cons color size))) #:color 
"red" #:size 3)
when what I have is a list such as '(#:color "red" #:size 3) ?
How can I feed keyword arguments stored in a list as symbols in a way that 
doesn't involve parsing manually?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/06a1b45b-77ca-4595-b5b6-0b4ce01fe026n%40googlegroups.com.