r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Ammon
30-Mar-2005
[851x2]
>> one-word: func [block /local val][val: copy [] foreach word block 
[append val get word]val]
>> one-word [foo bar]
== [a [does this] b [does that]]
;~>
Chris
30-Mar-2005
[853]
I should have specified built-in...
Ammon
30-Mar-2005
[854x2]
Hehe, I expected that. ;~>
Although, I don't understand why.  It is a one-line solution so I 
don't see why it should make a difference if it is built in or not...
Chris
30-Mar-2005
[856x2]
I was thinking (name pending) --

one-word: func [blk][head while [not tail? blk][blk: change/part 
blk get blk/1 1]]
I'm not partial to creating new values when I don't have to :^)
Ammon
30-Mar-2005
[858x3]
I think I like that better.
I think that this functionality should be added to GET
There are quite a few words in REBOL that, IMHO, should have automatic 
block handling.
Chris
30-Mar-2005
[861]
Hmm, and I was thinking of a refinement to 'reduce.  'Get makes more 
sense...
[unknown: 10]
30-Mar-2005
[862]
... Is there a quick trick to compare 2 pairs??  Seems 'lesser? or 
greater? dont work on pairs...
Ammon
30-Mar-2005
[863]
Tell me, is 10x0 or 0x10 greater?
[unknown: 10]
30-Mar-2005
[864x2]
from my point of view its equal ;-)
Thansk for the wakeup call ;-)
Brock
30-Mar-2005
[866]
Are you thinking about this?
>> data: 10x0
== 10x0
>> data/x
== 10
>> data/y
== 0
>>
Ammon
30-Mar-2005
[867]
I'd just compare X and Y values separately...
[unknown: 10]
30-Mar-2005
[868x2]
perhpas yes...
Yes thats what i do currently...
Chris
30-Mar-2005
[870]
Are you trying to compare area?  -- greater-pair?: func [p1 p2 /local 
ps][ps: reduce [p1 p2] pick ps (p1/x * p1/y) > (p2/x * p2/y)] -- 
which can be tweaked for when p1 and p2 are equal...
[unknown: 10]
30-Mar-2005
[871]
Thanks Chris... i worked it out by splitting the pair into parts...
Ladislav
31-Mar-2005
[872x2]
Chris:

>> foo: [a [does this]]
== [a [does this]]
>>  bar: [b [does that]]
== [b [does that]]
>> foobar: compose [(foo) (bar)]
== [a [does this] b [does that]]
ah, it's there already
Izkata
31-Mar-2005
[874]
Yes it is - but he wants [foo bar] to become  [a [does this] b [does 
that]] without the () or anything else inside..
Anton
31-Mar-2005
[875]
I think the answer is: it's not in current rebol.
Chris
31-Mar-2005
[876]
Yep, I'm resigned to that.  (and I'll word my queries a little better 
next time :^)
Gregg
1-Apr-2005
[877]
http://www.rebolforces.com/articles/pairs/
[unknown: 10]
1-Apr-2005
[878]
A nice one...
Graham
5-Apr-2005
[879x2]
how does one find out the currrent secure level?
secure query
eFishAnt
5-Apr-2005
[881]
a nice 1x1...
Graham
5-Apr-2005
[882]
writing my own faq
JaimeVargas
7-Apr-2005
[883x4]
I hope this is useful for someone

REBOL []

rest: func [s [series!]][skip s 1]

