[REBOL] Re: Binding problem with 'do
From: tim:johnsons-web at: 1-Aug-2005 14:22
Volker:
The entire text of your email is very had to read. My mailer is
receiving it full of equal signs and number combination and is
practically speaking, obfuscating your code. What's up? Is this your
mailer, or my mailer or is it the mailing list?
I see this a lot especially in this ML.
I know that you have much good code to share, but
I just can't see it for all of the weird text.
Sorry
Tim
* Volker Nitsch <[volker--nitsch--gmail--com]> [050801 12:15]:
> Hi Tim,
>
> On 8/1/05, Tim Johnson <[tim--johnsons-web--com]> wrote:
> >
> > * Anton Rolls <[antonr--lexicon--net]> [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:
> > >
> > > =09- widget style
> > > =09- 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[
> =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.
> > >
> > > > # Example: explicit
> > > > def do_sql(name,layout):
> > > > dispatch = {
> > > > '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; ..
> =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; ..
> =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,
> ; 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='', text='', **kw):
> > > > self.target = None
> > > > self.onClick = None
> > > > self.onMouseOver = None
> > > > self.onMouseOut = None
> > > > self.url = url
> > > > self.text = 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] = kw[item]
> > > > else:
> > > > raise KeyError, `item`+' not a valid parameter
> > > > 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
> =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]
> =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"]
> ]
>
> > --
> > Tim Johnson <[tim--johnsons-web--com]>
> > 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
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.alaska-internet-solutions.com