2013-08-21 12 views
5

üzerinden nesne yapmak: ...Interleave engellediği veya iki blok (alan adları ve değerleri) yerine yazılı tarafından nesneleri oluşturma

obj: object [ 
    name: "Fork" 
    id: 1020 
] 

Ben böyle bir şey yazmak istiyorum

obj: something-or-another [name id] ["Fork" 1020] 

... ve aynı sonucu elde edin.

obj: something-or-another [name: id:] ["Fork" 1020] 

yeterince kolay bir something-or-another yazmak için, ancak bu uygun bir şey "kutusuna" zaten yapar: İdeal bir çözüm ayrıca izin verecek?

cevap

3

Bunu yapmak için pişmiş bir yol olduğuna inanmıyorum. Değil zor olsa: herhalde

func [words values][ 
    set words: context append map-each word words [to set-word! word] none values 
    words 
] 

biraz bu yıkmak olabilir:

func [ 
    "Creates an Object From a Block of Words, Assigns Values" 
    words [block!] "Words used to create object" 
    values "Value(s) to assign" 
][ 
    words: map-each word words [to set-word! word] ; The requisite set-words 
    append words none ; Initial value for all words 
    words: context words ; Create our object 
    set words values ; Assigns our value(s) to the object 
    words ; returns the object 
] 

Sen gibi blokların, serpiştirmek için farklı bir yöntem kullanır olabilir: Yazdığım

func [words [block!] values [block!]][ 
    collect [ 
     repeat index max length? words length? values [ 
      keep words/:index 
      keep values/:index 
     ] 
    ] 
] 
+0

Kod örneklerinizden birincisi (işlevin bir isme ihtiyacı olabilir) kısa ve temiz ve bana göre en iyi şekilde görünüyor. – Ladislav

1

Benzer bir işlev (Rebol2) sadece birkaç gün önce:

build-object: func [ 
    "Builds an object from a block" 
    names [block!] "Field names" 
    /values val [block!] "Initial values" 
    /local o name value 
] [ 
    o: copy [] 
    names: compose names 
    o: either values [ 
     parse names [ 
      some [ 
       set name [word! | set-word!] 
       (append o to-set-word name) 
       | skip 
      ] 
     ] 
     set/pad reflect o: context append o none 'words val 
     o 
    ] [ 
     if any [ 
      parse names [ 
       some [ 
        set name [word! | set-word!] 
        (append o reduce [to-set-word name none]) 
       ] 
      ] 
      parse names [ 
       (clear o) some [ 
        set name [word! | set-word!] set value any-type! 
        (append o reduce [to-set-word name :value]) 
       ] 
      ] 
     ] [context o] 
    ] 
    o 
] 

Eğer herhangi yazabilirsiniz senin nesneyi oluşturmak için: (f: does ["x"] olarak bir işlev oluşturmak)

  • build-object [name "Fork" id 1020]
  • build-object [name: "Fork" id: 1020]
  • build-object/values [name id] ["Fork" 1020]
  • build-object/values [name: id:] ["Fork" 1020]
  • build-object [name f]
  • build-object [name (f)] ;block is composed
  • build-object [name 5 id f]
  • build-object [name 5 id 'f]

, ör değerleri dışarıda bırakın durumunda da none olarak ayarlanmış alanlara sahip nesneler yapabilir Ayrıca tanımsız değerler ayarlayarak izin vermek isterseniz

func [ 
    "Create an object based on some words and values." 
    words [any-word! block!] "Word or block of words" 
    values [any-type!] "Value or block of values" 
    /local object 
][ 
    object: make object! either block? words [length? words] [1] 
    set bind/copy/new :words object :values 
    object 
] 

, bu deneyin:

func [ 
    "Create an object based on some words and values." 
    words [any-word! block!] "Word or block of words" 
    values [any-type!] "Value or block of values" 
    /any "Allows setting words to any value, including unset" 
    /local object 
][ 
    object: make object! either block? words [length? words] [1] 
    apply :set [bind/copy/new :words object :values any] 
    object 
] 

Bunların her ikisi de nesneleri oluşturmak

build-object [name id] 
+0

Kısa bir süre içinde birden çok kez ortaya çıkması ilginç! Belki sık sık ortaya çıktığını gösteren bir işaret ve burada bir şey standartlaştırılmalıdır."Değer-kuralı" nı, "herhangi bir türden" bir "fark" ın tersi gibi ifade etmenin bir nedeni var mı? – HostileFork

+0

Özel ihtiyacım içindi. Ama sen haklısın, herhangi bir tip kullanıyorsun! yararlı olabilir. – endo64

+0

Herhangi bir tür ve bazı küçük düzeltmeler kullanarak başka bir sürüm yazdım, önceki cevabımı düzenlediğimde veya yeni bir tane eklemem gerekir mi? – endo64

2

ile Burada gerektiren bir şey en azından Rebol'un 3 var self, dolayısıyla self olmadan bir nesne oluşturmak istiyorsanız, bazı meraklı hileleri yapmanız gerekir. Detaylar için bkz. the selfless proposal.

İlgili konular