define-object: func [
	spec [block!] 
	/local 

  arg-spec ctx-spec object-name constructor-name predicate-name attributes
		spec-rule type-spec continue? w
][
	arg-names: copy []

	continue?: [none] ;used to stop parsing
	name-rule: [set w word! (insert tail arg-names w)]

 type-rule: [set w word! (unless datatype? attempt [get w] [continue?: 
 [end skip]])]

 spec-rule: [name-rule some [name-rule opt [into [some [type-rule 
 continue?]]]]]

	if any [
		not parse spec spec-rule
		arg-names <> unique arg-names
	][
		make error! "invalid spec"
	]

    object-name: to-string first arg-names
	constructor-name: to-word join 'make- object-name
	predicate-name: to-word join first arg-names '?
	attributes: rest arg-names

	arg-spec: copy []
	foreach itm attributes [
		insert tail arg-spec reduce [
			to-word join itm '-value
			either block? w: select spec itm [w][[any-type!]]
		]
	]

	ctx-spec: copy []
	arg-names: extract arg-spec 2 1
	repeat i length? attributes [

  insert tail ctx-spec reduce [to-set-word attributes/:i to-get-word 
  arg-names/:i]
	]

	;create constructor function
	set constructor-name make function! 

  compose [(reform ["Makes a new" uppercase object-name "object with 
  attributes" mold attributes]) (arg-spec)]
		compose/only [make object! (ctx-spec)] ;body

	;create predicate function
	set predicate-name make function! 

  compose [(reform ["Determines if value is a" uppercase object-name 
  "object"]) value [object!] /local types]
		compose/deep/only [
			either (attributes) = rest first value [
				foreach itm (attributes) [
					unless any [

      [any-type!] = types: select (arg-spec) to-word join itm '-value
						find types type?/word value/:itm
					][return false]
				]
				true
			][
				false
			]
		] 
]
With it you can create objects that are type checked when constructed.
;; usage
define-object [name attribute1 [datatype1 ...] ...]
;; creates two functions
make-name
name?
;; example untyped
define-object [point x y]
point? probe o: make-point 1 2
point? probe o: context [x: 1 y: "two"]

;; example typed
define-object [point x [integer!] y [integer!]]
point? probe o: make-point 1 2
point? probe o: context [x: 1 y: "two"]
Ammon
7-Apr-2005
[887]
Jaime, your REST function (AFAICT) has the same functionality as 
NEXT is there any reason that you are not using NEXT?
JaimeVargas
7-Apr-2005
[888x2]
Didn't knew about next. I will use it from now on.
Do you find define-object useful?
Ammon
7-Apr-2005
[890]
I can see how it could be useful in the right environment.
JaimeVargas
7-Apr-2005
[891x3]
;-)
If anyone ever wanted multi-methods or function overload in rebol 
here is the answer. Enjoy ;-)

REBOL []

define-method: func [
	'name [word!] spec [block!] locals [block!] code [block!]

 /local w type-rule spec-rule continue? register-name methods-name
][
	;; first validate the spec
	continue?: [none] ;used to stop parsing

 type-rule: [set w word! (unless datatype? attempt [get w] [continue?: 
 [end skip]])]
	spec-rule: [some [word! into [type-rule continue?]]]
    unless parse spec spec-rule [make error! "invalid spec"]

	register-name: to-word join :name '-register
	methods-name: to-word join :name '-methods?
	unless value? name [
		
		context [
			dispatch-table: copy []
			
			spec-fingerprint: func [spec [block!] /local types][
				types: copy []
				foreach itm extract/index spec 2 2 [insert tail types itm/1 ]
				types
			]
			
			values-fingerprint: func [values [block!] /local types][
				types: copy []
				foreach v values [insert tail types type?/word v]
				types
			]
			

   retrieve-func: func [values [block!]][select/only dispatch-table 
   values-fingerprint values]
			
			set :name func [values [block!]][
				do compose [(retrieve-func values) (values)]
			]
			
			set :register-name func [spec code /local fingerprint pos][
				fingerprint: spec-fingerprint spec
				either found? pos: find/only dispatch-table fingerprint [
					poke dispatch-table 1 + index? pos function spec locals code
				][

     insert tail dispatch-table reduce [fingerprint function spec locals 
     code]
				]
			]
			
			set :methods-name does [probe dispatch-table]
		]
	]

	do reduce [register-name spec code]
]

define-method f [x [integer!]] [] [x + 1]
define-method f [s [block!]] [] [attempt [pick s 2]]
define-method f [x [decimal!]] [] [sine x] 

f[5] == 6
f[[one two three]] == two
f[90.0] == 1.0
Are the above functions DEFINE-METHOD and DEFINE-OBJECT worthy for 
the rebol.org library?
Gabriele
7-Apr-2005
[894x2]
Well, why not. :) I'd suggest you to upload them.
Note that I have something vaguely similar to both in my YourValues 
code (http://www.colellachiara.com/soft/YourValues/), though that's 
probably not general enough for your needs.
JaimeVargas
7-Apr-2005
[896x2]
Making a few modifications two define-method it will be possible 
to have multiple inheritance and object based dispatch in rebol. 
I need to think for a good name to post this in rebol.org.
Rebol never ends to amaze me. The src code for both functions combined 
is under 120 lines including comments and tests.
Gregg
8-Apr-2005
[898]
Please do put them on REBOL.org! No bookmarks in AltME yet, and others 
will find them there.  Wish I had time to play with them right now!
Sunanda
8-Apr-2005
[899]
Exactly the sort of useful utility the Library exists for.
And, as Gregg says, much more likely to be found by others there.
JaimeVargas
11-Apr-2005
[900]
I had been teaching a few more tricks to DEFINE-METHOD. This is what 
it is capable off: