Merging add and sub functions in SafeMath.sol - solidity

I want to merge the addition and subtraction function with their associated revert guard functions using OR (||) operators, but I am missing the syntax. Could anyone advise?
The functions are initially two in different blocks. I am attempting to make addition and subtraction functions into one and call them using one name in the contract. My assumption is that the OR operator could allow me do that.
I expected the new merged function to work just fine.
But I am getting the error message telling me that there is an overflow.
I want to merge the addition and subtraction function with their associated revert guard functions using OR (||) operators, but I am missing the syntax. Could anyone advise?
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
bool whichOp; // to test which operator to use
whichOp = c = a + b || c = a - b;
whichOp = require(c >= a, "SafeMath: addition overflow") || require(b <= a, "SafeMath: subtraction overflow");
return c;
}
I expected the new merged function to work just fine.
But I am getting the error message telling me that there is an overflow.

Related

use associate array total value count Lua

i want to count the data type of each redis key, I write following code, but run error, how to fix it?
local detail = {}
detail.hash = 0
detail.set = 0
detail.string = 0
local match = redis.call('KEYS','*')
for i,v in ipairs(match) do
local val = redis.call('TYPE',v)
detail.val = detail.val + 1
end
return detail
(error) ERR Error running script (call to f_29ae9e57b4b82e2ae1d5020e418f04fcc98ebef4): #user_script:10: user_script:10: attempt to perform arithmetic on field 'val' (a nil value)
The error tells you that detail.val is nil. That means that there is no table value for key "val". Hence you are not allowed to do any arithmetic operations on it.
Problem a)
detail.val is syntactic sugar for detail["val"]. So if you expect val to be a string the correct way to use it as a table key is detail[val].
Possible problem b)
Doing a quick research I found that this redis call might return a table, not a string. So if detail[val] doesn't work check val's type.

Using a table for variable name in a table is not found when called for

I am making quite the complex thing and I am trying to use tables as variable names cause I have found that lua works with it, that is:
lua
{[{1,2}]="Meep"}
The issue is it is callable, when I do it and try to call it using the same kind of table, it won't find it.
I have tried looking for it and such but I have no clue why it won't do this.
ua
local c = {[{1,2}]="Meep"}
print(c[{1,2}],c)
Do I expect to become but it does not.
"Meep",{[{1,2}]="Meep"}
but what I get is
nil,{[{1,2}]="Meep"}
If I however try
lua
local m={1,2}
local c = {[m]="Meep"}
print(c[m],c)
it becomes the correct one, is there a way to avoid that middle man? After all m=={1,2} will return true.
The problem you have is that tables in lua are represented as references. If you compare two different talbes you are comparing those references. So the equation only gets true if the given tables are exactly the same.
t = { 1, 2, 3 }
t2 = { 1, 2, 3 }
print(t == t) -- true
print(t2 == t) -- false
print(t2 == t2) -- true
Because of this fact, you can pass them in function per reference.
function f(t)
t[1] = 5
end
t2 = { 1 }
f(t2)
print(t2[1]) -- 5
To bypass this behavior, you could (like suggested in comments) serialize the table before using it as a key.

'numpy.float64' object is not callable

I get the error as in the title of my post. I have seen this come up in other questions, but I am interested in understanding what this means since the other answers were in a specific context that does not apply to me.
Secondly, I would like to understand how this applies to my code, shown below. Note that this all works OK if Zindx = 0, but not for any other case.
Zindx = list(E).index(0)
for m in range(0,N):
if m != Zindx:
for n in range(0,N):
if n != Zindx:
if n != m:
x[m,m] = x[m,m] (
- (E[n]-E[m] + E[n])*x[m,n]*x[n,Zindx]
/x[m,Zindx]/E[m]
)
This:
x[m,m] (
- (E[n]-E[m] + E[n])*x[m,n]*x[n,Zindx]
/x[m,Zindx]/E[m]
)
Is trying to call x[m,m] as a function with the expression within the parenthesis as an argument. I am guessing x[m,m] returns a float.
Do you mean to multiply x[m,m] by the term in the parenthesis? If so, add the *.

Nested lambda expression translated from SQL statement

Using lambda expressions, how do you translate this query?
select * from employee where emp_id=1 and dep_id in (1,2,3,4).
I am trying this expression but this results in exceptions:
public IEnumrable<employees> getemployee(int emp,list<int> dep )
{
_employeeService.GetAll(e=>(e.emp_id=emp||emp==null) && (e.dep_id.where(dep.contains(e.dep_id))|| dep.count==0 )
}
Any suggestion for translating these queries to these lambda expressions?
wahts wrong in these function?
I don't mean to sound patronizing, but I'd suggest you pick up a beginner's C# book before going further. You've made errors in basic syntax such as operator precedence and equality comparison.
Also, remember that primitive data types cannot be checked for null, unless they're explicitly specified as nullable. In your case emp would be of type int?,not int.
Meanwhile, try
_employeeService.Where(e=>(e.emp_id==emp) && (dep == null || dep.contains(e.dep_id)) );

Currying and compiler design

This is a homework question:
Explain the transformations the type
of a routine undergoes in partial
parameterization.
So far I understand currying. But I cannot find any resources on how a function like this is implemented by the compiler in memory. Could I be pointed in the right direction, maybe keywords to search for or links to resources or possibly an explanation here of how the compiler generates the type and symbol table among other things thats related to the question.
Thanks.
Currying is the conversion of n argument functions into n unary functions:
Example, if you have a ternary function f :: t1 x t2 x t3 -> t you can represent this function as
f1 :: t1 -> (t2 -> (t3 -> t))
In other words, f1 is a function which takes an argument of type t1 and returns a function of type f2.
f2 :: t2 -> (t3 -> t)
f2 is a function which takes an argument of type t2 and returns a function of type f3.
f3 :: t3 -> t
f3 is a function which takes an argument of type t3 and returns type t.
Example, if f(a,b,c) = a+b*c then:
f3(C) == c1+c2*C where c1 and c2 are constant.
f2(B) == f3(C) where c1 is constant and c2 is replaced with B.
f1(A) == f2(B) where c1 is replaced with A.
In functional languages, functions are first class citizens so its common to have them as return type.
Currying is like fixing a parameter of the function. What you really need to modify is the prototype of the function called.. if you have for example retn_type function(param1, param2) and you currying it on first parameter you set it to a fixed value and you obtain a new function retn_type(param2) that can be called and passed in a different way from the original one.
Actually you can obtain it in a compiler in many ways or hacks but the core of everything to its simplicity is just to redefine a new version that is linked to first one. So when you call retn_type(param2) you execute the same code of first function assuming that parameter1 is specified by curryfication.