1. Math with sequences and single values
- Posted by jaygade Mar 13, 2011
- 2532 views
Forked from Re: Euphoria 4.0.1
I was going to requote the whole thread but it's there for reference.
Vinoba, can you please explain exactly what you are looking for? Because it seems to me that Euphoria DOES have the feature you're asking about but you aren't quite satisfied with it.
2. Re: Math with sequences and single values
- Posted by jaygade Mar 13, 2011
- 2486 views
-- Now, a REAL invoice program would probably use EDS or an external database -- system and not even worry about sequences in this way exactly. include std/math.e -- Return a price or a list of prices changed by the amount in change. -- Note: a discount must be negative, a markup postive. If the discount or -- markup are on a per-item basis then price and change must be the same length. -- Change is expressed as a decimal indicating a percentage markup or markdown. function priceChange(object price, object change = 0) integer lpr, lch object total if sequence(price) and sequence(change) then lpr = length(price) lch = length(change) -- ensure sequences are the same size by padding with 0s. if lpr < lch then price &= repeat(0, lch - lpr) else change &= repeat(0, lpr - lch) end if end if total = price * (1 + change) return total end function -- This is the equivalent of Vinoba's original "invoice()" function function item_total(object sprice, object squantity) integer lsp, lsq object sitemcost if sequence(sprice) and sequence(squantity) then lsp = length(sprice) lsq = length(squantity) -- ensure sequences are the same size by padding with 0s. if lsp < lsq then sprice &= repeat(0, lsq - lsp) else squantity &= repeat(0, lsp - lsq) end if end if sitemcost = sprice * squantity return sitemcost end function -- Print out the subtotals for each item purchased. Could be re-written to -- return strings or sequences or html or whatever instead. procedure invoice(sequence sitems, object subtotals) integer lsi, lsu if atom (subtotals) then subtotals = {subtotals} -- promote subtotals to a sequence end if lsi = length(sitems) lsu = length(subtotals) if lsi < lsu then sitems &= repeat("", lsu - lsi) else subtotals &= repeat(0, lsi - lsu) end if -- Really, we should have "item, quantity, price, total" but we're not -- doing that complicated. If so then I would refactor this whole thing. for i = 1 to lsi do printf(1, "Item: %s Total: %.2f \n", {sitems[i], subtotals[i]}) end for puts(1, "\n") end procedure -- Main sequence spurchages = { "Screws", "Nails", "Rivets" } sequence spr = {7.34, 2.54, 6.72} object squan = 12 -- buy 12 of everything. sequence sdetails sdetails = item_total(spr, squan) ? sdetails -- the sum cost of each item ? sum(sdetails) -- the total purchase invoice(spurchages, sdetails) squan = { 12, 10, 20 } -- buy different quantities of each thing. sdetails = item_total(spr, squan) ? sdetails ? sum(sdetails) invoice(spurchages, sdetails) sequence disprices = priceChange(spr, -0.15) -- discount everything 15% sdetails = item_total(disprices, squan) ? sdetails ? sum(sdetails) invoice(spurchages, sdetails) disprices = priceChange(spr, {-0.10, 0, -0.20}) -- discount by different amounts sdetails = item_total(disprices, squan) ? sdetails ? sum(sdetails) invoice(spurchages, sdetails)
Not perfect, but it demonstrates what we've been talking about.
BTW, the example program you posted would fail, or at least the invoice() method would if the sequences sprice and squantity were different lengths anyway.
This program - while a simplification, but that's what we're working with here - shows operations on sequences and single values and operations on sequences and sequences. One caveat with regards to operations on two sequences is that they must have the same length and shape. The interpreter doesn't assume that you haven't made a mistake and silently pad your shorter sequences with zeroes or empty strings if they don't match. That's up to the programmer to do.
3. Re: Math with sequences and single values
- Posted by jaygade Mar 14, 2011
- 2462 views
500 items in a store. Each item has a separate price Some have discounts (on varying days) Whole section of items (e.g. all tiles) have one single discount as a special which will last only till Friday and then not. Customers come in and buy - sometime 5 items and sometimes 100 items and in small and large quantities. Whilst the cashier is busy, the manger comes to her and says "give him 10% discount on all lumber and 5% on all trusses", the demure old lady next in queue is waiting to buy one screwdriver only with its special discount today. What I have described is a normal store. Yes they all have nicely programmed cash machines so you and I do have to write a program for them, but supposing you were writing an application?
Somewhere in this situation we are faced with having to operate on single values in a sequence and a function with parameters defined as (sequence a, sequence b, sequence c, sequence x, sequence y, sequence z ) does not work if we have to build a loop around each sequence items, because here, there and everywhere, now and then and tomorrow, the sequence supplied will not be sequence but a single element.
Similar problem would arise in a 80 employee office getting payroll. some have exact percentage deductions, some have special deductions, some have holiday pay etc. To accommodate all this in a FUNCTION, you have to work in a loop of a sequence of names or numbers and apply sequences of pay/hour etc. You end up with a loop and your parameter definitions (sequence a,sequence b,sequence c,sequence x,sequence y, sequence z, ) bombs.
You know, the more I look at this, especially the last paragraph, the more I realize that you are approaching the problem in completely the wrong way.
If I need to figure a payroll, and I have x number of employee records, each with a salary and certain regular and irregular deductions, well, then, I would probably approach this in a much more object-oriented fashion. That is, each employee record is its own object, and "deductions" would be a class of which there would be several different objects. The list of deductions which would apply to any given employee would be a part of his or her record.
To figure payroll a given method would take an employee record, the number of hours worked in the pay period, any special deductions in the pay period not otherwise contained in the employee record's "normal" deductions, any given bonuses, and would itself return a record of total pay plus bonuses minus deductions and probably itemized.
I mean, such complicated situations as invoicing and payroll as you are presenting are FAR more complicated than just any given binary operation, whether that binary operation can handle sequences or atoms or both. There are a lot of data members to deal with both as input and as output.
4. Re: Math with sequences and single values
- Posted by ChrisB (moderator) Mar 14, 2011
- 2505 views
Hi
Coming in late, but I agree - Vinoba's making his requirements far more complicated than they need to be.
IMHO, each item should be a Record in a Table with each property a Field (price, tax/VAT, discount, etc), then the pricing of each item should act on the items Fields. Ie using a database.
Secondly, and forgive me if I'm wrong, but if each priced up item has a variable number of sequences for the final 'invoice' calculation, then that's going to lead to a lot of nightmares anyway. Again IMHO, each 'invoiced up' item should have a fixed number of items within the sequence, and it's often best to assign a constant to each sequence number, eg
constant PRICE = 1, TAX = 2, DISCOUNT = 3 (etc)
item[PRICE] = 20
item[TAX] = item[PRICE] * current_tax
etc
just my 2p worth
Chris