pass < or > operator into function as parameter? - actionscript-3

Inside my function there is an if() statement like this:
if(passedValue < staticValue)
But I need to be able to pass a parameter dictating whether the if expression is like above or is:
if(passedValue > staticValue)
But I cant really pass < or > operator in has a parameter, so I was wondering what is the best way to do this?
Also if the language I am using matters its ActionScript 3.0
Thanks!!

Instead of passing an operator, which is impossible in AS3, why not pass a custom comparison function?
function actualFunction(passedValue:Number, compareFunction:Function) {
/* ... */
if(compareFunction(passedValue, staticValue)) {
/* ... Do something ... */
}
/* ... */
}
Then to use it:
actualFunction(6, function(x:Number, y:Number) {
return x > y;
});
or:
actualFunction(6, function(x:Number, y:Number) {
return x < y;
});

Why not make the function take a bool as an argument and perform the comparison directly when calling the function?
ExampleFunction(arg1, (passedValue > staticValue))

I don't know Actionscript, but can't you make a variable called:
bool greaterThan = true;
and if its true, do >, if its false do < ?

You're right, you can't pass operators. You can pass a variable indicating which operator to use though.
lessThan = true;
func(passedValue, lessThan);

Related

AS3 - Get and Set

Why does the get and set keywords exist? They seem to be useless for me...
For example:
public function set player_X(x:Number):void
{
player.x = x;
}
public function setPlayerX(x:Number):void
{
player.x = x;
}
These two functions does the same thing right? And the second one does not use the set keyword.
The difference is that the set method is implicitly called when you set a property of the same name.
You do not have to type the ( ) that do the function call but assign the value via =.
player_X = 5;
vs.
setPlayerX(5);
It can help with information hiding as to the user of a class, this is appears to be a property and can be used as such.

ActionScript "?:" conditional operator and return in function

I have function :
public static function validate(value:*):Boolean
{
...
if(field_counter < FIELD_LIMIT){
field_counter++;
}else{
return false;
}
return true;
}
I want to make it one line, but it shown Syntax error on "return false":
field_counter < FIELD_LIMIT ? field_counter++ : return false;
If field_counter is not a negative number, you can forget that if and compute everything in a single instruction:
public static function validate(value:*):Boolean
{
return (field_counter < FIELD_LIMIT && ++field_counter)
}
The instruction ++field_counter will not be executed if field_counter is not lower than FIELD_LIMIT.
Edit
Here's a preview:
http://wonderfl.net/c/c7lA
Why make it hard on yourself and any other developer when you can make it simple?
if(field_counter >= FIELD_LIMIT)
return false;
field_counter++;
return true;
You can try working around this by testing something about the field_counter (not the nicest way but should work):
return (field_counter < FIELD_LIMIT ? (field_counter++!=null) : false);
You are attempting to stuff a return statement into a conditional. The trick is, the ?: operator returns a value, so you can do say x= y>z ? 1 : z-y; and return statement does not return a value in terms of an expression. You'd better leave the original if statement intact.

Need clear concept of Recursive function at beginning stage

