Is there an easy way to check if either all variables are all true together, or all false together? - boolean

If I have a bunch of variables that either all need to have been initialised or not initialised, like:
int handle = -1; // unitialised
void* ptrToSomethingImportant = null;
bool bHasBeenInitialised = false;
Then:
bool bAreAllUnitialised = handles != -1 && ptrToSomethingImportant != null && bHasBeenInitialised == false;
So this tells me if all variables are initialised, but I can't just flip that bool to test if NONE have been initialised. Do I need to rewrite the test again with them all negated?

Related

Dart Function Returning Set<bool> Instead of bool

I'm building a mobile client for a blog with a paid CMS that shows a number of articles all the time, plus a rotating article each week, and I've built a simple function to get the current week of the year and return a Boolean value if an article should be displayed this week:
bool displayArticle(StoredArticle article){
if (article.week < 0) {
return true;
}
DateTime now = DateTime.now();
DateTime janFirst = DateTime(now.year, 1, 1);
int weekNum = (now.difference(janFirst).inDays / 7).ceil();
if(article.week == weekNum || article.week == (weekNum - 1)){
return true;
}
return false;
}
I then use this function to filter a list of all the articles like so:
List<StoredArticle> articlessToDisplay = storedArticleObjs.where((article) => {
displayArticle(article)
}).toList();
This is all enclosed within a Stateful Widget.
However, using the function like this throws an error at the function call that The return type 'Set<bool>' isn't a 'bool', as required by the closure's context.
My first thought was that there was an issue with the displayArticle() function being a static member function to a stateful widget, but moving the function directly into the closure as follows did not impact the error.
List<StoredArticle> articlessToDisplay = storedArticleObjs.where((article) => {
if (article.week < 0) {
return true;
}
DateTime now = DateTime.now();
DateTime janFirst = DateTime(now.year, 1, 1);
int weekNum = (now.difference(janFirst).inDays / 7).ceil();
if(article.week == weekNum || article.week == (weekNum - 1)){
return true;
}
return false;
}).toList();
Next I thought it might be that the early return was confusing the inspector to belive it was returning multiple values, so I converted it to a single return function as follows, but that did nothing either.
bool displayArticle(StoredArticle article){
bool shouldDisplay = false;
if (article.week < 0) {
shouldDisplay = true;
}
DateTime now = DateTime.now();
DateTime janFirst = DateTime(now.year, 1, 1);
int weekNum = (now.difference(janFirst).inDays / 7).ceil();
if(article.week == weekNum || article.week == (weekNum - 1)){
shouldDisplay = true;
}
return shouldDisplay;
The only resources on similar issues have been referring to functions that return Future<T> instead of T. Putting aside the fact that my issue is with a Set<T> rather than a Future<T>, those errors have all been thrown by the return statement or the function definition rather than the function call.
I haven't been able to find any resources relating to this specific issue, though as I'm new to Flutter and Dart I suppose could be missing some specific terminology.
That being said, returning a set of the return type does not make any sense to me. Is this a quirk of implementation in a Stateful Widget?
The problem is that you have a few too many braces, and {"A"} is set-syntax in Dart.
You have:
storedArticleObjs.where((article) => {
displayArticle(article)
}).
Change that to:
storedArticleObjs.where((article) =>
displayArticle(article)
).
Note that the => function syntax doesn't use braces.
You could even probably write it more compactly using tear-offs like so:
storedArticleObjs.where(displayArticle).

Can object properties be used as function parameters?

I have a class with several Boolean properties:
class aTest {
String name;
bool twoGroups;
bool continuous;
bool parametric;
bool covariates;
bool paired;
aTest(
{this.name,
this.twoGroups,
this.continuous,
this.parametric,
this.covariates,
this.paired});
} //end aTest
I also have a list with instances of aTest:
List<aTest> testList = [
aTest(
name: "independent samples t-test",
twoGroups: true,
continuous: true,
parametric: true,
covariates: false,
paired: false),
//followed by a bunch of similar objects
]
Elsewhere in my app I filter the List<aTest> with procedures like:
void isParametric(bool value) {
List<aTest> newList = [];
for (aTest test in testList) {
if (test.parametric == value || test.parametric == null) {
newList.add(test);
}
}
testList = newList;
}
void isTwoGroups(bool value) {
List<aTest> newList = [];
for (aTest test in testList) {
if (test.twoGroups == value || test.twoGroups == null) {
newList.add(test);
}
}
testList = newList;
}
(I don't know whether this is the best way to filter and remove objects from the List.) All that differs among these procedures is an object property, e.g., test.parametric and test.twoGroups in the code above.
Is there a way to refactor the code? Something like
void filter (aBooleanPropertyGoesHere, bool value)
You can simply filter with the lists with one liner by where method.
var parametricList = testList.where((i) => (i.continuous && i.parametric == null)).toList()
var twoGroupsList = testList.where((i) => (test.twoGroups == value || test.twoGroups == null)).toList()
Something like this https://dartpad.dev/79a7e9aa5882af745b6ff2cb55815921
For detailed explanation check out the documentation

Converting boolean integer to string value in javascript

I am reading a boolean integer value from database (0 or 1).
Is there an simple solution to convert a boolean int to boolean string?
When I was saving the value to my database I was able convert the string to an int using a javascript ternary operator.
var i = result ? 1 : 0;
Is it possible to preform the opposite?
My current work-around is:
function boolIntToString(i) {
if (i == 1) {
return true;
}
else {
return false;
}
}
The expression i != 0 evaluates to boolean false if i = 0, or true otherwise, so to get true or false, you could simply write:
var theBool = i != 0;
If you want a string, you can call .toString() on that boolean result. Wrapping this into your function, you get:
function boolIntToString(i) {
return (i != 0).toString();
}
console.log(boolIntToString(1));
Note that your own function returns a boolean, not a string.

Reset Boolean values?

I am trying to get my password verifier to loop until a correct response is give. It's looping right now, but its not resetting the boolean values after the first iteration, resulting in a valid response the second time no matter which password. How do I make the loop check for every value, or reset the table every time?
boolean atLeast8 = false;
boolean oneLower = false;
boolean oneUpper = false;
boolean oneNumber = false;
boolean oneSpecial = false;
boolean noAnd = false;
boolean noEnd = false;
do
{
System.out.println("Enter your password. " + '\n');
password = input.nextLine();
System.out.println();
for(int i=0; i<password.length(); i++) //Checks for values througout the length of the string
{
char c = password.charAt(i);
if(password.length() >= 8)
atLeast8 = true;
if(Character.isLowerCase(c))
oneLower = true;
if(Character.isUpperCase(c))
oneUpper = true;
if(Character.isDigit(c))
oneNumber = true;
if(c=='!' || c=='#' || c=='#' || c=='$' || c=='%' || c=='^' || c=='&' || c=='&' || c=='*')
oneSpecial = true;
if (password.indexOf("and") < 0)
noAnd = true;
if (password.indexOf("end") < 0)
noEnd = true;
}
if(atLeast8 && oneLower && oneUpper && oneNumber && oneSpecial && noAnd && noEnd) //Does the string contain any true values?
{
System.out.println("Valid.");
}
else
{
System.out.println("Invalid!"); //Does the string contain any false values?
}
if(!atLeast8)
{
System.out.println("Must be at least 8 characters long.");
}
if(!oneLower)
{
System.out.println("Must contain one lower case letter.");
}
if(!oneUpper)
{
System.out.println("Must contain one upper case letter.");
}
if(!oneNumber)
{
System.out.println("Must contain one numeric digit.");
}
if(!oneSpecial)
{
System.out.println("Must contain one special character.");
}
if(!noAnd)
{
System.out.println("Must not contain the word 'and'.");
}
if(!noEnd)
{
System.out.println("Must not contain the word 'end'.");
}
}while (atLeast8 == false || oneLower == false || oneUpper == false || oneNumber == false || oneSpecial == false || noAnd == false || noEnd == false);
}
}
Can't you just put
atLeast8 = false;
oneLower = false;
oneUpper = false;
oneNumber = false;
oneSpecial = false;
noAnd = false;
noEnd = false;
at the start of the loop (somewhere between the "do {" and the "for")
Or, is that being too simplistic?

