I have a script in Arduino that'll get a letter, and to make things as short a possible I'd like to use a string combined with another variable to get the variable name I need to fill in into the function.
I'd like this because I have a function mySwitch.send(Int, 24). For the first Int variable I need to to send a number that is dependent on the letter I sent, and on the current value of A_stat, I defined these values in variables A_aan, A_uit, B_aan, B_uit, etc.
For example for the letter A I need to fill in the variable A_aan if a_stat == 0. If a_stat == 1 it needs to fill in A_off.
For B I need to fill in the variable B_aan if b_stat == 0, and I need to fill in the variable name B_uit on the place of Int if b_stat == 1.
Since the value of the variable vary at runtime, the only solution in my opinion is to use some functions.
For instance, if you want to get #_aan, where # is the value in a variable, you can use the following function:
int get_aan(char carat)
{
switch(carat)
{
case 'A':
return A_aan;
case 'B':
return B_aan;
...
}
return AN_INVALID_VALUE_YOU_DEFINE;
}
Remember to define an invalid value somewhere.
If you have to set that value, you can write a similar set function:
void set_aan(char carat, char value)
{
switch(carat)
{
case 'A':
A_aan = value;
break;
case 'B':
B_aan = value;
break;
...
}
}
You can also include some flags. For instance, if you wanted to get variable A_aan when A_stat = 0 and A_off when A_stat != 0, and repeat this for every variable, just modify the first function in:
int get_the_val(char carat)
{
switch(carat)
{
case 'A':
if (A_stat)
return A_off;
else
return A_aan;
case 'B':
if (B_stat)
return B_off;
else
return B_aan;
...
}
return AN_INVALID_VALUE_YOU_DEFINE;
}
Related
I have a function in R that recursively builds a list of lists of varying depth. The output node may be be
node<-list(right=(0))
or
node<-list(right=list(right=0))
In Rcpp I would like to build a recursively deconstruct the list and return the integer member, 0 in this case.
My problem is checking if node has a named member right
library(Rcpp)
cppFunction(
'
int predict(List node){
if(node["right"]){
return predict(node["right"]);
}
else{
return node;
}
}
}
'
)
I've looked at Dynamic Wrapping to check type and use switch/case, but it doesn't seem to like named lists.
Trying to check for existence of a named value using a subset, e.g. if(node["right"]), will trigger the following error:
Error in predict_bad(node) : Not compatible with requested type: [type=list; target=logical].
To search a List or *Vector for a named element, use .containsElementNamed("name") member function.
For example, we have:
#include<Rcpp.h>
// [[Rcpp::export]]
Rcpp::List predict_list(Rcpp::List node){
// Check if name is present
if(node.containsElementNamed("right")) {
return predict_list(node["right"]);
}
return node;
}
Notice, here we are returning an Rcpp::List, e.g.
node1 = list(right = list(right = 0))
predict_list(node1)
# [[1]]
# [1] 0
To obtain only an integer, we must first subset the list and cast to the appropriate type. The second component, if we are tricky enough, we can allow Rcpp automagic to handle the conversion. (Thanks to Qiang for revealing the prior answer does not have to be positionally limited.)
#include<Rcpp.h>
// [[Rcpp::export]]
int predict_node_val(Rcpp::List node) {
// Check if name is present
if(node.containsElementNamed("right")) {
// Check if element isn't a list.
switch(TYPEOF(node["right"])) {
case REALSXP:
case INTSXP:
return node["right"];
default: // Keep going down the tree
return predict_node_val(node["right"]);
}
}
// Quiet compiler by providing a final output case
return -1;
}
Output:
node1 = list(right = list(right = 0))
node2 = list(right = 0)
predict_node_val(node1)
# [1] 0
predict_node_val(node2)
# [1] 0
There are a few assumptions made above... The first is we will always have a list architecture based on typing. The second is the value we want to retrieve is always listed as "right". The third
You can just get the names and check if there is an element right.
The following code should work:
library(Rcpp)
cppFunction(
'
int predict(List node) {
std::vector<std::string> list_names = node.names();
if (std::find(list_names.begin(), list_names.end(), "right") != list_names.end()) {
if (TYPEOF(node["right"]) == REALSXP) {
return node["right"];
} else {
return predict(node["right"]);
}
} else {
return -1;
}
}
'
)
The results
> node<-list(right=(0))
> predict(node)
[1] 0
> node<-list(right=list(right=0))
> predict(node)
[1] 0
I'm trying to get the first K elements from a sorted map by the following piece of code:
//return top rank k elements
public static LinkedHashMap<String,Double> getTopRank(int i){
//store top k elements
LinkedHashMap<String, Double> result=new LinkedHashMap<>();
int count=0;
//use the static rankMap in the class
rankMap.each {key,value->
result.put(key, value);
count++;
if(count>=i){
println "Time to return"
return result;
}
}
//in case the loop does not work
return result;
}
What I expect is that when the result Map already has a size of i elements, the method will return, giving me a i-size sorted map. Note that rankMap stores the elements in a certain order I want, and its size is far bigger than int i I pass to the method.
And I'm calling the method by
LinkedHashMap<String,Double> content=getTopRank(outputSize);
But unexpectedly finally the content had the size of rankMap rather than i! And in the console I saw hundreds of Time to return lines. The line return result was executed again and again until it reached the end of rankMap.
I'm pretty sure that the line getTopRank(outputSize) was not in a loop. Then it seems strange to me why this method can return multiple times without ending. Is it caused by my putting return statement in the closure?
Please advise or tell me how this is true in Groovy. One step further, how can I get only first k elements from a sorted map then?
You misunderstood key concepts of Groovy.
The only way to finish the each() execution before reaching the end, is to throw an exception. If your want to exit the loop conditionally, use standard loop types like for or while:
int count=0
def result = [:]
for( def e in rankMap ){
count++
result[ e.key ] = e.value
if( i <= count ) return result
}
The method itself is not returning. each is a method which receives a closure. Closures have their own returning context, which is not tied to the method who invoked them, thus, the loop is not broken.
I'd like to suggest getting a range from the map's entrySet and collecting the resulting entries:
def getTopRank(int i) {
rankMap
.entrySet()
.toList()[0..<i]
.collectEntries()
}
rankMap = [
'Beatles' : 'The White Album',
'Pink Floyd' : 'The Dark Side of the Moon',
'Rolling Stones' : 'Sticky Fingers',
'The Doors' : 'Morrison Hotel',
'Bob Dylan' : 'Bob Dylan'
]
assert getTopRank(2) == [
'Beatles' : 'The White Album',
'Pink Floyd' : 'The Dark Side of the Moon']
assert getTopRank(4) == [
'Beatles' : 'The White Album',
'Pink Floyd' : 'The Dark Side of the Moon',
'Rolling Stones' : 'Sticky Fingers',
'The Doors' : 'Morrison Hotel',]
public static LinkedHashMap<String,Double> getTopRank(int i){
rankMap.take(i)
}
http://www.groovy-lang.org/gdk.html
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Map.html#take(int)
In Groovy, the return statement is optional, allowing you to write methods like:
def add(a, b) {
a + b
}
...which adds a and b and returns the result to the caller.
However, I'm wondering what the semantics are when the method has multiple return "statements". For example, in Java you might have:
String append(String a, String b) {
if (a == null) {
return b;
}
return a + b;
}
This could (hypothetically) be translated to Groovy like:
def append(a, b) {
if (! a) {
b
}
a + b
}
However, in this case, how does Groovy know that b inside of the if statement should be returned? Or does it not? I assume that Groovy cannot simply treat any statement whose result is unused as a return, correct? Are there any clearly defined semantics for how the "optional return" feature behaves in this case?
The page you linked (rather tersely) describes the exact semantics:
Notice that the return statement is optional at the end of methods.
So the b in that if block would never be returned unless you explicitly returned it. In practice this means that the return value will be the result of the last statement evaluated, so if your example were
def append(a, b) {
if (!a) { b }
else { a + b }
}
Then the result would be b if !a is true and a + b otherwise.
The result of a call to a void function is null, so if the example were
def append(a,b) {
if (!a) { b }
else { a + b }
println "debug: $a $b"
}
Then append would always return null.
My own rule of thumb for this is to always use an explicit return statement if the method or closure contains more than one statement. I think relying on the implicit return statement in more complex methods is dangerous since if anyone adds a line to the end of the method they will change the return value even though they most likely didn't intend to.
I have the following code, which works, but I'm wondering if there is a "groovier" way of doing this:
/**
* 10 digit - #-######-##-#
* 13 digit - ###-#-######-##-#
* */
private formatISBN(String isbn) {
if (isbn?.length() == 10) {
def part1 = isbn.substring(0, 1)
def part2 = isbn.substring(1, 7)
def part3 = isbn.substring(7, 9)
def part4 = isbn.substring(9, 10)
return "${part1}-${part2}-${part3}-${part4}"
} else if (isbn?.length() == 13) {
def part1 = isbn.substring(0, 3)
def part2 = isbn.substring(3, 4)
def part3 = isbn.substring(4, 10)
def part4 = isbn.substring(10, 12)
def part5 = isbn.substring(12, 13)
return "${part1}-${part2}-${part3}-${part4}-${part5}"
} else {
return isbn
}
}
You could first use the [] string operator to get the substrings instead of substring and drop the intermediate variables. For example in the case for length == 10:
"${isbn[0]}-${isbn[1..6]}-${isbn[7..8]}-${isbn[9]}"
Now, there is a bit of repetition there. You can get instead first get all the isbn segments and then .join them with '-':
[isbn[0], isbn[1..6], isbn[7..8], isbn[9]].join('-')
And, even further, instead of referencing isbn every time, you can make a list of the ranges you want to get and then get them all the same time using collect:
[0, 1..6, 7..8, 9].collect { isbn[it] }.join('-')
If you're going for code golfing, you can also do:
('-'+isbn)[1, 0, 2..7, 0, 8..9, 0, 10]
I'll leave it to you to figure out how that works, but i guess it's probably not a good idea to leave that on production code, unless you want to surprise future maintainers hehe.
Also, notice that the format when length == 13 is the same as for length == 10 but with a different prefix, you can then reuse the same function in that case. The whole function (with a couple of tests) would be:
/**
* 10 digit - #-######-##-#
* 13 digit - ###-#-######-##-#
**/
def formatIsbn(isbn) {
switch (isbn?.length()) {
case 10: return [0, 1..6, 7..8, 9].collect { isbn[it] }.join('-')
case 13: return isbn.take(3) + '-' + formatIsbn(isbn.drop(3))
default: return isbn
}
}
assert formatIsbn('abcdefghij') == 'a-bcdefg-hi-j'
assert formatIsbn('abcdefghijklm') == 'abc-d-efghij-kl-m'
Now, i think there are some bad smells in that code. Can isbn be null? At least to me, this doesn't look like a function that needs to bother about the nullity of its argument, or at least that's not clear by reading its name (it should be called something like formatIsbnOrNull instead if both ISBN strings and null values are accepted). If null values are not valid, then let it blow up with a NullPointerException when accessing isbn.length() so the caller know they have passed a wrong argument, instead of silently returning the same null.
The same goes for the return ISBN at the end. Is it expected for that function to receive a string that's neither 10 nor 13 characters long? If not, better throw new IllegalArgumentException() and let the caller know they have called it wrongly.
Finally, i'm not sure if this is the most "readable" solution. Another possible solution is having a string for the format, like '###-#-######-##-#' and then replace the #s by the isbn characters. I think it might be more self-documenting:
def formatIsbn(isbn) {
def format = [
10: '#-######-##-#',
13: '###-#-######-##-#'
][isbn.length()]
def n = 0
format.replaceAll(/#/) { isbn[n++] }
}
Consider adding the method to the String class, as shown here. Note that this answer is a spin on a clever suggestion in epidemian's answer (re: collect).
Note:
This code augments String with asIsbn().
The range [0..2] does not need the call to asIsbn(), but the symmetry of using collect twice is irresistable.
Groovy returns the last expression in if/else, so 'return' is not necessary
/**
* 10 digit - #-######-##-#
* 13 digit - ###-#-######-##-#
**/
String.metaClass.asIsbn = { ->
if (delegate.length() == 10) {
[0, 1..6, 7..8, 9].collect { delegate[it] }.join('-')
} else if (delegate.length() == 13) {
[0..2, 3..12].collect { delegate[it].asIsbn() }.join('-')
} else {
delegate
}
}
assert "abcdefghij".asIsbn() == 'a-bcdefg-hi-j'
assert "abcdefghijklm".asIsbn() == 'abc-d-efghij-kl-m'
assert "def".asIsbn() == "def"
String s = null
assert s?.asIsbn() == null
I would try using Regex... I think it's pretty much readable if you know how to use regex, and it's javascript inspired syntax in groovy is pretty cool also.
One more thing: it's pretty clear, looking at the capture groups, what your string looks like for the desired formatting.
private formatISBN(String isbn) {
if (isbn?.length() == 10) {
m = isbn =~ /(\d{1})(\d{6})(\d{2})(\d{1})/
return "${m[0][1]}-${m[0][2]}-${m[0][3]}-${m[0][4]}"
} else if (isbn?.length() == 13) {
m = isbn =~ /(\d{3})(\d{1})(\d{6})(\d{2})(\d{1})/
return "${m[0][1]}-${m[0][2]}-${m[0][3]}-${m[0][4]}-${m[0][5]}"
} else {
return isbn
}
}
Btw, #epidemian suggestion using backreferences is great! I think the code would look like:
private formatISBN(String isbn) {
if (isbn?.length() == 10) {
return isbn.replaceAll(/(\d{1})(\d{6})(\d{2})(\d{1})/, '$1-$2-$3-$4')
} else if (isbn?.length() == 13) {
return isbn.replaceAll(/(\d{3})(\d{1})(\d{6})(\d{2})(\d{1})/, '$1-$2-$3-$4-$5')
} else {
return isbn
}
}
Dunno if I like this any better. I'd make the position map a static final, too.
private isbnify(String isbn) {
def dashesAt = [ 10: [[0,1], [1,7], [7,9], [9,10]],
13: [[0,3], [3,4], [4,10], [10,12], [12,13]]]
def dashes = dashesAt[isbn?.length()]
(dashes == null) ? isbn
: dashes.collect { isbn.substring(*it) }.join('-')
}
Ranges make for a bit less clutter, IMO:
private isbnify3(String isbn) {
def dashesAt = [ 10: [0, 1..6, 7..8, 9],
13: [0..2, 3, 4..9, 10..11, 12]]
def dashes = dashesAt[isbn?.length()]
dashes == null ? isbn : dashes.collect { isbn[it] }.join("-")
}
With an inject-with-two-accumulators it should be easy to do a list-of-dash-positions version, too.
This should be a comment to #everton, but I don't have the 50 reputation needed to do that yet. So this answer is really just a suggested variation on #everton's answer.
One less regex by making the first 3 digits optional. The downside is having to remove a leading '-' if the ISBN is 10 characters. (I also prefer \d over \d{1}.)
private formatISBN(String isbn) {
String result = isbn.replaceAll(/^(\d{3})?(\d)(\d{6})(\d{2})(\d)$/,
'$1-$2-$3-$4-$5')
if (result) {
return result.startsWith('-') ? result[1..-1] : result
} else {
return isbn // return value unchanged, pattern didn't match
}
}
println formatISBN('1234567890')
println formatISBN('9991234567890')
println formatISBN('123456789') // test an ISBN that's too short
println formatISBN('12345678901234') // test an ISBN that's too long
I don't understand exactly how to use a function that returns a boolean. I know what it is, but I can't figure out how to make it work in my program. I'm trying to say that if my variable "selection" is any letter beween 'A' and 'I' then it is valid and can continue on to the next function which is called calcExchangeAmt(amtExchanged, selection). If it is false I want it to ask the user if they want to repeat the program and if they agree to repeat. I want it to clear the screen and restart to the main function. How do I make my program work as intended?
This is my bool function:
bool isSelectionValid(char selection, char yesNo, double amtExchanged)
{
bool validData;
validData = true;
if ((selection >= 'a' && selection <= 'i') ||
(selection >= 'A' && selection <= 'I'))
{
validData = calcExchangeAmt (amtExchanged, selection);
}
else(validData == false);
{
cout << "Do you wish to continue? (Y for Yes / N for No)";
cin >> yesNo;
}
do
{
main();
}
while ((yesNo =='y')||(yesNo == 'Y'));
{
system("cls");
}
return 0;
}
I get this warning:
warning C4800: 'double' : forcing value to bool 'true' or 'false' (performance warning)
A bool function should return true or false. I'm guessing your warning is caused by the fact that you're declaring validData as bool, but then assign it a different value (returned by calcExchangeAmt function). That value is getting converted from its value type (double) to boolean (true or false).
So, your IsSelectionValid method should just return true if selection is valid, or false if it's not. Then whatever code needs to know that information can proceed accordingly.
I don't know much C++, so forgive me for syntax problems my code is bound to have, but your code should look something like this:
bool isSelectionValid(char selection)
{
return (selection >= 'a' && selection <= 'i') || (selection >= 'A' && selection <= 'I');
}
void myCallingFunction(double amtExchanged, char selection)
{
bool isSelectionValid = isSelectionValid(selection);
if(isSelectionValid)
{
double exchangeAmt = calcExchangeAmt (amtExchanged, selection);
}
else
{
cout<<"Do you wish to continue? (Y for Yes / N for No)";
cin>>yesNo;
if((yesNo =='y')||(yesNo == 'Y'))
{
main(); // or whatever code starts another attempt
}
}
This code is seriously confusing and very non-C++ like. We normally expect main() to be the function that drives things and calls other functions, not to have it called from some other place. We generally avoid do unless there is a compelling reason (and I don't see one here). I think it's highly unlikely that a function called calcExchangeAmt returns true or false; I suspect it actually returns a number that you should be doing something else with (showing to the user?).
With all this going on, trying to explain your actual compiler error messages is of limited value. Your code is all inside out and backwards. Anna Lear's answer seems like a better starting point if it makes sense to you.
The type of 0 is not bool; true or false is bool. It is telling you that 0 is a double, but it is forcing it to a boolean type.