today 1st time i'm trying to learn recursive function thru online tutorials but stuck at beginning stage. i found below code but its provide me 'output: 1' against any value.
so, i need better explanation of that:
function factorial($n){
if($n==0){
return 1;
}
return fact($n, 1);
}
function fact($i, $j){
if($i<1){
return 1;}
else {
return fact($i-1, $i*$j);
}
}
echo factorial(5);
one more thing, i need clarification of how below return method:
return fact($i-1, $i*$j);
will work to convey single value from two parameters. any1 plsss give me some ideas regarding this issue to clear my concept. Thnx in advance..
You have a function factorial here that calls a recursive function fact. A recursive function always works like this:
If you call it with a “trivial” argument, it can immediately give you the answer. In your code, that is the part saying if($i<1){ return $j; } (As per the comment of #Sreenath)
If the argument is more “complicated”, the function simplifies the argument (that is $i-1 in your example: The trivial case is $i<1, so making $i smaller makes the argument easier in some way) and then calls itself with the simpler argument and possibly some additional information, which is where the fact($i-1, $i*$j) call comes from.
So the recursive function fact here works doing the following thing:
fact(i, j) = fact(i-1, i*j)
= fact(i-2, (i-1)*(i*j)) = fact(i-3, (i-2)*(i-1)*i*j)
= ... = fact(1, (i-(i-1)) * (i-(i-2)) * ... * (i-1) * i * j)
= fact(0, (i-i) * (i-(i-1)) * (i-(i-2)) * ... * (i-1) * i * j)
= (i-i) * (i-(i-1)) * (i-(i-2)) * ... * (i-1) * i * j # Because 0<1
= i! * j
Now if you want just the factorial, you need to call fact with 1 as second argument, just as you do in return fact($n, 1);.
function factorial($n){
if($n==0){ # The trivial case
return 1;
}
# Every other case is "complicated": call a specialized function.
return fact($n, 1);
}
function fact($i, $j){
# Helper function: returns i!*j, doing a recursive calculation.
if($i<1){ # The trivial case
return j; # i!*j for i<1 is just j
}
else { # The "complicated" case:
return fact(
$i-1, # Simplify the argument
$i*$j # Pass my current state down
); # And call myself with the simpler argument and the internal state.
}
}
# Test it: This should return 5!=120
echo factorial(5);

What does this line of Actionscript do?

I'm looking at the as3delaunay library and most of the code is clear to me. This part is not, however (note the line that I put preceded with an arrow):
public function circles():Vector.<Circle>
{
var circles:Vector.<Circle> = new Vector.<Circle>();
for each (var site:Site in _sites)
{
var radius:Number = 0;
var nearestEdge:Edge = site.nearestEdge();
=======>> !nearestEdge.isPartOfConvexHull() && (radius = nearestEdge.sitesDistance() * 0.5);
circles.push(new Circle(site.x, site.y, radius));
}
return circles;
}
For reference, isPartOfConvexHull() is found in Edge.as and looks like this:
internal function isPartOfConvexHull():Boolean
{
return (_leftVertex == null || _rightVertex == null);
}
What does !nearestEdge.isPartOfConvexHull() do? Does that mean that the radius = nearestEdge.sitesDistance() * 0.5 only executes if false is returned from the call to isPartOfConvexHull()? Does that stop execution of any other code?
It is equivalent to:
if (!nearestEdge.isPartOfConvexHull()) {
radius = nearestEdge.sitesDistance() * 0.5;
}
In the following line:
var b:Boolean = expression1 && expression2;
expression2 will not be evaluated if expression1 is false because we already know the final result: b = false.
Now in the following line:
expression1 && expression2;
The same thing happens except the fact that we are not assigning the result to a variable.
And this is exactly what happens in the line you are asking about where !nearestEdge.isPartOfConvexHull() is the first expression and (radius = nearestEdge.sitesDistance() * 0.5) is the second expression.
To extends #sch answer with some explanations (I didn't knew if editing answer to almost double it was ok).
This is based on lazy execution of the interpreter. If (!nearestEdge.isPartOfConvexHull()) is False then there's no need to execute the second part of the AND statement to know it'll be False, then it's left unexecuted. If it's true the evaluation of the complete statement is needed (and then done) to tell wether or not this boolean is True. So this is equivalent to an if statement.
TMHO this is bad code since it's to much condensed and hard to understand.

is number between 2 values

What's the simplest way in actionscript to find if number is between -20 and +20, and return a true/false? I can see there's a number validator but I see it involves firing and catching events, which I think maybe overkill for the simple test I'm trying to do here.
Simplest way would be comparing the number with both values and logical combine the results:
return num > -20 && num < 20;
You may use >= or <= to include the values if needed.
You can make that into a nice function:
function isBetween(num:Number, lowerBound:Number, upperBound:Number):Boolean {
return num > lowerBound && num < upperBound;
}
Just write a function, conceptually like this:
protected function validatateNumbers(value:Number):Boolean{
if((value > -20) && (value <20)){
return true;
}
return false;
}
Then call the function whenever you want to validate your input.