STM32 atoi and strtol sometimes missing first 2 digits

I am reading a value sent over RS485 which is the value of an encoder I first check if it has returned an E character (the encoder is reporting an error) and if not then do the following
*position = atoi( buffer );
// Also tried *position = (s32) strtol(buffer,NULL,10);
The value in the buffer is 4033536 and position gets set to 33536 this does not happen every time in this function probably 1 in 1000 times maybe although I am not counting. Setting the program counter back and doing the line again if has failed returns the same result but starting the debugger again causes the value to convert correctly.
I am using keil uvision 4, its a custom board using an stm32f103vet6 and the stm32f10 library V2.0.1 This one has really got me stumped never come across something like this before any help would be much appreciated.
Thanks
As no one knows I will just post what I ended up doing which was to write my own function for convertion not ideal but it worked.
bool cdec2s32(char* text, s32 *destination)
{
s32 tempResult = 0;
char currentChar;
u8 numDigits = 0;
bool negative = FALSE;
bool warning = FALSE;
if(*text == '-')
{
negative = TRUE;
text++;
}
while(*text != 0x00 && *text != '\r') //while current character not null or carridge return
{
numDigits++;
if(*text >= '0' && *text <= '9')
{
currentChar = *text;
currentChar -= '0';
if((warning && ((currentChar > 7 && !negative) || currentChar > 8 && negative )) || numDigits > 10) // Check number not too large
{
tempResult = 2147483647;
if(negative)
tempResult *= -1;
*destination = tempResult;
return FALSE;
}
tempResult *= 10;
tempResult += currentChar;
text++;
if(numDigits >= 9)
{
if(tempResult >= 214748364)
{
warning = TRUE; //Need to check next digit as close to limit
}
}
}
else if(*text == '.' || *text == ',')
{
break;
}
else
return FALSE;
}
if(negative)
tempResult *= -1;
*destination = tempResult;
return TRUE;
}