Re: [racket-users] Macro to extract select subexpressions into other locations

2015-07-21 Thread Alexander D. Knauth
Here's a macro that does something similar:

#lang at-exp racket

(require (for-syntax syntax/parse
 racket/contract
 ))

(begin-for-syntax
  (define (get-extracted-exprs-box extracted-exprs-id)
(syntax-local-value extracted-exprs-id))
  (define (get-extracted-exprs extracted-exprs-id)
(unbox (get-extracted-exprs-box extracted-exprs-id)))
  (define (box-update! b f)
(set-box! b (f (unbox b
  (define (add-extracted-expr! extracted-exprs-id new-expr)
(box-update! (get-extracted-exprs-box extracted-exprs-id)
 (λ (lst) (append lst (list (syntax-local-introduce 
new-expr)))

(define-syntax extract-expression
  (syntax-parser
[(extract-expression expr:expr extracted-exprs-id:id)
 (add-extracted-expr! #'extracted-exprs-id #'expr)
 #'expr]))

(define-syntax with-extract-expressions
  (syntax-parser
[(with-extract-expressions stx-expr:expr ...)
 #:with [extracted-exprs macro-id:id ...]
 (generate-temporaries #'[extracted-expressions stx-expr ...])
 #'(begin
 (define-syntax extracted-exprs (box '()))
 (begin
   (define-syntax macro-id
 (lambda (stx)
   (stx-expr #'extracted-exprs)))
   (macro-id))
 ...)]))

(define (square x) (* x x))

(with-extract-expressions
 (lambda (exprs)
   (with-syntax ([exprs exprs])
 #'(module+ test
 (require rackunit)
 (check-equal? (extract-expression (square 5) exprs) 25)
 (check-equal? (extract-expression (square -5) exprs) 25
 (lambda (exprs)
   (with-syntax ([(expr ...) (get-extracted-exprs exprs)])
 #'(module+ doc
 (require scribble/manual
  scribble/eval)
 (define evaluator (make-base-eval))
 @defproc[(square [x real?]) real?]{
   Returns the square of @racket[x], the result of
   multiplying @racket[x] by itself.
   @examples[#:eval evaluator expr ...]}


On Jul 20, 2015, at 8:03 PM, Jack Firth jackhfi...@gmail.com wrote:

 I'm trying to create a way to automatically turn test cases into examples. 
 I'd like a macro that turns this:
 
 (extract-expressions
 (module+ test
   (check-equal? (extract-expression (square 5)) 25)
   (check-equal? (extract-expression (square -5)) 25))
 (module+ doc
   @defproc[(square [x real?]) real?]{
 Returns the square of @racket[x], the result of
 multiplying @racket[x] by itself.
 @examples[#:eval evaluator (include-extracted-expressions)]}))
 
 into this:
 
 (begin
  (module+ test
(check-equal? (square 5) 25))
  (module+ doc
@defproc[(square [x real?]) real?]{
  Returns the square of @racket[x], the result of
  multiplying @racket[x] by itself.
  @examples[#:eval evaluator (square 5) (square -5)]}))
 
 That's the general idea anyway. I want to be able to gather arbitrary 
 expressions inside a region and copy them into some other marked place 
 inside the expressions. How exactly do I go about doing this, and how should 
 I do it properly?
 
 -- 
 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.

-- 
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.


[racket-users] About redrawing buttons on-demand in gui framework

2015-07-21 Thread Rickard Andersson

Hi, everyone.

I've stumbled upon some unforeseen strangeness regarding the redrawing of 
certain elements in the GUI framework recently.


Initially, I assumed that a set-label call for a button, for example, 
would redraw that button automatically, with the new dimensions and 
everything in mind.


When that wasn't the case, I looked for ways to force a redraw of the 
button, but the only things I could find were reflow-container and the 
associated methods. They only seem to apply to containers, however, and 
won't actually redraw the buttons in those containers.


The ugly hack that I had to settle on in lieu of finding a better choice 
is to just recreate the buttons on demand, but this brought a lot of 
baggage in terms of having to make sure all references still point to the 
button, etc., so I would've liked to have a version that simply redraws 
the exact same object with another label.


Any help is appreciated, as I'd like to avoid these changes becoming too 
ingrained in my code.


// Rickard Andersson

--
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] Macro introduce identifiers in the new expander

2015-07-21 Thread Spencer Florence
That fixed the example I gave, but now this fails:

(let ()
  (def)
  (let ()
(use)))

On Mon, Jul 20, 2015 at 2:50 PM Matthew Flatt mfl...@cs.utah.edu wrote:

 Repair pushed.



  On Jul 20, 2015, at 11:14 AM, Matthew Flatt mfl...@cs.utah.edu wrote:
 
  Thanks for the info. I think it's a bug in the expander, and I have a
  repair, but I think that repair might point to another bug that I'm
  still investigating.
 
  At Mon, 20 Jul 2015 12:56:49 -0400, Alexander D. Knauth wrote:
  I don't really know what's going on, but this might help:
 
  ;; It seems to be introducing the definition correctly, but not the use:
  ;; this works
  (let ()
   (def) ; no difference between (def) and (define id 5)
   id)
 
  ;; this doesn't
  (let ()
   (def)
   (use)) ; but there is a difference between (use) and id
 
  ;; and this doesn't either
  (let ()
   (define id 5)
   (use))
 
  On Jul 20, 2015, at 12:35 PM, Spencer Florence spen...@florence.io
 wrote:
 
  Hello,
 
  I'm trying to update some code to the new expander. The below code
 works on
  6.2 but fails on the new expander with an unbound identifier error.
 
  #lang racket
  (require (for-syntax syntax/parse))
 
  ;; a standard context for identifiers
  (define-for-syntax ctx #'ctx)
 
  ;; create an ID with the context `ctx`, and the current
  ;; expander mark (so that the mark is canceled later),
  ;; and the location loc
  (define-for-syntax (make-id loc)
   (syntax-local-introduce
(datum-syntax ctx 'id loc)))
 
  ;; This introduces a binding
  (define-syntax (def stx)
   (syntax-parse stx
 [(def)
  (with-syntax ([id (make-id #'here)])
#'(define id 5))]))
 
  ;; this attempts to use the binding introduced by `def`
  (define-syntax (use stx)
   (syntax-parse stx
 [(use)
  (with-syntax ([id (make-id #'here)])
#'id)]))
 
  ;; this works
  #;
  (begin
   (def)
   (use))
 
  ;; this fails
  (let ()
   (def)
   (use))
 
  --
  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.
 
  --
  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.
 
  --
  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.
 

 --
 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.


-- 
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] Macro to extract select subexpressions into other locations

2015-07-21 Thread Alexander D. Knauth
Oh. I checked that that the expressions were accumulating in the definition of 
extract-expression, but I didn't check that it expanded in the right order to 
do what you want.

So this doesn't actually work. 

On Jul 21, 2015, at 3:08 PM, Alexander D. Knauth alexan...@knauth.org wrote:

 Here's a macro that does something similar:
 
 #lang at-exp racket
 
 (require (for-syntax syntax/parse
 racket/contract
 ))
 
 (begin-for-syntax
  (define (get-extracted-exprs-box extracted-exprs-id)
(syntax-local-value extracted-exprs-id))
  (define (get-extracted-exprs extracted-exprs-id)
(unbox (get-extracted-exprs-box extracted-exprs-id)))
  (define (box-update! b f)
(set-box! b (f (unbox b
  (define (add-extracted-expr! extracted-exprs-id new-expr)
(box-update! (get-extracted-exprs-box extracted-exprs-id)
 (λ (lst) (append lst (list (syntax-local-introduce 
 new-expr)))
 
 (define-syntax extract-expression
  (syntax-parser
[(extract-expression expr:expr extracted-exprs-id:id)
 (add-extracted-expr! #'extracted-exprs-id #'expr)
 #'expr]))
 
 (define-syntax with-extract-expressions
  (syntax-parser
[(with-extract-expressions stx-expr:expr ...)
 #:with [extracted-exprs macro-id:id ...]
 (generate-temporaries #'[extracted-expressions stx-expr ...])
 #'(begin
 (define-syntax extracted-exprs (box '()))
 (begin
   (define-syntax macro-id
 (lambda (stx)
   (stx-expr #'extracted-exprs)))
   (macro-id))
 ...)]))
 
 (define (square x) (* x x))
 
 (with-extract-expressions
 (lambda (exprs)
   (with-syntax ([exprs exprs])
 #'(module+ test
 (require rackunit)
 (check-equal? (extract-expression (square 5) exprs) 25)
 (check-equal? (extract-expression (square -5) exprs) 25
 (lambda (exprs)
   (with-syntax ([(expr ...) (get-extracted-exprs exprs)])
 #'(module+ doc
 (require scribble/manual
  scribble/eval)
 (define evaluator (make-base-eval))
 @defproc[(square [x real?]) real?]{
   Returns the square of @racket[x], the result of
   multiplying @racket[x] by itself.
   @examples[#:eval evaluator expr ...]}
 
 
 On Jul 20, 2015, at 8:03 PM, Jack Firth jackhfi...@gmail.com wrote:
 
 I'm trying to create a way to automatically turn test cases into examples. 
 I'd like a macro that turns this:
 
 (extract-expressions
 (module+ test
  (check-equal? (extract-expression (square 5)) 25)
  (check-equal? (extract-expression (square -5)) 25))
 (module+ doc
  @defproc[(square [x real?]) real?]{
Returns the square of @racket[x], the result of
multiplying @racket[x] by itself.
@examples[#:eval evaluator (include-extracted-expressions)]}))
 
 into this:
 
 (begin
 (module+ test
   (check-equal? (square 5) 25))
 (module+ doc
   @defproc[(square [x real?]) real?]{
 Returns the square of @racket[x], the result of
 multiplying @racket[x] by itself.
 @examples[#:eval evaluator (square 5) (square -5)]}))
 
 That's the general idea anyway. I want to be able to gather arbitrary 
 expressions inside a region and copy them into some other marked place 
 inside the expressions. How exactly do I go about doing this, and how should 
 I do it properly?
 
 -- 
 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.
 
 -- 
 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.

-- 
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] Re: How to draw a 3D ball?

2015-07-21 Thread JK


Le 20/07/2015 11:03, Mianlai Zhou a écrit :

Hi,

Thanks for your answer. I meant the former, i.e., a 2D pict of a ball 
with some shading to make it look 3D.
Is there any quick way to do it or I have to implement the algorithm 
by myself?






The answer is more general than just in the context of Racket...
Do you want something of this kind (sorry for the quality, it was just a 
30 second test, it could be better).
The algorithm is just a radial gradient, and a clipping circle. Its 
'softness' translates visually into the amount of specularity, or the 
roughness, if you prefer. Some exquisite parametrization may be found 
anywhere, e. g. here (a page on SVG):


http://doc.qt.io/qt-5/qtsvg-svgviewer-files-spheres-svg.html

which gives:



See also the book
Flash 3D Cheats Most Wanted by Yard, Balkan, et al.

Now, the Racket solution ...
You will find the information about radial gradients (with examples) 
here, in your local doc:

... /doc/draw/radial-gradient_.html?q=gradient#%28tech._radial._gradient%29

On line:
http://docs.racket-lang.org/draw/radial-gradient_.html#%28tech._radial._gradient%29

To have something more realistic, you should combine (at least) the 
diffuse and specular lighting, which is non-linear. Quite easy, and 
implemented in the 3D simulation in SVG, and several drawing packages, 
but all depends on your ambitions. For simple drawings, a quite trivial 
CSS suffices, there is a radial gradient in the standard, and multiple 
colour stops are possible.


http://www.w3schools.com/css/css3_gradients.asp



Best regards

Jerzy Karczmarczuk
/Caen, France/




--
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] Macro to extract select subexpressions into other locations

2015-07-21 Thread Matthias Felleisen


Why not step back and design a notation where testing and documenting can share 
concepts instead of retro-actively extracting expressions from one place to put 
somewhere else. -- Matthias







On Jul 21, 2015, at 3:23 PM, Alexander D. Knauth alexan...@knauth.org wrote:

 Oh. I checked that that the expressions were accumulating in the definition 
 of extract-expression, but I didn't check that it expanded in the right order 
 to do what you want.
 
 So this doesn't actually work. 
 
 On Jul 21, 2015, at 3:08 PM, Alexander D. Knauth alexan...@knauth.org wrote:
 
 Here's a macro that does something similar:
 
 #lang at-exp racket
 
 (require (for-syntax syntax/parse
racket/contract
))
 
 (begin-for-syntax
 (define (get-extracted-exprs-box extracted-exprs-id)
   (syntax-local-value extracted-exprs-id))
 (define (get-extracted-exprs extracted-exprs-id)
   (unbox (get-extracted-exprs-box extracted-exprs-id)))
 (define (box-update! b f)
   (set-box! b (f (unbox b
 (define (add-extracted-expr! extracted-exprs-id new-expr)
   (box-update! (get-extracted-exprs-box extracted-exprs-id)
(λ (lst) (append lst (list (syntax-local-introduce 
 new-expr)))
 
 (define-syntax extract-expression
 (syntax-parser
   [(extract-expression expr:expr extracted-exprs-id:id)
(add-extracted-expr! #'extracted-exprs-id #'expr)
#'expr]))
 
 (define-syntax with-extract-expressions
 (syntax-parser
   [(with-extract-expressions stx-expr:expr ...)
#:with [extracted-exprs macro-id:id ...]
(generate-temporaries #'[extracted-expressions stx-expr ...])
#'(begin
(define-syntax extracted-exprs (box '()))
(begin
  (define-syntax macro-id
(lambda (stx)
  (stx-expr #'extracted-exprs)))
  (macro-id))
...)]))
 
 (define (square x) (* x x))
 
 (with-extract-expressions
 (lambda (exprs)
  (with-syntax ([exprs exprs])
#'(module+ test
(require rackunit)
(check-equal? (extract-expression (square 5) exprs) 25)
(check-equal? (extract-expression (square -5) exprs) 25
 (lambda (exprs)
  (with-syntax ([(expr ...) (get-extracted-exprs exprs)])
#'(module+ doc
(require scribble/manual
 scribble/eval)
(define evaluator (make-base-eval))
@defproc[(square [x real?]) real?]{
  Returns the square of @racket[x], the result of
  multiplying @racket[x] by itself.
  @examples[#:eval evaluator expr ...]}
 
 
 On Jul 20, 2015, at 8:03 PM, Jack Firth jackhfi...@gmail.com wrote:
 
 I'm trying to create a way to automatically turn test cases into examples. 
 I'd like a macro that turns this:
 
 (extract-expressions
 (module+ test
 (check-equal? (extract-expression (square 5)) 25)
 (check-equal? (extract-expression (square -5)) 25))
 (module+ doc
 @defproc[(square [x real?]) real?]{
   Returns the square of @racket[x], the result of
   multiplying @racket[x] by itself.
   @examples[#:eval evaluator (include-extracted-expressions)]}))
 
 into this:
 
 (begin
 (module+ test
  (check-equal? (square 5) 25))
 (module+ doc
  @defproc[(square [x real?]) real?]{
Returns the square of @racket[x], the result of
multiplying @racket[x] by itself.
@examples[#:eval evaluator (square 5) (square -5)]}))
 
 That's the general idea anyway. I want to be able to gather arbitrary 
 expressions inside a region and copy them into some other marked place 
 inside the expressions. How exactly do I go about doing this, and how 
 should I do it properly?
 
 -- 
 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.
 
 -- 
 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.
 
 -- 
 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.

-- 
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] Macro introduce identifiers in the new expander

2015-07-21 Thread Matthew Flatt
That one should fail (and the same as with the old expander).

Each `(let () )` starts a new scope, and the identifier introduced
by `(use)` doesn't have the scope for the outer `(let () )` where
`(def)` creates a binding.

At Tue, 21 Jul 2015 19:38:57 +, Spencer Florence wrote:
 That fixed the example I gave, but now this fails:
 
 (let ()
   (def)
   (let ()
 (use)))
 
 On Mon, Jul 20, 2015 at 2:50 PM Matthew Flatt mfl...@cs.utah.edu wrote:
 
  Repair pushed.
 
 
 
   On Jul 20, 2015, at 11:14 AM, Matthew Flatt mfl...@cs.utah.edu wrote:
  
   Thanks for the info. I think it's a bug in the expander, and I have a
   repair, but I think that repair might point to another bug that I'm
   still investigating.
  
   At Mon, 20 Jul 2015 12:56:49 -0400, Alexander D. Knauth wrote:
   I don't really know what's going on, but this might help:
  
   ;; It seems to be introducing the definition correctly, but not the use:
   ;; this works
   (let ()
(def) ; no difference between (def) and (define id 5)
id)
  
   ;; this doesn't
   (let ()
(def)
(use)) ; but there is a difference between (use) and id
  
   ;; and this doesn't either
   (let ()
(define id 5)
(use))
  
   On Jul 20, 2015, at 12:35 PM, Spencer Florence spen...@florence.io
  wrote:
  
   Hello,
  
   I'm trying to update some code to the new expander. The below code
  works on
   6.2 but fails on the new expander with an unbound identifier error.
  
   #lang racket
   (require (for-syntax syntax/parse))
  
   ;; a standard context for identifiers
   (define-for-syntax ctx #'ctx)
  
   ;; create an ID with the context `ctx`, and the current
   ;; expander mark (so that the mark is canceled later),
   ;; and the location loc
   (define-for-syntax (make-id loc)
(syntax-local-introduce
 (datum-syntax ctx 'id loc)))
  
   ;; This introduces a binding
   (define-syntax (def stx)
(syntax-parse stx
  [(def)
   (with-syntax ([id (make-id #'here)])
 #'(define id 5))]))
  
   ;; this attempts to use the binding introduced by `def`
   (define-syntax (use stx)
(syntax-parse stx
  [(use)
   (with-syntax ([id (make-id #'here)])
 #'id)]))
  
   ;; this works
   #;
   (begin
(def)
(use))
  
   ;; this fails
   (let ()
(def)
(use))
  
   --
   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.
  
   --
   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.
  
   --
   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.
  
 
  --
  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.
 
 
 -- 
 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.

-- 
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.