newLISP Fan Club

Forum => Anything else we might add? => Topic started by: BrickCaster on October 29, 2004, 01:06:30 PM

Title: anonymous context-objects
Post by: BrickCaster on October 29, 2004, 01:06:30 PM
i experiment with NewLISP and LEGO bricks, using the LDraw library standard.

i use a newlisp BRICK class, here is an excerpt:

(constant 'red 4)

(context 'BRICK)

(define color MAIN:red)

(define (BRICK:new ctx col x y z)
  (MAIN:new BRICK ctx)
  (set 'ctx (eval ctx))
  (set 'ctx:color col)
  ctx
)

now what i want is to define LEGO models as a list of BRICKs:

(list
  (BRICK:new 'ctx1 ...)
  (BRICK:new 'ctx2 ...)
  (BRICK:new 'ctx3 ...)
  (BRICK:new 'ctx4 ...)
  ...
)

the problem is i want anonymous context-objets, i don't want to name  each brick in my LEGO models, some have 700+ bricks.
Title:
Post by: Lutz on October 29, 2004, 04:00:36 PM
Sorry, there are no anonymous context objects in newLISP, but perhaps you can invent some numbering scheme i.e:



(BRICK:new (symbol (format "brick-%03d" 111)) 'blue) => brick-111



brick-111:color => blue



you could also do:



(map (fn(x) (BRICK:new (symbol (format "brick%03d" x)) 'blue)) (sequence 1 5))



=> (brick001 brick002 brick003 brick004 brick005)



For making several at once, etc.



Lutz
Title:
Post by: Lutz on October 29, 2004, 04:10:35 PM
The you also could do:



(set 'bricks (map (fn(x) (BRICK:new (symbol (format "brick%03d" x)) 'blue)) (sequence 1 5)))
 
 => (brick001 brick002 brick003 brick004 brick005)

(dolist (b bricks)
  (set 'b:color 'black))

brick003:color => black


you can refer to the bricks by variable, etc.



Lutx
Title:
Post by: BrickCaster on October 30, 2004, 10:43:51 AM
thanks for your help Lutz.



unfortunately, the more i experiment with NewLISP the more i discover how much i am biased towards the everything-is-a-first-class-value approach.



so i will probably convert my code to some Scheme or Scheme derivative.

that makes sense because animating LEGO scenes also requires some paralellism that can only be implemented using true continuations.



however, be sure i was really pleased to discover NewLISP and how well you maintain and support it.
Title:
Post by: Lutz on October 30, 2004, 11:21:53 AM
good luck!



Lutz
Title:
Post by: nigelbrown on November 01, 2004, 06:56:53 PM
Regarding anon contexts perhaps a function that will return a random symbol name that is unique within a given (or current) context could be useful viz

(anon) -> gwei842y321rui1dygk1

(anon 'CTXZ) -> gwei842y321rui1dygk1

I've used the numbered symbol generation myself but sometimes

anything will do?



I'm sure a little function could be written for it but why keep inventing it - perhaps a topic for a 5c-tip?

Nigel