Skip to content

Question about contibuting code #135

@gambiteer

Description

@gambiteer

I'm talking to a student starting to use LambdaNative to do some GUI stuff, and he started with Conway's Game of Life, which has a lot of boxes (either black or white) and he noticed that his program was going quite slowly.

While investigating, I think I found some quadratic behaviour (funny, my browser just marked "behavior" as a spelling mistake) in glgui-widget-add and glgui-widget-delete. That's probably not the reason the student's program is slow, but it's something.

So, a number of questions:

Are you looking for people to suggest changes to the code?

If so, do you have a required coding style?

And is there any way to discuss the code outside of starting a new issue?

Brad

For these routines I would suggest

(define (glgui-widget-add g . args)
  ;; Add a new widget to the end of g's widget-list
  (let* ((tmplist
          (let loop ((a args)  ;; collect pairs of args into an association list
                     (r '()))  ;; in reverse order (order doesn't matter in list->gtable)
            (cond ((null? a)
                   r)
                  ((null? (cdr a))
                   (error "glgui-widget-add: arguments must come in pairs: " args))
                  (else
                   (loop (cddr a)
                         (cons (cons (car a) (cadr a))
                               r))))))
         (entry
          (list->gtable tmplist)))
    (let ((widget-list (glgui-get g 'widget-list '())))
      (glgui-set! g 'widget-list (append widget-list (list entry))))
    (let ((widget-count (glgui-get g 'widget-count 0)))
      (glgui-set! g 'widget-count (fx+ widget-count 1)))
    entry
  ))

(define (glgui-widget-delete g w)
  ;; remove all instances of w from g's widget-list
  (let ((l (glgui-get g 'widget-list))
        (c (glgui-get g 'widget-count)))
    (glgui-set! g 'widget-list
                (let remove-w ((ws l))   ;; remove w while maintaining list order
                  (cond ((null? ws)
                         ws)
                        ((equal? (car ws) w)
                         (remove-w (cdr ws)))
                        (else
                         (cons (car ws)
                               (remove-w (cdr ws)))))))
    (glgui-set! g 'widget-count (fx- c 1))
  ))

Tested lightly with the following interaction inside a REPL in DemoRedSquare:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
----
LambdaNative REPL
DemoRedSquare 1.0
Built 2016-10-14 15:34:15 (00h 01m 32s ago)
> gui
#<table #2>
> (glgui-get gui 'widget-list)
(#<table #3>)
> (glgui-box gui 0 0 10 10 Blue)
#<table #4>
> (glgui-get gui 'widget-list)
(#<table #3> #<table #4>)
> (glgui-box gui 10 10 10 10 Green)
#<table #5>
> (glgui-get gui 'widget-list)
(#<table #3> #<table #4> #<table #5>)
> (glgui-widget-delete gui #4)
> (glgui-get gui 'widget-list)
(#<table #3> #<table #5>)
> 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions