Set specific range for int in scala - scala

class Test(val myInt :Int){}
I want that for myInt only 0 to 10 must be allowed.So how to specify range for member variable in scala with val type.

Give Refined a look. It allows you to create range types which are checked at compile time.
Your range would look like this:
type InMyRange = Interval.ClosedOpen[W.`0`.T, W.`10`.T]
and you can create a value of this type like so:
refineMV[InMyRange](0)
// Refined[Int, InMyRange] = 0
refineMV[InMyRange](9)
// Refined[Int, InMyRange] = 9
In the error cases:
refineMV[InMyRange](-1)
// Left predicate of (!(-1 < 0) && (-1 < 10)) failed: Predicate (-1 < 0) did not fail
refineMV[InMyRange](10)
// Right predicate of (!(10 < 0) && (10 < 10)) failed: Predicate failed: (10 < 10)

simply adding require or whatever which throws an exception when it gets invalid value solves your issue.
class Test(val myInt :Int){
require(0 <= myInt && myInt <= 10)
}

Related

Drools rule with not condition with multiple condition causing error

When i use below condition with 'not' I am getting an error.
not(Obj1(value == 0) && Obj2(value <= 3))
However if i replace above condition as below I am not getting any casting exception
Obj1(value != 0) or Obj2(value > 3)
The rule looks like this:
rule "test_6"
salience 10
when
not(Obj1(value == 0) && Obj2(value <= 3))
then
.....
end
And this is the error I'm getting:
throwing error Error Message: org.drools.core.rule.GroupElement cannot be cast to org.drools.core.rule.Pattern
The && and || operators can only be used inside a single pattern. For example: Obj1( value > 3 && value < 10 || value == 0). According to the documentation, to separate Patterns, you have to use the and and or operators.
So, in your case, your rule should be:
rule "test_6"
salience 10
when
not(Obj1(value == 0) and Obj2(value <= 3))
then
.....
end
Note that it was not failing when you were using or because that was the right operator to use instead of ||.
Hope it helps,

How to resolve a stack overflow error while solving the coin change problem using recursion

I'm getting stack overflow error while running the below function for coin change problem.
{
def countchanger(m: Int, c:List[Int]): Int = {
if (money == 0 || coins.isEmpty) 1
else if (money < 0) 0
else countchanger(money - coins.head, coins) + countchanger(money, coins.tail)
}
countchanger(money, coins.sorted)
}
Appreciate any help in understanding why the stack overflow error and how this function can be written in a better way.
This is not an issue when countchanger function is removed from countChange function.
def countChange(money: Int, coins: List[Int]): Int =
{
if ((money < 0) || coins.isEmpty) 0
else if (money == 0) 1
else countChange(money - coins.head, coins) + countChange(money, coins.tail)
}
In your code you have called coins.tail and coins.head rather than c.tail and c.head. This means that the function is endlessly calling itself from the second iteration onward with the same input parameters, thus never completing and hitting the maximum depth of recursive calls allowed. A good example of why maintaining the scope of variable names is important!

Arithmetically simulate 32-bit integer overflow

Is there a way to arithmetically simulate 32-bit, twos-complement integer overflow with numbers of a type whose value space is a strict superset of that of the 32-bit twos-complement integers? I need to perform such an operation in, for example, WolframAlpha, which has no explicit typing and no type casting.
The output of my desired convertTo32BitSignedInt(aValue) function needs to be the same as if I had cast the value in a language that supports it, such as Java: (int)aValue.
By example, the output of convertTo32BitSignedInt(17643225600) needs to be 463356416, the same as I had used the following cast (int)17643225600.
For the moment, my convertTo32BitSignedInt function (in pseudo code) looks like this and I am pretty sure it's not the better solution.
if(x > 2147483647 && floor(x / 2147483647 ) == 1){
return (x - 2*2147483648);
}else if(x > 2147483647 && (x % 2147483647 == 0) && (x % 2 == 0)){
return -1 * (x / 2147483647);
}else if(x > 2147483647 && (x % 2147483647 == 0) && (x % 2 > 0)){
return -1 * ((x - 2147483647) / 2147483647) + 2147483647;
}else if(x > 2147483647 && (x % 2147483647 > 0) && (x % 2 == 0)){
return -1 * floor(x / 2147483647) + (x % 2147483647);
}
//...
Use Case:
I try to demonstrate a certain behavior that will occures when there is a 32-bit signed integer overflow in a java program using a recursive implementation of the factorial function.
public int factorial(int n) {
return n == 1 || n==0 ? 1 : n * factorial(n - 1);
}
This int implementation, for factorial(13) gives 1932053504, because 13 * 479001600 > 2147483647, the 32-bit signed integer maximum value.
I use WolframAlpha for the demonstration. WolframAlpha allows numbers > 2147483647 and I want to simulate these number as 32-bit integer numbers. WolframAlpha gives the real answer which is 6227020800. I would like to be able to convert 6227020800 as 1932053504.
Edit: this is a rip & replace of my initial answer
You can do something roughly like this:
convertTo32BitSignedInt(aValue) {
bits32 = BitAnd(aValue, BitShiftLeft(1, 32) - 1);
sign = BitShiftRight(bits32, 31);
return bits32 - BitShiftLeft(sign, 32);
}

Return zero or positive number?

I was initially thinking that the code below would return 0, my question, is there a function that I can use to only receive zero/positive results here?
NSUInteger pruneSize = 5 - 20; // Returns: -15
Currently I am just checking the result myself, but was wondering if I was missing something simpler.
NSUInteger pruneSize = 5 - 20;
if(pruneSize >= 0) {
// Do zero/positive Stuff ...
}
pruneSize >= 0 is always true as pruneSize is unsigned. You should get a warning here. You need to change the type to NSInteger, that is the signed integer. If you want to clip the lower value to zero for a signed int then you can do this:
NSInteger pruneSize = 5 - 20; // signed int
pruneSize = pruneSize < 0 ? 0 : pruneSize;
You can use abs(pruneSize) which will return you positive or zero number in any case.
EDIT:
NSUInteger pruneSize = 5-20;
if(pruneSize < 0)
{
pruneSize = 0;
}
NSLog(#"%d",pruneSize);
Hope this helps you.
If you want your function to return always zero if your result is in negative(less than 0) then return zero or else return result
int n=0;
if(result > 0){ //positive
n = result
else
n = 0
return n
or use the abs method

multiple instance if statement in xcode (iphone)

what is the proper way to do If statement with two variable in Xcode for Iphone
currently I have
if (minute >0) && (second == 0) {
minute = minute - 1;
second = 59;
}
The same was you would do it in any C/C++/Objective-C compiler, and most Algol derived languages, and extra set of parenthesis in order to turn to seperate boolean statements and an operator into a single compound statement:
if ((minute > 0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
You'll need another set of parenthesis:
if ((minute >0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
Or you could also write:
if (minute > 0 && second == 0)
Which is what you'll start doing eventually anyway, and I think (subjective) is easier to read. Operator precedence insures this works...