Re: [racket-users] parameterize and keyword - doable?

2018-08-04 Thread Philip McGrath
On Sat, Aug 4, 2018 at 3:18 PM, Sanjeev Sharma  wrote:

> but as long as one is defaulting #:unless
> PLUS #unless is common to all the for's across some arbitrary scope,
> ideally one could elide it from each of the for's within that scope.
>

Local macros might be part of what you want:

#lang racket

(define (f unless?)
  (define-syntax-rule (for/sum/unless ([id rhs]
   for-clause ...)
body ...)
(for/sum ([id rhs]
  #:unless (unless? id)
  for-clause ...)
  body ...))
  (list (for/sum/unless ([x '(1 2 3 4 5)])
  x)
(for/sum/unless ([y '(6 7 8 9 10)]
 [2y (in-value (* 2 y))])
  (displayln y)
  (add1 2y

(f odd?)

(f even?)

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] parameterize and keyword - doable?

2018-08-04 Thread Sanjeev Sharma
the specific case I had in mind was near this

(let ([unless? odd?]) 
  (for/sum ([x '(1 2 3 4 5)] 
#:unless (uless? x)) 
x)) ;6 

but as long as one is defaulting #:unless 
PLUS #unless is common to all the for's across some arbitrary scope, 
ideally one could elide it from each of the for's within that scope. 

your current-unless looks a good candidate for another place. 



On Saturday, August 4, 2018 at 3:47:42 PM UTC-4, Greg Hendershott wrote:
>
> Do you mean that in something like this: 
>
> (for/sum ([x '(1 2 3 4 5)] 
>   #:unless (odd? x)) 
>   x) ;6 
>
> You have many occurrences of the `(odd? x)` and you'd like to define 
> that in one place that you can vary? 
>
> If so: 
>
> First, if they're in the same local scope, you could use `let`: 
>
> (let ([unless? odd?]) 
>   (for/sum ([x '(1 2 3 4 5)] 
> #:unless (uless? x)) 
> x)) ;6 
>
> Or (same difference) if they're in the same function definition, you 
> could use a function parameter: 
>
> (define (f unless?) 
>   (for/sum ([x '(1 2 3 4 5)] 
> #:unless (unless? x)) 
> x)) 
> (f odd?) ;6 
>
> Or -- and maybe this is what you're asking? -- you could define a 
> Racket parameter: 
>
> (define current-unless? (make-parameter (const #f))) 
>
> (define (f) 
>   (for/sum ([x '(1 2 3 4 5)] 
> #:unless ((current-unless?) x)) 
> x)) 
>
> (parameterize ([current-unless? odd?]) 
>   (f)) ;6 
>
> I think this is the best you can do; the `for` macros don't provide a 
> parameter to inject an #:unless clause. 
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] parameterize and keyword - doable?

2018-08-04 Thread Greg Hendershott
Do you mean that in something like this:

(for/sum ([x '(1 2 3 4 5)]
  #:unless (odd? x))
  x) ;6

You have many occurrences of the `(odd? x)` and you'd like to define
that in one place that you can vary?

If so:

First, if they're in the same local scope, you could use `let`:

(let ([unless? odd?])
  (for/sum ([x '(1 2 3 4 5)]
#:unless (uless? x))
x)) ;6

Or (same difference) if they're in the same function definition, you
could use a function parameter:

(define (f unless?)
  (for/sum ([x '(1 2 3 4 5)]
#:unless (unless? x))
x))
(f odd?) ;6

Or -- and maybe this is what you're asking? -- you could define a
Racket parameter:

(define current-unless? (make-parameter (const #f)))

(define (f)
  (for/sum ([x '(1 2 3 4 5)]
#:unless ((current-unless?) x))
x))

(parameterize ([current-unless? odd?])
  (f)) ;6

I think this is the best you can do; the `for` macros don't provide a
parameter to inject an #:unless clause.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] parameterize and keyword - doable?

2018-08-03 Thread Philip McGrath
I'm still not sure that I understand what the original questioner is trying
to do. It might help to see an example in code to be clear that we're all
talking about the same thing.

A few points for the list, though:

On Fri, Aug 3, 2018 at 10:53 PM, George Neuner  wrote:

>   (define mykey (make-parameter (string->keyword "#:unless")))
>

Using (string->keyword "#:unless") will produce the same keyword value as
(quote #:#:unless), which is *not* the same as the value of (quote
#:unless).


> One thing you might do is define the functions to take a plist "rest"
> argument, use ordinary symbols for keywords, and parse the arguments from
> the list.  …
>   (define (f x y . keys ) ... )
>   (f 1 2)
>   (f 1 2 'unless #t )
>

This is absolutely possible, but I would strongly advise against it.

You can call a function with dynamically calculated keyword arguments using
keyword-apply
,
and you can create a function that accepts arbitrary or
dynamically-calculated keyword arguments (which is rarely desirable) using
make-keyword-procedure
.
These support essentially the same interface as an ad-hoc "keyword" system
using symbols, but take advantage of Racket's (current and future)
optimizations and features for keyword functions. Perhaps most importantly,
clients that *don't *need to dynamically calculate keywords can use the
normal calling conventions without having to worry about all this.

-Philip

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] parameterize and keyword - doable?

2018-08-03 Thread George Neuner


On 8/3/2018 11:52 AM, Sanjeev Sharma wrote:
make a keyword useable as the parameter-expr in a parameterize 
expression.
for example, If I need a similar #:unless cause for  a bunch of for 
expressions
It's not a current issue, but would be good  to have in the toolbox 
for next time.


If I'm understanding correctly, then no.  You can make the keyword 
symbol a parameter:


      (define mykey (make-parameter (string->keyword "#:unless")))

but you can't use it AS a keyword in a define, and you can't you use it 
in a keyword position in a function call.

e.g.,

      (define (f (mykey) [k #f] )  ... )
  > ERR: (mykey) not an identifier

      (define (g #:unless [k #f] )  ... )
  (g (mykey) 42)
      > ERR: arity mismatch


You probably can do something using macros, but I am not sure how. I'm 
not aware of a way in Racket to make a keyword name variable, or to 
modify a function's keyword list at runtime [some languages can do this].


One thing you might do is define the functions to take a plist "rest" 
argument, use ordinary symbols for keywords, and parse the arguments 
from the list.  It's annoying to do for a lot of functions, but if you 
have many functions using the same keyword list, you can break out the 
key parsing into a separate function.


e.g.,

      (define (f x y . keys ) ... )
  (f 1 2)
  (f 1 2 'unless #t )


George

--
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] parameterize and keyword - doable?

2018-08-03 Thread Sanjeev Sharma
make a keyword useable as the parameter-expr in a parameterize expression. 

for example, If I need a similar #:unless cause for  a bunch of for 
expressions 

It's not a current issue, but would be good  to have in the toolbox for 
next time. 


On Thursday, August 2, 2018 at 8:25:31 PM UTC-4, gneuner2 wrote:
>
>
> On 8/2/2018 7:45 PM, Sanjeev Sharma wrote: 
> > can racket's  #: keywords be finagled / coerced into a parameterizable 
> > form? 
>
> ??? 
>
> Certainly you can pass a parameter to a keyword argument, and/or make a 
> parameter the default value of a keyword argument.  And you can use an 
> argument to parameterize code lower down in the call stack.  If you mean 
> something else, could you perhaps explain it better? 
>
> George 
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] parameterize and keyword - doable?

2018-08-02 Thread George Neuner



On 8/2/2018 7:45 PM, Sanjeev Sharma wrote:
can racket's  #: keywords be finagled / coerced into a parameterizable 
form?


???

Certainly you can pass a parameter to a keyword argument, and/or make a 
parameter the default value of a keyword argument.  And you can use an 
argument to parameterize code lower down in the call stack.  If you mean 
something else, could you perhaps explain it better?


George

--
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.
For more options, visit https://groups.google.com/d/optout.