---------- Forwarded message ----------
From: Volker Nitsch <[EMAIL PROTECTED]>
Date: Aug 2, 2005 1:19 AM
Subject: test
To: ich <[EMAIL PROTECTED]>


Hi Tim,

On 8/1/05, Tim Johnson <[EMAIL PROTECTED]> wrote:
>
> * Anton Rolls <[EMAIL PROTECTED]> [050801 01:38]:
> >
> > Hi Tim,
> >
> > Your code below seems to be generating code using a macro.
> > It seems extremely unwieldy to me and needs a clever
> > bit of rebol to shrink it to its essence.
> >
> > What are the changing parts (what value and type are they) ?
> >
> > It looks to me like the changing parts are:
> >
> >     - widget style
> >     - widget feel
> >
> > where the feel is just used to detect different key presses (?)
> >
> > I would love to know the intention of your code so we
> > can refactor it.
>
>   <grin> We just did. We refactored it in rebol. Yes. python
>          is cumbersome, but it scales well, and some
>          customers want it. Even the code below could be
>          factored more. FI: dispatch should be a method
>          of kblists...
>
>          tim
>

Maybe looking at other languages and showing how to do that in rebol is
not to bad for bilinguists. After all rebol can do pretty conventional oops=
.
And i would learn what
   return '  %s %s,' % (name,dispatch[layout['type']])
does! :)

So lets enter a lesson of Dr Rebols tutorial

SQUEEZING CODE LIKE A PYTHON!

Ok, first lets enter the area of rebol-psychology.
Did you know rebol has a first self and a second self?
And when getting older, even got a third? see:

pdp: func[dump][print mold/all do ?? dump]
proof: context[
        me: does[pdp[first self]]
        metoo: does[pdp[ second self]]
        methree: does[pdp[ third self]]
        me metoo methree
]

--output:

dump: [first self]
[self me metoo methree]
dump: [second self]
[
    #[object! [
        me: #[function! [][pdp [first self]]]
        metoo: #[function! [][pdp [second self]]]
        methree: #[function! [][pdp [third self]]]
    ]] #[function! [][pdp [first self]]] #[function! [][pdp [second
self]]] #[function! [][pdp [third self]]]]
dump: [third self]
[me: #[function! [][pdp [first self]]] metoo: #[function! [][pdp
[second self]]] methree: #[function! [][pdp [third self]]]]

(Exercise: figure out what these selfs are ;)

> > Anton.
> >
> > > # Example: explicit
> > >  def do_sql(name,layout):
> > >       dispatch =3D {
> > >           'radio':kbLists.do_sql_radio(),
> > >           'text':kbLists.do_sql_text(),
> > >           'textarea':kbLists.do_sql_textarea(),
> > >           'decimal':kbLists.do_sql_decimal(),
> > >           'uinteger':kbLists.do_sql_uinteger(),
> > >           'select':kbLists.do_sql_select(),
> > >           'date':kbLists.do_sql_date(),
> > >           'checkbox':kbLists.do_sql_checkbox(),
> > >           'password':kbLists.do_sql_text(),
> > >           }
> > >       return '  %s %s,' % (name,dispatch[layout['type']])
>

i hope i get it:

pdp: func [dump] [print mold/all do ?? dump]

; python can easily put kbLists.do_sql_radio() in a list.
; rebol likes to buck when getting functions from pathes.
; so i resort to using functions, more ugly, other way shown later.
; i want to stay close to python-code.

do_sql: func [name layout /local dispatch] [
        dispatch: reduce [
                "radio" get in kb-lists 'do_sql_radio
                "text" get in kb-lists 'do_sql_text
                ; ..
        ]
        reduce [name select dispatch layout/type]
]

kb-lists: context [
        do_sql_radio: does [#radio]
        do_sql_text: does [#text]
        ; ..
]

layo: [
        type "radio"
        ; ..
]

pdp [do_sql "name" layo]

; if the dispatchees are always from lb-lists, we can do

do_sql: func [name layout /local dispatch word] [
        dispatch: [
                "radio" do_sql_radio
                "text" do_sql_text
                ; ..
        ]
        word: select dispatch layout/type
        reduce [name get in kb-list word]
]

; and if the words match to the names, like "radio" -> "do_sql_radio"

do_sql: func [name layout /local dispatch word] [
        word: to-word join "do_sql_" name
        reduce [name get in kb-list word]
]

; i guess python has similar ways to get from a string to a function,
; tell me ;)

> to elaborate: python automagically builds in internal dictionary
>   with the declared class members. Among other things, it allows
>   testing for validity of keyword values...
>   thus the 'for item in keys' test loop.
>

See rebol-psychology above ;)

> > > # Example: using builtin _dict_ in class constructor
> > >     def __init__(self, url=3D'', text=3D'', **kw):
> > >         self.target =3D None
> > >         self.onClick =3D None
> > >         self.onMouseOver =3D None
> > >         self.onMouseOut =3D None
> > >         self.url =3D url
> > >         self.text =3D text
> > >         # target ... text constitutue self.__dict__ when they are
> > >         # preceded by 'self.'
> > >         for item in kw.keys():
> > >             if self.__dict__.has_key(item):
> > >                 self.__dict__[item] =3D kw[item]
> > >             else:
> > >                 raise KeyError, `item`+' not a valid parameter
> > > for this class.'
> >


clazz: context [

        ;     extra 'demo^init to allow super-call, we are pythonizing
        init: demo^init: func [kw /url url* /text text*] [
                foreach item next first kw ; the "keys", skip 'self
                [
                        either in self item [
                                self/:item: get in kw item
                                ; if it can be functions, else kw/:item is =
ok
                        ] [
                                make error! rejoin [
                                        "KeyError, " item " not a
valid parameter for this class."
                                ]
                        ]
                ]
                self
        ]

        ; can be initialized immediate
        target: on-click: on-mouse-over: on-mouse-out: none

]
demo: make clazz []
probe demo/init context [on-click: does["click"]]
probe demo/init context [on-magic: does["magic"]]

; in this simple case we would use the inbuild stuff of course:

demo: make clazz [
        on-click: does["click"]
]

> --
> Tim Johnson <[EMAIL PROTECTED]>
>       http://www.alaska-internet-solutions.com
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
>
>


--
-Volker

"Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem." David
Wheeler


--=20
-Volker

"Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem." David
Wheeler
-- 
To unsubscribe from the list, just send an email to 
lists at rebol.com with unsubscribe as the subject.

Reply via email to