Jensen's Device; RosettaCode and Interpret

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Jensen's Device; RosettaCode and Interpret

rvjansen
I am in the process of adding some examples to the codebase (currently adding them in my sandbox in svn) following the http://rosettacode.org approach; if you did not see this site yet, it is worth to take a look. After a few straightforward tasks I stumbled upon Jensen's device, a forerunner of the Knuth Man-or-boy test for compilers (Algol-compilers, that is). The Rexx example for this has an 'interpret' in it, ang goes like this:

/*REXX program to demonstrate Jensen's device  (call sub, arg by name). */
numeric digits 50 /*might as well get some accuracy.*/
say sum( 'i', '1', '100', '1/i' ) /*invoke SUM (100th harmonic num.)*/
exit
 
sum: procedure; parse arg i,start,finish,term; sum=0
interpret 'do' i'='start 'to' finish';sum=sum+'term';end'
return sum
Apart from usefulness, which I am not going to discuss here (have a look at http://rosettacode.org/wiki/Jensen%27s_Device for the explanation), I am wondering how we can approach this.

I have a working NetRexx version, in the sense that it produces the right answer, but it does not do the required call-by-name.

say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/

  method sum(i,start,finish,term) static
    sum = Rexx 0
    loop i=start to finish
      sum = sum + term_func(i)
    end
    return sum

  method term_func(x) static
    return 1/x
    
Where term should clearly be evaluated, I fake it out by evaluating this in a function; the AppleScript version does the same if I am not mistaken:

set i to 0
 
on jsum(i, lo, hi, term)
set {temp, i's contents} to {0, lo}
repeat while i's contents ≤ hi
set {temp, i's contents} to {temp + (term's f(i)), (i's contents) + 1}
end repeat
return temp
end jsum
 
script term_func
on f(i)
return 1 / i
end f
end script
 
return jsum(a reference to i, 1, 100, term_func)

as opposed to Ruby, which has a cool way to say:

def sum2(lo, hi)
lo.upto(hi).inject(0.0) {|sum, n| sum += yield n}
end
p sum2(1, 100) {|i| 1.0/i} # => 5.18737751763962


Now I can do a kind of 'interpret' by writing the term into a file, starting the interpreter and having it return the result, but this needs to be done 100 times and the code and the performance would be atrocious. If we can compile from a memory buffer it would be better and we could craft a kind of 'interpret' support.

My question is: am I missing something, and does the current NetRexx have a way to resolve this? I haven't added the task to RosettaCode; I did with the others.
If someone else likes to spend some time with these tasks, that would be great. Please do signal that you are working on one, so we are not duplicating work; however pleasant it is to fill some empty moments, there are also other things to do. I am going to put placeholders in the RosettaCode wiki when I am working on one, so that anyone can see which ones are already taken. Alternatively, you can just send an email to this list to reserve a task. Let's see how quickly we can have all the tasks in NetRexx!

best regards,

René.






_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

RE: Jensen's Device; RosettaCode and Interpret

Mike Cowlishaw
Busy and backlogged at the moment (son graduated from Cambridge on Thursday) .. so no time to investigate myself .. but a quick question would be to ask: 'is there a Java example'?
 
Also .. writing and reading files might not be as slow as you think; the data may never be flushed to the disk.  DIR > file and reading the file is often faster than using RxQueue, for example...
 
Mike

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Jensen's Device; RosettaCode and Interpret

rvjansen
no, there is no java for this, otherwise I would have translated it one-to-one; going to try the 'interpret' via diskfile later.

best regards,

René.

On 2 jul 2011, at 15:49, Mike Cowlishaw wrote:

Busy and backlogged at the moment (son graduated from Cambridge on Thursday) .. so no time to investigate myself .. but a quick question would be to ask: 'is there a Java example'?
 
Also .. writing and reading files might not be as slow as you think; the data may never be flushed to the disk.  DIR > file and reading the file is often faster than using RxQueue, for example...
 
Mike
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/



_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Jensen's Device; RosettaCode and Interpret

Kermit Kiser
René --

I can't say I see any use for this feature unless you are writing some kind of general library of functions since you have to recompile in order to change your term function. However the problem intrigued me so I wrote up a test sample using the interpreter API. It runs several passes both with the interpreter API and your "function" example. In no case could I detect any elapsed time in milliseconds, even on the first pass to load the interpreter and translate the interpreted term file. Granted I am running on my 3.6 GHz hexcore dev box, but I expected at least some time on the first load and translate. Can you try the following "jensen.nrx"program and tell me the results? BTW, the interpreter API is a bit complex to use - I suggest we simplify it somehow.

-- Kermit

import COM.ibm.netrexx.process.

class jensen
   
    properties static
        interpreter=NetRexxA
        exp=Rexx ""   
        termMethod=Method
       
    method main(x=String[]) static
   
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed1="(endtime-starttime)
       
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed2="(endtime-starttime)
       
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed3="(endtime-starttime)

            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed1="(endtime-starttime)
               
            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed2="(endtime-starttime)
   
     method sum(i,start,finish,term) static
        sum = Rexx 0
        loop i=start to finish
          sum = sum + term_func(i)
        end
        return sum
   
     method term_func(x) static
        return 1/x

    method sumkk(i,lo,hi,term) static SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
   
        sum=0
        loop iv=lo to hi
            sum=sum+termeval(i,iv,term)
            end
        return sum
   
    method termeval(i,iv,e) static returns Rexx SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
        if e\=exp then interpreter=null
        exp=e
   
        if interpreter=null then do
            termpgm='method term('i'=Rexx) static returns rexx;return' e
            fw=FileWriter("termpgm.nrx")
            fw.write(termpgm,0,termpgm.length)
            fw.close
            interpreter=NetRexxA()
            interpreter.parse([String 'termpgm.nrx'],[String 'nocrossref'])
            termClass=interpreter.getClassObject(null,'termpgm')
            classes=[interpreter.getClassObject('netrexx.lang', 'Rexx', 0)]
            termMethod=termClass.getMethod('term', classes)
            end
   
        return Rexx termMethod.invoke(null,[iv])
           
       
On 7/2/2011 11:48 AM, René Jansen wrote:
no, there is no java for this, otherwise I would have translated it one-to-one; going to try the 'interpret' via diskfile later.

best regards,

René.

On 2 jul 2011, at 15:49, Mike Cowlishaw wrote:

Busy and backlogged at the moment (son graduated from Cambridge on Thursday) .. so no time to investigate myself .. but a quick question would be to ask: 'is there a Java example'?
 
Also .. writing and reading files might not be as slow as you think; the data may never be flushed to the disk.  DIR > file and reading the file is often faster than using RxQueue, for example...
 
Mike
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/


_______________________________________________ Ibm-netrexx mailing list [hidden email] Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Jensen's Device; RosettaCode and Interpret

rvjansen
Kerrmit,

well, I get this:

===== Exec: jensen =====
NetRexx portable processor, version 3.00
Copyright (c) RexxLA, 2011.  All rights reserved.
Parts Copyright (c) IBM Corporation, 1995,2008.
Program termpgm.nrx
    function term(Rexx)
5.18737752
kk elapsed1=0
5.18737752
kk elapsed2=0
5.18737752
kk elapsed3=0
5.18737752
elapsed1=0
5.18737752
elapsed2=0
Processing of 'jensen.nrx' complete

unmeasurable in millis, which is quite good and in line with what Mike said.
Do you mind if I paste the example in RosettaCode? I do agree that we can simplify this api, hopefully into a full 'interpret'.

best regards,

René.

On 3 jul 2011, at 03:06, Kermit Kiser wrote:

René --

I can't say I see any use for this feature unless you are writing some kind of general library of functions since you have to recompile in order to change your term function. However the problem intrigued me so I wrote up a test sample using the interpreter API. It runs several passes both with the interpreter API and your "function" example. In no case could I detect any elapsed time in milliseconds, even on the first pass to load the interpreter and translate the interpreted term file. Granted I am running on my 3.6 GHz hexcore dev box, but I expected at least some time on the first load and translate. Can you try the following "jensen.nrx"program and tell me the results? BTW, the interpreter API is a bit complex to use - I suggest we simplify it somehow.

-- Kermit

import COM.ibm.netrexx.process.

class jensen
   
    properties static
        interpreter=NetRexxA
        exp=Rexx ""   
        termMethod=Method
       
    method main(x=String[]) static
   
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed1="(endtime-starttime)
       
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed2="(endtime-starttime)
       
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed3="(endtime-starttime)

            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed1="(endtime-starttime)
               
            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed2="(endtime-starttime)
   
     method sum(i,start,finish,term) static
        sum = Rexx 0
        loop i=start to finish
          sum = sum + term_func(i)
        end
        return sum
   
     method term_func(x) static
        return 1/x

    method sumkk(i,lo,hi,term) static SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
   
        sum=0
        loop iv=lo to hi
            sum=sum+termeval(i,iv,term)
            end
        return sum
   
    method termeval(i,iv,e) static returns Rexx SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
        if e\=exp then interpreter=null
        exp=e
   
        if interpreter=null then do
            termpgm='method term('i'=Rexx) static returns rexx;return' e
            fw=FileWriter("termpgm.nrx")
            fw.write(termpgm,0,termpgm.length)
            fw.close
            interpreter=NetRexxA()
            interpreter.parse([String 'termpgm.nrx'],[String 'nocrossref'])
            termClass=interpreter.getClassObject(null,'termpgm')
            classes=[interpreter.getClassObject('netrexx.lang', 'Rexx', 0)]
            termMethod=termClass.getMethod('term', classes)
            end
   
        return Rexx termMethod.invoke(null,[iv])
           
       
On 7/2/2011 11:48 AM, René Jansen wrote:
no, there is no java for this, otherwise I would have translated it one-to-one; going to try the 'interpret' via diskfile later.

best regards,

René.

On 2 jul 2011, at 15:49, Mike Cowlishaw wrote:

Busy and backlogged at the moment (son graduated from Cambridge on Thursday) .. so no time to investigate myself .. but a quick question would be to ask: 'is there a Java example'?
 
Also .. writing and reading files might not be as slow as you think; the data may never be flushed to the disk.  DIR > file and reading the file is often faster than using RxQueue, for example...
 
Mike
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/


_______________________________________________ Ibm-netrexx mailing list [hidden email] Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/



_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

RE: Jensen's Device; RosettaCode and Interpret

Michael Dag

Rene,

Shouldn’t you be using System.nanoTime instead of System.currentTimeMillis

I read somewhere in a certain JVM’s the actual time used by System.currentTimeMillis is only updated each 10-15 msec…

 

Michael

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of René Jansen
Sent: Sunday, July 03, 2011 14:04
To: IBM Netrexx
Subject: Re: [Ibm-netrexx] Jensen's Device; RosettaCode and Interpret

 

Kerrmit,

 

well, I get this:

 

===== Exec: jensen =====

NetRexx portable processor, version 3.00

Copyright (c) RexxLA, 2011.  All rights reserved.

Parts Copyright (c) IBM Corporation, 1995,2008.

Program termpgm.nrx

    function term(Rexx)

5.18737752

kk elapsed1=0

5.18737752

kk elapsed2=0

5.18737752

kk elapsed3=0

5.18737752

elapsed1=0

5.18737752

elapsed2=0

Processing of 'jensen.nrx' complete

 

unmeasurable in millis, which is quite good and in line with what Mike said.

Do you mind if I paste the example in RosettaCode? I do agree that we can simplify this api, hopefully into a full 'interpret'.

 

best regards,

 

René.

 

On 3 jul 2011, at 03:06, Kermit Kiser wrote:



René --

I can't say I see any use for this feature unless you are writing some kind of general library of functions since you have to recompile in order to change your term function. However the problem intrigued me so I wrote up a test sample using the interpreter API. It runs several passes both with the interpreter API and your "function" example. In no case could I detect any elapsed time in milliseconds, even on the first pass to load the interpreter and translate the interpreted term file. Granted I am running on my 3.6 GHz hexcore dev box, but I expected at least some time on the first load and translate. Can you try the following "jensen.nrx"program and tell me the results? BTW, the interpreter API is a bit complex to use - I suggest we simplify it somehow.

-- Kermit

import COM.ibm.netrexx.process.

class jensen
   
    properties static
        interpreter=NetRexxA
        exp=Rexx ""   
        termMethod=Method
       
    method main(x=String[]) static
   
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed1="(endtime-starttime)
       
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed2="(endtime-starttime)
       
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed3="(endtime-starttime)

            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed1="(endtime-starttime)
               
            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed2="(endtime-starttime)
   
     method sum(i,start,finish,term) static
        sum = Rexx 0
        loop i=start to finish
          sum = sum + term_func(i)
        end
        return sum
   
     method term_func(x) static
        return 1/x

    method sumkk(i,lo,hi,term) static SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
   
        sum=0
        loop iv=lo to hi
            sum=sum+termeval(i,iv,term)
            end
        return sum
   
    method termeval(i,iv,e) static returns Rexx SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
        if e\=exp then interpreter=null
        exp=e
   
        if interpreter=null then do
            termpgm='method term('i'=Rexx) static returns rexx;return' e
            fw=FileWriter("termpgm.nrx")
            fw.write(termpgm,0,termpgm.length)
            fw.close
            interpreter=NetRexxA()
            interpreter.parse([String 'termpgm.nrx'],[String 'nocrossref'])
            termClass=interpreter.getClassObject(null,'termpgm')
            classes=[interpreter.getClassObject('netrexx.lang', 'Rexx', 0)]
            termMethod=termClass.getMethod('term', classes)
            end
   
        return Rexx termMethod.invoke(null,[iv])
           
       
On 7/2/2011 11:48 AM, René Jansen wrote:

no, there is no java for this, otherwise I would have translated it one-to-one; going to try the 'interpret' via diskfile later.

 

best regards,

 

René.

 

On 2 jul 2011, at 15:49, Mike Cowlishaw wrote:



Busy and backlogged at the moment (son graduated from Cambridge on Thursday) .. so no time to investigate myself .. but a quick question would be to ask: 'is there a Java example'?

 

Also .. writing and reading files might not be as slow as you think; the data may never be flushed to the disk.  DIR > file and reading the file is often faster than using RxQueue, for example...

 

Mike

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

 

 
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
 

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

 


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Jensen's Device; RosettaCode and Interpret

rvjansen
<base href="x-msg://1207/">Michael,

I did not know about that one - I'll give it a try.

best regards,

René.

On 3 jul 2011, at 15:19, Michael Dag wrote:

Rene,
Shouldn’t you be using System.nanoTime instead of System.currentTimeMillis
I read somewhere in a certain JVM’s the actual time used by System.currentTimeMillis is only updated each 10-15 msec…
 
Michael
 
From: [hidden email] [mailto:[hidden email]] On Behalf Of René Jansen
Sent: Sunday, July 03, 2011 14:04
To: IBM Netrexx
Subject: Re: [Ibm-netrexx] Jensen's Device; RosettaCode and Interpret
 
Kerrmit,
 
well, I get this:
 
===== Exec: jensen =====
NetRexx portable processor, version 3.00
Copyright (c) RexxLA, 2011.  All rights reserved.
Parts Copyright (c) IBM Corporation, 1995,2008.
Program termpgm.nrx
    function term(Rexx)
5.18737752
kk elapsed1=0
5.18737752
kk elapsed2=0
5.18737752
kk elapsed3=0
5.18737752
elapsed1=0
5.18737752
elapsed2=0
Processing of 'jensen.nrx' complete
 
unmeasurable in millis, which is quite good and in line with what Mike said.
Do you mind if I paste the example in RosettaCode? I do agree that we can simplify this api, hopefully into a full 'interpret'.
 
best regards,
 
René.
 
On 3 jul 2011, at 03:06, Kermit Kiser wrote:


René --

I can't say I see any use for this feature unless you are writing some kind of general library of functions since you have to recompile in order to change your term function. However the problem intrigued me so I wrote up a test sample using the interpreter API. It runs several passes both with the interpreter API and your "function" example. In no case could I detect any elapsed time in milliseconds, even on the first pass to load the interpreter and translate the interpreted term file. Granted I am running on my 3.6 GHz hexcore dev box, but I expected at least some time on the first load and translate. Can you try the following "jensen.nrx"program and tell me the results? BTW, the interpreter API is a bit complex to use - I suggest we simplify it somehow.

-- Kermit

import COM.ibm.netrexx.process.

class jensen
    
    properties static
        interpreter=NetRexxA
        exp=Rexx ""    
        termMethod=Method
        
    method main(x=String[]) static
    
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed1="(endtime-starttime)
        
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed2="(endtime-starttime)
        
            starttime=System.currentTimeMillis
        say sumkk('i',1,100,'1/i')
            endtime=System.currentTimeMillis
            say "kk elapsed3="(endtime-starttime)

            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed1="(endtime-starttime)
                
            starttime=System.currentTimeMillis
        say sum( 'i', '1', '100', '1/i' )     /*invoke SUM (100th harmonic num.)*/
            endtime=System.currentTimeMillis
            say "elapsed2="(endtime-starttime)
    
     method sum(i,start,finish,term) static
        sum = Rexx 0
        loop i=start to finish
          sum = sum + term_func(i)
        end
        return sum
    
     method term_func(x) static
        return 1/x

    method sumkk(i,lo,hi,term) static SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
    
        sum=0
        loop iv=lo to hi
            sum=sum+termeval(i,iv,term)
            end
        return sum
    
    method termeval(i,iv,e) static returns Rexx SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException 
        if e\=exp then interpreter=null
        exp=e
    
        if interpreter=null then do
            termpgm='method term('i'=Rexx) static returns rexx;return' e
            fw=FileWriter("termpgm.nrx")
            fw.write(termpgm,0,termpgm.length)
            fw.close
            interpreter=NetRexxA()
            interpreter.parse([String 'termpgm.nrx'],[String 'nocrossref'])
            termClass=interpreter.getClassObject(null,'termpgm')
            classes=[interpreter.getClassObject('netrexx.lang', 'Rexx', 0)]
            termMethod=termClass.getMethod('term', classes)
            end
    
        return Rexx termMethod.invoke(null,[iv])
            
        
On 7/2/2011 11:48 AM, René Jansen wrote:
no, there is no java for this, otherwise I would have translated it one-to-one; going to try the 'interpret' via diskfile later.
 
best regards,
 
René.
 
On 2 jul 2011, at 15:49, Mike Cowlishaw wrote:


Busy and backlogged at the moment (son graduated from Cambridge on Thursday) .. so no time to investigate myself .. but a quick question would be to ask: 'is there a Java example'?
 
Also .. writing and reading files might not be as slow as you think; the data may never be flushed to the disk.  DIR > file and reading the file is often faster than using RxQueue, for example...
 
Mike

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

 
 
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
 

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

 
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/



_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Jensen's Device; RosettaCode and Interpret

Kermit Kiser
In reply to this post by rvjansen
René -

Sure you can use it if you like. Here is a slightly cleaned up version
with error handling that passes the variable name and value in a single
Rexx item. The advantage being that it can be easily extended to support
multi-variable expressions by passing the name/value pairs as an array.

import COM.ibm.netrexx.process.

class JensensDevice

     properties static
         interpreter=NetRexxA
         exp=Rexx ""
         termMethod=Method

     method main(x=String[]) static

         say sum('i',1,100,'1/i')

     method sum(i,lo,hi,term) static

         sum=0
         varname=i
         loop i=lo to hi
             i['name']=varname
             sum=sum+termeval(term,i)
             end
         return sum

     method termeval(expression,variable) static returns Rexx
         if expression\=exp then interpreter=null
         exp=expression
      do
         if interpreter=null then do
             termpgm='method term('variable['name']') static returns
rexx;return' expression
             fw=FileWriter("termpgm.nrx")
             fw.write(termpgm,0,termpgm.length)
             fw.close
             interpreter=NetRexxA()
             interpreter.parse([String 'termpgm.nrx'],[String 'nocrossref'])
             termClass=interpreter.getClassObject(null,'termpgm')
             classes=[interpreter.getClassObject('netrexx.lang', 'Rexx', 0)]
             termMethod=termClass.getMethod('term', classes)
             end

         return Rexx termMethod.invoke(null,[variable['value']])
      catch err=Exception; err.printStackTrace; return Rexx ''
      end



On 7/3/2011 5:04 AM, René Jansen wrote:

> Kerrmit,
>
> well, I get this:
>
> ===== Exec: jensen =====
> NetRexx portable processor, version 3.00
> Copyright (c) RexxLA, 2011.  All rights reserved.
> Parts Copyright (c) IBM Corporation, 1995,2008.
> Program termpgm.nrx
>     function term(Rexx)
> 5.18737752
> kk elapsed1=0
> 5.18737752
> kk elapsed2=0
> 5.18737752
> kk elapsed3=0
> 5.18737752
> elapsed1=0
> 5.18737752
> elapsed2=0
> Processing of 'jensen.nrx' complete
>
> unmeasurable in millis, which is quite good and in line with what Mike
> said.
> Do you mind if I paste the example in RosettaCode? I do agree that we
> can simplify this api, hopefully into a full 'interpret'.
>
> best regards,
>
> René.
>
>
_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/