Euphoria vs. C and OOP
- Posted by Bill Reed <rpfproeu at netscape.net> Jan 04, 2007
- 769 views
People have been saying that Euphoria should be more "C" like, or more "OOP" like. Euphoria has unique features that set it apart from C or OOP, and make it flexible, but in different ways. Instead of trying to make Euphoria fit into a C mold or OOP mold, take advantage of the unique features of Euphoria. sequences are structures, but not like C. They are just as flexible, perhaps even more so, since it is easier to add to or change what a sequence is. A sequence can be just a string, "ABC", or a structure:
a = { "ABC", -- string 123, -- integer or atom {"XYZ", 456} -- another sequence }
Referencing these members can be done with constants, like
constant NAME = 1, AMT = 2, DESC = 3, DNAME = 1, DAMT = 2 a[NAME] ("ABC") or a[DESC] ( {"XYZ", 456} ) a[DESC][DNAME] ("XYZ") a[DESC][DAMT] (456)
C has Public and Private functions. Euphoria can have them too, but in different ways. See the following include and program to see what I mean. I hope this can stimulate some discussion as to using Euphoria in a more structured way. ==================================================================== == Member banking.e == ====================================================================
--*----------------------------------------------------------------* --* banking.e bank account program * --* * --* Public * --* function BankProgram * --* * --* Private (or member functions) * --* function CreateAcct * --* function Withdrawal * --* function Deposit * --* procedure Balance * --* * --* example of account: * --* * --* a = {015505, "John Smith", 12000.15} * --* a[1] = acctno * --* a[2] = acctname * --* a[3] = balance * --*----------------------------------------------------------------* global constant acctno = 1, acctname = 2, balance = 3, blank_acct = {0, "", 0} function CreateAcct() -- Private function returns account sequence User_account User_account = blank_acct User_account[acctno] = 111000 -- assign next account # User_account[acctname] = prompt_string("Enter name : ") return User_account end function function Withdrawal(sequence account, atom amount) -- Private function account[balance] -= amount return account end function function Deposit(sequence account, atom amount) -- Private function account[balance] += amount return account end function procedure Balance(sequence account) -- Private procedure printf(1, "account : %d %s Balance : %.2f\n", {account[acctno], account[acctname], account[balance]}) end procedure global function BankProgram(sequence parms) --*----------------------------------------------------------------* --* Public because it is global * --*----------------------------------------------------------------* sequence command, User_account, ret_account atom amount --*----------------------------------------------------------------* --* parms[1] = command * --* parms[2] = account (optional when creating new account) * --* parms[3] = optional amount * --* parms[4]... optional and expandable * --*----------------------------------------------------------------* command = parms[1] if equal(command, "new") then ret_account = CreateAcct() elsif equal(command, "Balance") then User_account = parms[2] Balance(User_account) ret_account = {} elsif equal(command, "Deposit") then User_account = parms[2] amount = parms[3] ret_account = Deposit(User_account, amount) elsif equal(command, "Withdrawal") then User_account = parms[2] amount = parms[3] ret_account = Withdrawal(User_account, amount) else puts(1, "Unknown command\n") return {} end if return ret_account end function
==================================================================== == end of Member banking.e == ==================================================================== ==================================================================== == main program: == ====================================================================
include get.e include graphics.e include banking.e sequence myAccount, null_status text_color(WHITE) bk_color(BLUE) clear_screen() myAccount = BankProgram({"new"}) myAccount = BankProgram({"Deposit", myAccount, 12000.00}) myAccount = BankProgram({"Withdrawal", myAccount, 150.00}) null_status = BankProgram({"Balance", myAccount})
Other functions and features can be added from this basic framework. Features such as checking for overdraft status, adding extra fees, crediting or debiting accounts, etc.