Re: pass by ref
- Posted by _tom (admin) Feb 11, 2016
- 2191 views
I've only had trivial needs - I used a "beginner's" method. Perhaps others do similar things? I make a "big data" sequence. I define "pointer constants" for everything relevant. As a clumsy example, let's imagine ... :
constant boss = 1, someone_else = 2,...telno = 1, address = 2, next_meeting = 3,
my_function(integer name, integer section, ... etc.)
which can read OR modify mybigdata[boss][next_meeting] as required. OK for simple applications?
OpenEuphoria frees you from some ideas you get by reading about programming in "other language." I wrote a tutorial to debunk ideas a beginner may have about PBR and "bigdata." A "clumsy example" now becomes a useful personal program.
_tom
OpenEuphoria vs PBR
Here are some thoughts about PBR (Pass-By-Reference) . If you program in OpenEuphoria you do not need PBR.
Pass-By-Reference
Using "other-language" the idea of PBR seems to offer:
- speed
- laziness
- OOP
speed
Euphoria is fast without speed tricks like PBR. However, if your are programming in "other language" then tricks to speed things up are worthwile. Speed is the bonus you get by programming in Euphoria.
laziness
Using PBR to avoid writing an assignment statement is good for laziness and bad for safety. Unfortunately, PBR hides the exact location where the value of an object is changed in your code. Euphoria programs are easier to read and debug without PBR.
OOP
OOP style programming, while clever, can turn a simple program into a complicated program. By avoiding OOP and PBR you can keep programs simple. OpenEuphoria without PBR is friendlier.
But I Want PBR
OpenEuphoria is flexible if you insist. There is the option of using pseudo memory or structures to write powerful code. OpenEuphoria lets you write programs in your own style.
Conclusion
You don't need PBR to write efficient OpenEuphoria programs.
OpenEuphoria vs Big Data
The OpenEuphoria sequence is a marvel of language design. Go ahead and write reallybig_copy = reallybig without a speed or memory penalty because no actual copy is made. It "looks" like a copy and "behaves" like a copy but Euphoria actually uses internal pointers and optimizations to make your program fast and efficient. Go ahead and write simple--beginner--programs and let OpenEuphoria cope with "big data" effortlessly.
BigData Example
Create an enum to make indexing easier
enum Boss, Someone_else, Telno, Address, Next_meeting
You get a set of constants numbered 1 2 3 4 5 without extra work.
You could create a huge sequence by writing:
atom hugenumber = 1_000_000 sequence bigdata = repeat( {0,0,0,0,0}, hugenumber )
That gives you a database with a million items ready to be updated.
But, why bother when you can add an item just when you need it:
sequence bigdata = {} bigdata = append(bigdata, {"president", "", 41234, "head office", "Mon", "2:30pm" } )
You now have:
{ {"president", "", 41234, "head office", "Mon 2:30pm" }, -- room for lots more $ }
You are free to add, change, and remove items at any time.
Finally, use the enum constants to read and write values:
-- change values bigdata[1][Boss] = "Mr Big" bigdata[1][Next_meeting] = "Tue 9:00am" -- look at the values include std/console.e display( bigdata[1] )
You could create a procdure to output data:
procedure show( sequence data_item ) printf(1, ` Boss : %s Someone_else : %s Telno : %s Address : %s Next_meeting : %s `, data_item ) end procedure
You now get output like:
show( bigdata[1] )
Boss : Mr Big Someone_else : Telno : 41234 Address : head office Next_meeting : Tue 9:00am
A utilty to enter|change data looks like:
function write( sequence boss="", sequence someone_else="", sequence telno="", sequence address="", sequence nextmeeting="" ) return { boss, someone_else, telno, address, nextmeeting } end function
For example you could add a new item:
bigdata = append(bigdata, write("CEO","","","Branch Office", "Fri 3pm" ) ) display( bigdata )
The demo program now looks like:
enum Boss, Someone_else, Telno, Address, Next_meeting sequence bigdata = {} bigdata = append(bigdata, {"president", "", "41234", "head office", "Mon", "2:30pm" } ) -- change values bigdata[1][Boss] = "Mr Big" bigdata[1][Next_meeting] = "Tue 9:00am" -- look at the values include std/console.e display( bigdata[1] ) /* { "Mr Big", "", 41234, "head office", "Tue 9:00am", "2:30pm" } */ procedure show( sequence data_item ) printf(1, ` Boss : %s Someone_else : %s Telno : %s Address : %s Next_meeting : %s `, data_item ) end procedure show( bigdata[1] ) /* Boss : Mr Big Someone_else : Telno : 41234 Address : head office Next_meeting : Tue 9:00am */ --A utilty to enter|change data looks like: function write( sequence boss="", sequence someone_else="", sequence telno="", sequence address="", sequence nextmeeting="" ) return { boss, someone_else, telno, address, nextmeeting } end function --For example you could add a new item: bigdata = append(bigdata, write("CEO","","","Branch Office", "Fri 3pm" ) ) display( bigdata ) /* { "Mr Big", "", "41234", "head office", "Tue 9:00am", "2:30pm" }, { "CEO", "", "", "Branch Office", "Fri 3pm" } } */
Conclusion
You can create a small database program for yourself, with unlimited capacity and flexibility, with very little code.