Hi Tim,

On 8/1/05, Tim Johnson <[EMAIL PROTECTED]> wrote:
>=20
> * Anton Rolls <[EMAIL PROTECTED]> [050801 01:38]:
> >=20
> > Hi Tim,
> >=20
> > 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.
> >=20
> > What are the changing parts (what value and type are they) ?
> >=20
> > It looks to me like the changing parts are:
> >=20
> > =09- widget style=20
> > =09- widget feel
> >=20
> > where the feel is just used to detect different key presses (?)
> >=20
> > I would love to know the intention of your code so we
> > can refactor it.
> =20
>   <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
>=20

Maybe looking at other languages and showing how to do that in rebol is=20
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! :)
=20
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[
=09me: does[pdp[first self]]
=09metoo: does[pdp[ second self]]
=09methree: does[pdp[ third self]]
=09me 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.
> >=20
> > > # 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] [
=09dispatch: reduce [
=09=09"radio" get in kb-lists 'do_sql_radio
=09=09"text" get in kb-lists 'do_sql_text
=09=09; .. =20
=09]
=09reduce [name select dispatch layout/type]
]

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

layo: [
=09type "radio"
=09; ..
]

pdp [do_sql "name" layo]

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

do_sql: func [name layout /local dispatch word] [
=09dispatch: [
=09=09"radio" do_sql_radio
=09=09"text" do_sql_text
=09=09; .. =20
=09]
=09word: select dispatch layout/type
=09reduce [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] [
=09word: to-word join "do_sql_" name
=09reduce [name get in kb-list word]
]

; i guess python has similar ways to get from a string to a function,=20
; tell me ;)
=20
> 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     =20
> > >     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=20
> > > for this class.'
> >


clazz: context [

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

=09; can be initialized immediate
=09target: 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 [
=09on-click: does["click"]
]
=20
> --=20
> Tim Johnson <[EMAIL PROTECTED]>
>       http://www.alaska-internet-solutions.com
> --=20
> To unsubscribe from the list, just send an email to=20
> lists at rebol.com with unsubscribe as the subject.
>=20
>=20


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