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

World: r3wp

[!REBOL3-OLD1]

Jerry
11-Nov-2009
[19619]
num-of-parameters: funct [ f [ any-function! ] ] [
    clear find spec: spec-of :f /local

    remove-each element spec [ any [ string? element block? element ] 
    ] 
    length? spec
]
Pekr
11-Nov-2009
[19620]
Important - Finalizing read and write - http://www.rebol.net/r3blogs/0294.html
PeterWood
11-Nov-2009
[19621x3]
Jerry - you can use words-of instead of spec-of. It doesn't return 
the comments.


>> a: func [b "comment" c "comment"  /local d] []                
        
 
>> b: func [c d e f /local z] []

 >> n-o-p: func [f] [length? copy/part spec: words-of :f find spec 
 /local]
 
>> n-o-p :a
 
== 2


>> n-o-p :b
 
== 4


Sory for the cryptic code. I had to keep it to a single line in the 
R3 console.
The code above doesn't handle 0 parameters though :-(
... and more importantly it doesn't handle refinements either.


What would you expect num-of-parameters to return for the following 
function:

a: func [b c /d e /f g /local x y z]
Jerry
12-Nov-2009
[19624]
clear find spec: spec-of :f /local     ... can move the refinements 
part
PeterWood
12-Nov-2009
[19625]
Here is a version using parse:


>> n-o-p: func [f] [ct: 0 parse words-of f [some [word! (++ ct) | 
refinement! thru end]] ct]
 >> n-o-p :now
 == 0


>> a: func [b c /local e] []
>> n-o-p :a
 == 2
>> x: func [b c /d e /f g /local x y z] [
 
>> n-o-p :x                                

== 2
Geomol
12-Nov-2009
[19626x4]
A version without parse:

>> npars: func [f /local w] [
	w: words-of :f
	length? copy/part w any [find w refinement! tail w]
]
>> npars :print
== 1
>> npars :now
== 0
>> npars :insert
== 2
Some thought of naming. How should such a function be named the REBOL 
way?

number-of-parameters seems a bit long. I sometimes write functions, 
that return the number-of something. I come to think of # as being 
number-of, but # in the start mean an issue! datatype. What about 
allowing # in the end of words? Like:

parameters#: func [ ....

Or is that too ugly?
A bit like we have ? in the end of many words: length? tail? none? 
...
Maybe not pluralis, but just
parameter#: func [ ...
Izkata
12-Nov-2009
[19630]
num-args, num-of-args ?
Henrik
12-Nov-2009
[19631]
args-of
BrianH
12-Nov-2009
[19632]
Remember that there is nothing special about the /local refinement 
or the words that follow it. All /local is is an option that you 
aren't using, and in that is no different than any other option you 
aren't using. In order to determine the number of args that a function 
takes, you need to specify which options you will be using in the 
call, perhaps by providing a path! instead of a function reference. 
Otherwise, just use WORDS-OF.
Jerry
12-Nov-2009
[19633]
Thanks you guys, very helpful.
PeterWood
13-Nov-2009
[19634]
John's non-parse version looks to be the fastest on 10,000 iterations 
with :print, :now and :insert :

Jerry's original took 0:00:00.346343

John's version took   0:00:00.06549

Parse version took    0:00:00.129942
BrianH
13-Nov-2009
[19635x3]
Try this one (words-of already copies, no need to again):
npars: func [f [any-function!] /local w] [
	length? also w: words-of :f clear find w refinement!
]
Version without also:
npars: func [f [any-function!] /local w] [
	clear find w: words-of :f refinement!
	length? w
]
CLEAR and REMOVE of none just return none and don't complain. This 
allows chaining without needing conditional code.
PeterWood
13-Nov-2009
[19638]
Both very fast - no real difference between them:

Jerry's original took             0:00:00.355178

John's version took             0:00:00.061354

Parse version took              0:00:00.129759

Brian's 1st version took      0:00:00.056886
Brian's 2nd version too k    0:00:00.051447
BrianH
13-Nov-2009
[19639x2]
The type test in the argument list might be taking a little time. 
Try John's with the type spec. I'm curious to see what the difference 
is.
I mean the [any-function!] part.
Pavel
13-Nov-2009
[19641x2]
Is struct! working in actual version? If so some example would be 
handy!
To Brian I've read your description between FUNC and FUNCT in DevChat. 
Never seen such summarizing description anywhere, but I think it 
is very usefull not for beginners only but for everybody not so hawkeyed 
as gurus. This should be mentioned in docs, or even better short 
lectures shall be written about such "deep lake" details (to reproduce 
Carls definition)
PeterWood
13-Nov-2009
[19643]
Adding the type spec didn't make any significant difference.
Pekr
13-Nov-2009
[19644]
I think that struct! and routine! are there left-overs from R2, and 
will be removed, as we are not going to get DLL interface, I wonder 
what those two datatypes would be good for. Maybe struct! might be 
usefull, if made more powerfull, dunno. Our interface is now Extensions.
Pavel
13-Nov-2009
[19645]
Yes Pekr and when you want to write Extension as generalDLL loader 
can you use a block! paramerer instead of struct, or better how to 
transform a block given as parameter of extension to struct needed 
as parameter of underlaying DLL.
BrianH
13-Nov-2009
[19646x3]
Good to know, Peter, thanks. Type checking was sped up in R3 through 
typesets.
The R2-style struct! and routine! types are not working in R3 and 
are likely to be removed. The struct! type might be replaced with 
a new, improved, incompatible struct! type. The routine! type has 
already been replaced with a new, improved, incompatible command! 
type.
You are right that documentation is an ongoing problem. The design 
has been changing a lot during the alpha phase, but the behavior 
of FUNC and FUNCT are unlikely to change, so they could be documented.
Geomol
14-Nov-2009
[19649]
I added a ticket to curecode about the math performance issue: Ticket 
#0001338
BrianH
14-Nov-2009
[19650x3]
Have you tested with prefix form math? The implementation of op! 
has changed, and the new implementation would probably be slower 
(at a guess) but is more flexible. Please test with prefix math so 
we can categorize the ticket properly.
If functions like ADD and SUBTRACT are slower, this is an issue. 
If it is just ops like + and - then it is a side effect of user-defined 
op!.
The new op! behavior has allowed us to speed up DO quite a bit overall. 
It won't be changed. If you want fast math, use prefix functions 
or better yet extensions.
Geomol
14-Nov-2009
[19653]
I tested under OS X with prefix math, and the same picture is seen. 
If it's because R3 isn't compiled for speed, then that might be the 
answer, so this isn't an issue.
BrianH
14-Nov-2009
[19654x2]
Oh, duh, it's because integers are 64bit in R3, and Windows' 64bit 
integer math emulation is better than Mac's. Fixable :)
(still guessing though)
Geomol
14-Nov-2009
[19656]
I was testing floating point math.
BrianH
14-Nov-2009
[19657]
Another guess failed then. Back to the "hasn't been optimized enough 
yet" theory.
Pavel
14-Nov-2009
[19658x3]
1. TCP question: where to get complete list of event types? lookup, 
connect , wrote, read, close .... what else, does exists the complete 
list? 
2. Is it possible to get other devices event types
3. is it possible to define "own" events?
Whwhere are the events defined?
Are the device events the same as GUI-events (meaning the machanism 
is the same or different)
Maxim
14-Nov-2009
[19661x3]
AFAIK, that will all be revealed within the host code shake down 
 :-)


