Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Generic Block to Object solution

 [1/3] from: al::bri::xtra::co::nz at: 8-Dec-2000 19:43


Earlier, I wrote:
> Deck: make block! divide length? Cards 3 > foreach [Type Value Number] Cards [
<<quoted lines omitted: 4>>
> ] > ]
That doesn't work. :-( I should test my code first, before misleading people. A generic solution to reading a block of values and creating a block of objects is this: [ Rebol [] Cards: [ [Type Value Number] Gold 0 2 Gold 11 1 Cups 2 4 ] Make-Objects: function [ Table [block!] Prototype! [object!] ][ Prefix Specification Custom Make-Object ][ Prefix: "-_Make-Objects_-" Specification: map first table func [Word [word!]] [ to word! join Prefix Word ] Custom: map first Table func [Word [word!]] [ reduce [ to set-word! Word to word! join Prefix Word ] ] Make-Object: function Specification [Object] [ Object: make Prototype! bind Custom 'Object ] map next Table :Make-Object ] Prototype: make object! [ f: function [test] [block] [ block: [] ; Don't do this at home! append block test probe block ] Type: lit-word! Value: integer! Number: integer! ] probe Deck: Make-Objects Cards Prototype halt ] And here's the result in Rebol console: [ make object! [ f: func [test /local block][ block: [] append block test probe block ] Type: 'Gold Value: 0 Number: 2 ] make object! [ f: func [test /local block][ block: [] append block test probe block ] Type: 'Gold Value: 11 Number: 1 ] make object! [ f: func [test /local block][ block: [] append block test probe block ] Type: 'Cups Value: 2 Number: 4 ]] There's one problem with the above code that I think is bad: Make-Object: function Specification [Object] [ Object: make Prototype! bind Custom 'Object ] Note that I 'bind Custom to 'Object. This is analogous to 'bind with 'self in objects. Does anyone know of a better way to do this? Andrew Martin ICQ: 26227169 http://members.nbci.com/AndrewMartin/

 [2/3] from: g:santilli:tiscalinet:it at: 8-Dec-2000 11:25


Hello Andrew! On 08-Dic-00, you wrote: AM> Earlier, I wrote: AM>> Deck: make block! divide length? Cards 3 AM>> foreach [Type Value Number] Cards [ AM>> insert tail Deck make object! reduce [ AM>> 'Type: Type AM>> 'Value: Value AM>> 'Number: Number AM>> ] AM>> ] AM> That doesn't work. :-( I should test my code first, before AM> misleading people. What about: foreach [type value number] cards [ insert tail deck make object! compose [ type: (type) value: (value) number: (number) ] ] or: foreach [t v n] cards [ insert tail deck make object! [ type: t value: v number: n ] ] For your Make-Objects: Make-Objects: func [ Table [block!] Prototype! [object!] /local Specification Result Object ] [ Specification: first Table Result: make block! to-integer divide length? next Table length? Specification foreach :Specification next Table compose/deep [ Object: make Prototype! [] set bind [(Specification)] in Object 'self reduce [(Specification)] insert tail Result Object ] Result ]
>> print mold Make-Objects Cards Prototype
[ make object! [ Type: 'Gold Value: 0 Number: 2 Something-else: 10 ] make object! [ Type: 'Gold Value: 11 Number: 1 Something-else: 10 ] make object! [ Type: 'Cups Value: 2 Number: 4 Something-else: 10 ]] Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [3/3] from: al:bri:xtra at: 9-Dec-2000 9:17


> Make-Objects: func [ > Table [block!]
<<quoted lines omitted: 3>>
> Specification: first Table > Result: make block! to-integer divide length? next Table length?
Specification
> foreach :Specification next Table compose/deep [ > Object: make Prototype! []
<<quoted lines omitted: 3>>
> Result > ]
Thank you, Gabriele! Now I've got to work out how to do the reverse, and implement a form of inheritance in the objects. Andrew Martin ICQ: 26227169 http://members.nbci.com/AndrewMartin/

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted