Why doesn't skipeWhile display the correct results? - rxjs

SkipWhile() continues to skip elements as long as the entry condition is true. When the condition becomes false, all remaining elements will return.
What I'm trying to do are two types of conditions:
It must enter when loading === false and when arrIds.length > 0
It must enter when loading === false and when arrIds.length === 0
My Example:
combineLatest([this.peopleSelectorsService.loading, this.peopleSelectorsService.allIds])
.pipe(skipWhile((observables) => !observables[0] && observables[1].length === 0))
.subscribe((observables) => {
});
RESULT:

your first emission is [true, []]
your skip condition can be rewritten as:
skipWhile(([loading, items]) => !loading && !items.length)
in english: skip while not loading and there are not items, in your first emission case this evaluates to false && true which is false.
skipWhile stops skipping after one false result, so the first emission stops it from evaluating anymore.
you either need to adjust your logic or use a different operator. can't say for sure as you haven't outlined your expected results. what I SUSPECT you want is:
skipWhile(([loading, items]) => loading || !items.length)
which will skip the first 2 emissions and emit the third.

Related

How do the two R codes, which should do the same thing (at least in my head), differ?

In order to replicate this code, you will need these packages:
Tidyverse, Nycflights13
I am basically trying to understand why two pieces of code, which in my head should do the same thing, don't do the same thing.
I am currently learning R for data science from R for Data Science by Garrett Grolemund & Hadley Wickham, and I've gotten to a point which the code begins to confuse me quite a bit, which hopefully is normal! I will write down the two pieces of code which confuse me in why they don't do the same thing!
filter(flights, dest == c("HOU","IAH"))
#and
filter(flights, dest == "HOU" | dest == "IAH")
I expected both of these codes to show the same amount of rows but the first one shows 4658 rows (the wrong amount), where as the second one shows 9313 (the right amount).
What I wanted to do is to shorten the code by using (filter(flights, dest == c("HOU","IAH"))) instead of (filter(flights, dest == "HOU" | dest == "IAH"))
but it yields different results, which gravely confuses me!
Please give me your advice, I am a newbie!
Because filter(flights, dest == c("HOU","IAH")) is the same as
filter(flights, dest == c("HOU","IAH"))
flights$dest = c("HOU","IAH")
* # For demonstation purposes, I'm assuming the flights dataset has has 4 rows, as:
c("HOU","CPH","IAH","EDI") == c("HOU","IAH")
Now, a vector of 4 elements cannot possible be the same as a vector of 2 elements. And this performs an element-wise comparison, "HOU" == "HOU", "CPH" == "IAH", "IAH" == ???. So R "helps" us by repeating the shorter vector. The output is thus:
> c("HOU","CPH","IAH","EDI") == c("HOU","IAH")
[1] TRUE FALSE FALSE FALSE
Try extending with a 5th element:
> c("HOU","CPH","IAH","EDI", "LDN") == c("HOU","IAH")
[1] TRUE FALSE FALSE FALSE FALSE
Warning message:
In c("HOU", "CPH", "IAH", "EDI", "LDN") == c("HOU", "IAH") :
longer object length is not a multiple of shorter object length
and you broke it.
So the 1st line only makes sense if and only if flights has 2 rows, whereupon an element-wise comparison can be performed.
However, what you might be looking for is the %in% operator:
> c("HOU","CPH","IAH","EDI") %in% c("HOU","IAH")
[1] TRUE FALSE TRUE FALSE
> c("HOU","CPH","IAH","EDI","LDN") %in% c("HOU","IAH")
[1] TRUE FALSE TRUE FALSE FALSE
which can be expanded to filter(flights, dest %in% c("HOU","IAH")) and luckily works for vectors of any length.

What is the difference between skipWhile and filter in RxJS?

As title suggest, both function seems to have similar effect and return emit nothing when predicate does not match. It looks like skipWhile is the reverse of filter ?
As #cartant says
skipWhile "... emits all further source items as soon as the condition becomes false"
Note that skipWhile takes a predicate (expression that returns a boolean).
In addition there is also skipUntil, which this takes an observable and values will be skipped until that observable emits anything.
So sometimes the following can be useful (pan is a hammerjs event btw.):
// When event.deltaX reaches 20 emit true to indicate a threshold is reached
const thresholdReached$ = pan$.pipe(map(e => Math.abs(e.deltaX) > 20 ), first(v => v), shareReplay(1));
// Emit all events, but skip them until the first time that thresholdReached$ emits
const panMove$ = pan$.pipe(skipUntil(thresholdReached$));
Important to realize that the observable passed to skipUntil can emit any value to trigger the skipping to stop. It doesn't have to be a 'truthy' value.
So if you have var b = new BehaviorSubject(false) then skipUntil(b) will immediately stop skipping and you will quickly get very confused!

Multiple conditions in while controller

Is it possible to add two conditions in while controller? My two conditions are Complete ="True" and Results >200.
I tried using it by setting Complete = False and Results=0 in user defined variables and used it in while controller as follows:
${__javaScript("${Complete}" != "true")} && ${__javaScript((parseInt(${Results}) >90)}.
But it is looping indefinitely. Please help.
Try the following condition (working for me):
${__jexl3("${Complete}" == "False" && ${Results} >= 0,)}
where Complete - False & Results - 0.
For above values, condition will be evaluated to true, hence executes the children of the While Controller.
Note: Please change the conditions == & >= symbols and values False && 0 as per your requirements.
You must reset the values inside the While Controller, to make the condition evaluated to false, otherwise you will struck in infinite loop.
References:
https://jmeter.apache.org/usermanual/component_reference.html#While_Controller
https://www.blazemeter.com/blog/using-while-controller-jmeter
For Multiple Condition in While Loop using Groovey Function for '&&' and '||' for the same field.
${__groovy(!(vars.get('ocrstatus_1').equals('500') || vars.get('ocrstatus_1').equals('1000')) ,)}

Test spec failing with my prime number checker

I am doing some coding challenges at code wars and ran into this one asking me to make a method that takes a number and determines if it is a prime number or not. If it is prime the method should return "true" if the number is not prime it should return "false".
The method passes every introductory test and each number I can think to throw at it but continually kicks back two tests as incorrect. At this point I'm curious if I am not understanding something about the testing process?
This is my code:
def isPrime(num)
counter=2 #is incremented with every time until loop loops
until counter>999999999999999 do
if num.abs <2
return false
elsif num.abs % counter == 0 && num.abs!=counter
return false
else
return true
end#if
counter+=1
end#
end```
and this is the feed back that code wars is sending back to me
isPrime
Should have isPrime defined.
Test Passed
Should return false for numbers less than 2.
Test Passed: Value == false
Test Passed: Value == false
Test Passed: Value == false
Should return false for non-prime numbers.
Test Passed: Value == false
Test Passed: Value == false
Expected: false, instead got: true # THESE ARE THE TESTS THAT FAIL
Expected: false, instead got: true # THESE ARE THE TESTS THAT FAIL
Should return true for prime numbers.
Test Passed: Value == true
Test Passed: Value == true
Test Passed: Value == true
Test Passed: Value == true
Test Passed: Value == true
Also I checked here page for help on the algorithm.
Any help is greatly appreciated.
There are a number of issues here. The biggest is with the if statement you have inside the loop.
if num.abs <2
return false
elsif num.abs % counter == 0 && num.abs!=counter
return false
else
return true
end
There is no condition under which this if statement will not terminate the loop and return either true or false in the first iteration. This will prevent counter from ever being incremented.
The next issue is in your loop control. You have
until counter>999999999999999 do
...
counter+=1
end
In this case it would be better to stop at the sqrt(num) rather than some large number. For better performance you probably should instead use as the loop control something like
until counter*counter > num do
This will avoid multiple sqrt calculations. You could precompute the sqrt instead, something like
sqrt_num = num.sqrt
until counter > sqrt_num do
(I don't know Ruby so I may have the syntax wrong, but I think you get the point). If you do this, though, be sure num is not negative before hand.
If you exit from the loop never having found a factor for num you know the number is prime.
There are 2 problems in your code
Why are you taking the num.abs? If the tester provides negative numbers, it doesn't fail. By formal definition negative numbers aren't prime.
The program can stop at Math.sqrt(n) https://stackoverflow.com/a/5811176/3804420

Handling multi-touch with corona/lua

I am using multi-touch for a simple game. I have three buttons and if one button is pressed, the corresponding object is removed and if the correct two buttons are 'multitouched' then that corresponding object is removed. My problem is that the multitouch handler triggers two events, therefore removing two objects when it should only remove one. Here's a paraphrased version of my code:
function buttonHandler(event)
-- Store the users input
if (event.phase == "began") then
mt[event.target.id] == true
end
-- When the buttons are released, give the users input a value.
if (event.phase == "ended") then
if mt.One == true and mt.Two == true then number = 3
elseif mt.One == true and mt.Two == false then number = 1
elseif mt.One == false and mt.Two == true then number = 2
elseif mt.One == false and mt.Two == false then number = 0
end
-- Compare the user input with the object and remove it if there's a match.
if object.number == number then
object:removeSelf()
object = nil
end
-- Reset the user input.
mt.One = false
mt.Two = false
mt.Three = false
end
What is happening is that if the user selects the number 3 by pressing both the 1 and 2 buttons, the comparison code is being triggered twice, once for each 'ended' phase. So if the the two objects on the screen are a 3 and a 1, then the three will be removed and for the 1, it acts as if the user guessed wrong. On the other hand, if there are two '3' objects on the screen, both objects will be removed with one 'guess'.
I thought about moving the comparison code outside of the function, but I would be in the same situation. There will still be two events fired for one guess.
(If my actual code is needed, I'd be more than happy to provide it. This just saves a lot of space. Also, if this is a basic answer and I just don't see it, a link to reading material would be greatly appreciated. I don't mind work, just don't know where to start.)

Resources