I've often run into this situation where I have a complicated if statement with a lot of inner if statements and a lot of repeated code. It essentially boils down to I have a bunch of cases and certain code blocks that execute based on those statements, but if a different condition is true then I want those code blocks to execute in different conditions. Here's a generic example:
if (condition) {
if (conditionA) {
codeBlockW;
}
else if (conditionB) {
codeBlockX;
}
else if (conditionC) {
codeBlockY;
}
else if (conditionD) {
codeBlockZ;
}
}
else {
if (conditionA) {
codeBlockZ;
}
else if (conditionB) {
codeBlockY;
}
else if (conditionC) {
codeBlockX;
}
else if (conditionD) {
codeBlockW;
}
}
if ((condition AND conditionA) OR (!condition AND conditionD)) { codeBlockW }
else if ((condition AND conditionB) OR (!condition AND conditionC)) { codeBlockX }
else if ((condition AND conditionC) OR (!condition AND conditionB)) { codeBlockY }
else if ((condition AND conditionD) OR (!condition AND conditionA)) { codeBlockZ }
You can shorten it a little bit like this maybe, I don't imagine any shorter way...
Related
I'd like to use a filter function that may return an Err result, and bubble it up to the containing function:
mycoll.into_iter()
.filter(|el| {
if el == "bad" {
Err(MyError)
} else {
Ok(el < "foo")
}
})
I found a good explanation on how to handle this type of case when it comes to map() (using .collect::<Result<...>>()): How do I stop iteration and return an error when Iterator::map returns a Result::Err? but I can't get a similar solution to work for filter().
What's the idiomatic solution here?
I'd probably suggest using filter_map. Your example would look like:
mycoll.into_iter()
.filter_map(|el| {
if el == "bad" {
Some(Err(MyError))
} else if el < "foo" {
Some(Ok(el))
} else {
None
}
})
I am trying to write a stages inside another stage based on if condition. I am not able to come up with a solution. Can anyone guide on this
stages {
stage('Example') {
steps {
script {
if(!(fileExists("c:/test.txt")))
{
echo "Inside if"
stage('1') {
echo "stage1"
}
stage('2') {
echo "stage2"
}
}
else
{
stage('else stage') {
echo "else stage1"
}
}
}
}
}
}
This worked for me.
when {
expression
{
return !(fileExists("c:/test.txt"))
}
}
Looking at the example here at Message Controller for Pizza Example, if I want to populate Size or Kind based on some user input and make a call to the database, how would I do that?
So far as I know, there is not an easy way to populate the Enum at runtime.
It looks like this hasn't been implemented yet. I took a look inside https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/FormFlow/FormBuilder.cs and found this:
internal static void TypePaths(Type type, string path, List<string> paths)
{
if (type.IsClass)
{
if (type == typeof(string))
{
paths.Add(path);
}
else if (type.IsIEnumerable())
{
var elt = type.GetGenericElementType();
if (elt.IsEnum)
{
paths.Add(path);
}
else
{
// TODO: What to do about enumerations of things other than enums?
}
}
else
{
FieldPaths(type, path, paths);
}
}
else if (type.IsEnum)
{
paths.Add(path);
}
else if (type == typeof(bool))
{
paths.Add(path);
}
else if (type.IsIntegral())
{
paths.Add(path);
}
else if (type.IsDouble())
{
paths.Add(path);
}
else if (type.IsNullable() && type.IsValueType)
{
paths.Add(path);
}
else if (type == typeof(DateTime))
{
paths.Add(path);
}
}
Notice the TODO about enumerations other than enums.
Outside of the FormBuilder we can use PromptDialog.Choice which takes an IEnumerable<> of your options.
It is possible to chain dialogs together, so you may have to split your FormDialog into two with the PromptDialog in-between.
Alternatively take a fork of BotBuilder and implement the TODO!
I cannot figure out why this will not compile.
It says functions ends without a return statement, but when I add a return after the else, it still won't compile.
func (d Foo) primaryOptions() []string{
if(d.Line == 1){
return []string{"me", "my"}
}
else{
return []string{"mee", "myy"}
}
}
Go forces else to be on the same line as the if brace.. because of its "auto-semicolon-insertion" rules.
So it must be this:
if(d.Line == 1) {
return []string{"me", "my"}
} else { // <---------------------- this must be up here
return []string{"mee", "myy"}
}
Otherwise, the compiler inserts a semicolon for you:
if(d.Line == 1) {
return []string{"me", "my"}
}; // <---------------------------the compiler does this automatically if you put it below
else {
return []string{"mee", "myy"}
}
..hence your error. I will link to the relevant documentation shortly.
EDIT: Effective Go has information regarding this.
In this question I just want to ask for some ideas. I sometimes run into situations where I end up writing such if statements, however, I feel like there's a better way to write this as func1() is written in two places, I believe it should be only in one place.
if (cond1) {
func1();
} else {
if (cond2) {
func1();
} else {
func2();
}
}
How would you write this in a better, and of course readable way?
You've not said what language, but it looks C/Java/C# based...
if (cond1 || cond2) {
func1();
} else {
func2();
}
or similar should work?
How about:
if ((cond1)||(cond2))
{
func1();
}
else
{
func2();
}