Re: Missing in misc.e
- Posted by Derek Parnell <ddparnell at bi?pon?.com> Jul 21, 2007
- 715 views
CChris wrote: > Would there be any objections to adding (with proper documentation) > the following to misc.e: Well I might as well submit my versions too The first file is my 'assert' implementation.
-- assert.e include misc.e --: Parameters: --: pA = Any value --: pB = Any value --: pName = Displayable name to identify this assert if it fails. --: Description: If pA and pB are not equal, this routine displays --: a message identifying the assert test and then aborts the program. --: --: Example: --: assert(sqrt(4), 2, "sqrt test") --: assert(isSaved, 0, "File should not be saved yet.") --: assert(fileType, "exw", "Only 'exw' is allowed") global procedure assert(object pA, object pB, sequence pName) if compare(pA, pB) != 0 then printf(2, "Assert error '%s'\n", {pName}) puts(2, "** First argument ...\n") pretty_print(2, pA, {2}) puts(2, "\n** Second argument ...\n") pretty_print(2, pB, {2}) ? 1/0 end if end procedure
Now here is my math.e library. I wrote this after seeing the contributions so far.
-- math.e constant vUnitTesting = find("-unittest", command_line()) include assert.e as assert --: Return: The absolute value of the argument. --: Parameters: --: pData = The data whose absolute value is returned. --: Description: When the argument is a sequence, each element is made absolute, and --: this is done recursively to each sub-sequence as well. --: Example: --: abs(3) returns 3 --: abs(-3) returns 3 --: abs(3.21) returns 3.21 --: abs(-3.21) returns 3.21 --: abs({1,-2,3,-4}) returns {1,2,3,4} --: abs({1,-2,{3.1,-3.2,-3.3},-4}) returns {1,2,{3.1,3.2,3.3},4} --------------------------------------------------------- global function abs(object pData) --------------------------------------------------------- object lTemp if atom(pData) then if pData >= 0 then return pData end if return -pData end if for i = 1 to length(pData) do lTemp = pData[i] if atom(lTemp) then if lTemp < 0 then pData[i] = -lTemp end if else pData[i] = abs(lTemp) end if end for return pData end function if vUnitTesting then assert:assert(abs(0), 0, "abs0") assert:assert(abs(3), 3, "abs1") assert:assert(abs(-3), 3, "abs2") assert:assert(abs(3.21), 3.21, "abs3") assert:assert(abs(-3.21), 3.21, "abs4") assert:assert(abs({1,-2,3,-4}), {1,2,3,4}, "abs5") assert:assert(abs({1,-2,{3.1,-3.2,-3.3},-4}), {1,2,{3.1,3.2,3.3},4}, "abs6") end if --: Return: The sign of the argument. -1 means negative, 1 means positive. --: Note that zero is deemed as positive. --: Parameters: --: pData = The data whose sign is returned. --: Description: When the argument is a sequence, the sign of each element --: is set, and this is done recursively to each sub-sequence as well. --: Example: --: sign(3) returns 1 --: sign(-3) returns -1 --: sign(3.21) returns 1 --: sign(-3.21) returns -1 --: sign({1,-2,3,-4}) returns {1,-1,1,-1} --: sign({1,-2,{3.1,-3.2,-3.3},-4}) returns {1,-1,{1,-1,-1},-1} --------------------------------------------------------- global function sign(object pData) --------------------------------------------------------- object lTemp if atom(pData) then if pData >= 0 then return 1 end if return -1 end if for i = 1 to length(pData) do lTemp = pData[i] if atom(lTemp) then if lTemp < 0 then pData[i] = -1 else pData[i] = 1 end if else pData[i] = sign(lTemp) end if end for return pData end function if vUnitTesting then assert:assert(sign(0), 1, "sign0") assert:assert(sign(3), 1, "sign1") assert:assert(sign(-3), -1, "sign2") assert:assert(sign(3.21), 1, "sign3") assert:assert(sign(-3.21), -1, "sign4") assert:assert(sign({1,-2,3,-4}), {1,-1,1,-1}, "sign5") assert:assert(sign({1,-2,{3.1,-3.2,-3.3},-4}), {1,-1,{1,-1,-1},-1}, "sign6") end if --: Return: The element with the lowest value --: Parameters: --: pList = A list of one or more items to examine. --: Description: The elements of the list are compared to each other --: and the one with the lowest value is returned. --: --: This does not do a recursive compare on sub-sequences. --: --: An empty sequence will cause a runtime error. --: --: Example: --: min({1,-2, 3.1,-3.2,-3.3,-4}) returns -4 --: min({"one", "two", "three", "four"}) returns "four" --------------------------------------------------------- global function min(sequence pList) --------------------------------------------------------- object lResult lResult = pList[1] for i = 2 to length(pList) do if compare(lResult, pList[i]) = 1 then lResult = pList[i] end if end for return lResult end function if vUnitTesting then assert:assert(min({1}), 1, "min1") assert:assert(min({1,-2, 3.1,-3.2,-3.3,-4}), -4, "min_AB") assert:assert(min({"one", "two", "three", "four"}), "four", "min3") end if --: Return: The element with the highest value --: Parameters: --: pList = A list of one or more items to examine. --: Description: The elements of the list are compared to each other --: and the one with the highest value is returned. --: --: This does not do a recursive compare on sub-sequences. --: --: An empty sequence will cause a runtime error. --: --: Example: --: max({1,-2, 3.1,-3.2,-3.3,-4}) returns 3.1 --: max({"one", "two", "three", "four"}) returns "two" --------------------------------------------------------- global function max(sequence pList) --------------------------------------------------------- object lResult lResult = pList[1] for i = 2 to length(pList) do if compare(lResult, pList[i]) = -1 then lResult = pList[i] end if end for return lResult end function if vUnitTesting then assert:assert(max({1}), 1, "max1") assert:assert(max({1,-2, 3.1,-3.2,-3.3,-4}), 3.1, "max_AB") assert:assert(max({"one", "two", "three", "four"}), "two", "max3") end if --: Return: The index to the element with the lowest value --: Parameters: --: pList = A list of one or more items to examine. --: Description: The elements of the list are compared to each other --: and the index to one with the lowest value is returned. --: --: This does not do a recursive compare on sub-sequences. --: --: An empty sequence will cause a runtime error. --: --: Example: --: min_index({1,-2, 3.1,-3.2,-3.3,-4}) returns 6 --: min_index({"one", "two", "three", "four"}) returns 4 --------------------------------------------------------- global function min_index(sequence pList) --------------------------------------------------------- object lTemp integer lPos lTemp = pList[1] lPos = 1 for i = 2 to length(pList) do if compare(lTemp, pList[i]) = 1 then lTemp = pList[i] lPos = i end if end for return lPos end function if vUnitTesting then assert:assert(min_index({1}), 1, "min_index1") assert:assert(min_index({1,-2, 3.1,-3.2,-3.3,-4}), 6, "min_index2") assert:assert(min_index({"one", "two", "three", "four"}), 4, "min_index3") end if --: Return: The index to the element with the highest value --: Parameters: --: pList = A list of one or more items to examine. --: Description: The elements of the list are compared to each other --: and the index to the one with the highest value is returned. --: --: This does not do a recursive compare on sub-sequences. --: --: An empty sequence will cause a runtime error. --: --: Example: --: max_index({1,-2, 3.1,-3.2,-3.3,-4}) returns 3 --: max_index({"one", "two", "three", "four"}) returns 2 --------------------------------------------------------- global function max_index(sequence pList) --------------------------------------------------------- object lTemp integer lPos lTemp = pList[1] lPos = 1 for i = 2 to length(pList) do if compare(lTemp, pList[i]) = -1 then lTemp = pList[i] lPos = i end if end for return lPos end function if vUnitTesting then assert:assert(max_index({1}), 1, "max_index1") assert:assert(max_index({1,-2, 3.1,-3.2,-3.3,-4}), 3, "max_index2") assert:assert(max_index({"one", "two", "three", "four"}), 2, "max_index3") end if --: Return: The argument with the lowest value --: Parameters: --: pA = Any value --: pB = Any value --: Description: The lower of pA and pB is returned. --: --: This does not do a recursive compare on sub-sequences. --: --: Example: --: min_AB(1,-2) returns -2 --: min_AB("one", "two") returns "one" --------------------------------------------------------- global function min_AB(object pA, object pB) --------------------------------------------------------- return min({pA, pB}) end function if vUnitTesting then assert:assert(min_AB(1,-2), -2, "min_AB_1") assert:assert(min_AB("one", "two"), "one", "min_AB_2") end if --: Return: The argument with the highest value --: Parameters: --: pA = Any value --: pB = Any value --: Description: The higher of pA and pB is returned. --: --: This does not do a recursive compare on sub-sequences. --: --: Example: --: max_AB(1,-2) returns 1 --: max_AB("one", "two") returns "two" --------------------------------------------------------- global function max_AB(object pA, object pB) --------------------------------------------------------- return max({pA, pB}) end function if vUnitTesting then assert:assert(max_AB(1,-2), 1, "max_AB_1") assert:assert(max_AB("one", "two"), "two", "max_AB_2") end if
I can't see the real need for smart internal do-it-all routines here as this is some low-level routines that are all small and can be written in an uncomplicated manner, thus easier to maintain (if needed) and probably runs faster too. -- Derek Parnell Melbourne, Australia Skype name: derek.j.parnell