devices are defined in the host and will be extensible, eventually. 
 they all use the same low-level/high-level api using fully async 
handlers.
geomol, AFAIK, Carl doesn't use the same compilers on different plaforms, 
so that will invariably produce different results for the same C 
code.
I've read often that intel optimisations in compilers make some C 
code faster than assembly because they'll use special shortcuts in 
the cpu when they discover specific coding patterns.
PeterWood
14-Nov-2009
[19664]
If functions like ADD and SUBTRACT are slower

 - I'm not sure whether you mean are slower under Mac OS X  or slower 
 than + and -. ADD, SUBTRACT etc. are much slower than +.-.

Results on an old 1ghz ThinkPad:

 >> a: 1. b: 2. dt [loop 10000000 [divide multiply add a b a b]]
== 0:00:21.5

>> a: 1. b: 2. dt [loop 10000000 [a + b * a / b]]
== 0:00:17.25

>> a: 1. b: 2. dt [loop 10000000 [divide multiply add a b a b]]
== 0:00:20.625

>> a: 1. b: 2. dt [loop 10000000 [a + b * a / b]]
== 0:00:17
BrianH
14-Nov-2009
[19665x2]
We mean slower on R3 than they are on R2. We need comparisons of 
both inline and prefix forms if we are going to optimize R3.
And we need platform-specific data too. It doesn't make sense to 
compare Windows and OSX, but it does make sense to compare R2 and 
R3 with each other on the same computer and OS. For that matter, 
there may be OSX version issues too.
PeterWood
14-Nov-2009
[19667]
The prefix forms in R3 are only about 20% slower than the inline 
forms in Mac OS X whereas they are over 50% slower in R2:

R2 results:
>> a: 1. b: 2. dt [loop 10000000 [divide multiply add a b a b]]

== 0:00:06.096334
>> a: 1. b: 2. dt [loop 10000000 [a + b * a / b]]              

== 0:00:03.679331

R3
>> a: 1. b: 2. dt [loop 10000000 [divide multiply add a b a b]]

== 0:00:06.72179

  >> a: 1. b: 2. dt [loop 10000000 [a + b * a / b]]                
    

== 0:00:05.521236
BrianH
14-Nov-2009
[19668]
Interesting... It looks like the op! changes weren't as bad for overhead 
as I thought :)