I want to execute sql statements when the type isn't fruits. This is meant by (type != "fruits" && !isFruits)
I tried the following code, but it drops an error because an else is missing. I don't need an else.
if (type != "fruits" && !isFruits) {
con.executeFuture(query).flatMap(_ => {
conn.initQuery(query, isVegetables)
if (isVegetables) {
conn.VegetableCon.get.queryFuture(RunArgs)
} else {
conn.VegetableQueryCon.get.queryFuture(RunArgs)
}
})
}
I did some research and found ".filter", so I tried the following code:
con.executeFuture(query).filter(_ => type != ("ddl") && !isFruits).flatMap(_ => {
conn.initQuery(query, isVegetables)
if (isVegetables) {
conn.VegetableCon.get.queryFuture(RunArgs)
} else {
conn.VegetableQueryCon.get.queryFuture(RunArgs)
}
})
}
It drops me the error : "Future.filter predicate is not satisfied."
Now I don't know how to solve my problem. Do you have any idea about my mistakes?
Related
I'm trying to convert some code from mule 3 to mule 4 so I'm using dataweave 2. I keep getting errors from this code
keySets: if(payload.autoQuote !=null) payload.autoQuote map (autoQuote) -> {
(if(payload.autoQuote.personalAuto.vehicles != null) autoQuote.personalAuto.vehicles map ((vehicles,indexOfvehicle) ->{
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF' ,
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
} else {}))
} else {}
On the first line I'm getting
mismatched input '->' expecting 'else'
on the second line I'm getting
- missing EOF at 'payload'
and
- no viable alternative at input '('
on the first two '('
I believe keySets is a list and not a object, you should be using [] and not {} in your else statements. I edited some of the braces, and it is compiling fine for me, though not sure if it will work as expected for you.
keySets: if(payload.autoQuote !=null) (payload.autoQuote map (autoQuote) -> {
(if(payload.autoQuote.personalAuto.vehicles != null)
autoQuote.personalAuto.vehicles map ((vehicles,indexOfvehicle) -> {
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF',
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
}) else [])
}) else []
Give it a try and let me know if it worked.
This doesn't give me errors, though I'm not sure if it makes sense:
%dw 2.0
output application/json
---
{
keySets: if(payload.autoQuote !=null) (payload.autoQuote) map(autoQuote, i) -> {
( if(payload.autoQuote.personalAuto.vehicles != null) (autoQuote.personalAuto.vehicles) map (vehicles,indexOfvehicle) ->{
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF' ,
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
} else {})
} else {}
}
You should give an example of the input and the expected output.
Also I'm not sure why you don't use the autoQuoteparameter of the map inside of the map instead of payload.autoQuote...
Turns out I had the parentheses wrong. The first else was in the parentheeses after the map. It should look like this. Don't know if it works but it at least compiles.
keySets: if(payload.autoQuote !=null) payload.autoQuote map (autoQuote) -> {
(if(payload.autoQuote.personalAuto.vehicles != null) autoQuote.personalAuto.vehicles map ((vehicles,indexOfvehicle) ->{
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF' ,
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
}) else {})
} else {}
Im trying to filter an array of objects by property values
queryDatasource = datasource.filter {
if (!values.users.isEmpty && values.users.contains($0.User.User)) {
return true
}
if (!values.categories.isEmpty && values.categories.contains($0.Category.Category)) {
return true
}
if (!values.priorities.isEmpty && values.priorities.contains($0.Priority.Priority)) {
return true
}
if (!values.statuses.isEmpty && values.statuses.contains($0.Status.Status)) {
return true
}
return false
}
When i select only a user Sara, it works as expected, i only see results from Sara.
When i select a second user Mike, it still work as expected, only results by Mike and Sara.
Now, if i added a priority filter High, it returns all objects by Mike and Sara AND all objects with the priority High ignoring the users (i get a high priority object by the user Dave which is unintended)
In a nutshell, i want to filter by User AND Priority AND Category OR Status. if its in the users filter, it has to match
I'll arrive at the solution step-by-step, to show you how I got there.
Firstly, remove all the unnecessary !values.x.isEmpty checks. If a sequence is empty, contains(_:) will just always return false, so we don't need to worry about it. Also, I'd remove all of the unnecessary bracketing from the predicate:
queryDatasource = datasource.filter {
if values.users.contains($0.User.User) {
return true
}
if values.categories.contains($0.Category.Category) {
return true
}
if values.priorities.contains($0.Priority.Priority) {
return true
}
if values.statuses.contains($0.Status.Status) {
return true
}
return false
}
From here, we're in a better position to identify this as an or operation, so let's explicitly rewrite it as such:
queryDatasource = datasource.filter {
return values.users.contains($0.User.User)
|| values.categories.contains($0.Category.Category)
|| values.priorities.contains($0.Priority.Priority)
|| values.statuses.contains($0.Status.Status)
}
And if we can use or (||), clearly we can just switch to and (&&):
queryDatasource = datasource.filter {
return values.users.contains($0.User.User)
&& values.categories.contains($0.Category.Category)
&& values.priorities.contains($0.Priority.Priority)
&& values.statuses.contains($0.Status.Status)
}
Let's say I have a class called Number and it has a bool even and a bool big (ignore the meanings of them).
I want to build a function that receives 2 Numbers a and b and do a different thing for all possibilities of their bools:
func tooMuchCodeToWrite(a: Number, b: Number){
if(!a.even && !a.big && !b.even && !b.big){
//do something
}
else if(!a.even && !a.big && !b.even && b.big){
//do something
}
else if(!a.even && !a.big && b.even && !b.big){
//do something
}
else if(!a.even && !a.big && b.even && b.big){
//do something
}
// ...
else if(a.even && a.big && b.even && !b.big){
//do something
}
else{ //everyone is true in this case
//do something
}
}
is there a trick or a beautiful way to deal with this instead of writing all this code?
The typical solution to this kind of problem is a tuple-switch:
func tooMuchCodeToWrite(a: Number, b: Number){
switch (a.even, a.big, b.even, b.big) {
case (false, false, false, false): // ...
case (false, false, false, true): // ...
case (false, false, true, false): // ...
...
}
The nice thing about this approach is that it will check that you cover all the cases exactly once. The bad thing about this approach is there is nothing meaningful about each true and false, which can be confusing.
This approach also lends itself to grouping common "don't care" cases. For example, if you are always going to do the same thing if a is even, no matter all the other values, you can write:
case (true, _, _, _): // ...
While the position-dependence is still annoying, this has the benefit of expressing "don't care about these" very clearly.
You can make it a little more documenting this way:
switch (aeven: a.even, abig: a.big, beven: b.even, bbig: b.big) {
case (aeven: false, abig: false, beven: false, bbig: true): break
// ...
}
Swift won't force you to add the labels, but if you do add the labels, Swift will make sure that they're correct. This can get pretty verbose, so it's a trade-off if it helps or hurts.
You could nest the individual tests:
if ( !a.even ) {
if ( !a.big ) {
if ( !b.even ) {
if ( !b.big ) {
//do something
} else {
//do something
}
} else {
if ( !b.big ) {
//do something
} else {
//do something
}
}
} else {
...
No sure about more beautiful, but (slightly) more efficient.
How about:
if (!a.even) {
if (!a.big) {
if (!b.even) {
if (!b.big) {
//Do Stuff
}
else {
//Do Stuff
.....
}
else {
//if a.even
....
}
I'm following a tutorial on how to validate form fields, but it only demonstrates it for one field. How can I validate, and display errors for multiple fields?
I tried the following - but it always succeeds and does a redirect - no matter the errors:
def process() = {
if (patientName == "Joe") {
S.error("patientName", "Joe not allowed!")
}
if (birthdate == "22/22/2222") {
S.error("birthdate", "Invalid date!")
}
S.notice("Success! You entered Patient name: " + patientName); S.redirectTo("/")
}
Ha! I figured it out. Beautiful.
def process() = {
if (patientName == "Joe") {
S.error("Joe not allowed!")
}
if (birthdate == "22/22/2222") {
S.error("birthdate", "Invalid birthdate!")
}
S.errors match {
case Nil =>S.notice("Patient name: " + patientName); S.redirectTo("/")
case _ =>
}
}
I want to check multiple conditions in a string in C# but it throws error saying Cannot use && for string or boolean
if ((duStart.Trim() != "" && duStart.Trim() != null) &&(duEnd.Trim() != "" && duEnd.Trim() != null))
{
//do this
}
else
//do that
The code you've given compiles fine. Here's a short but complete program - I've changed the whitespace of your line of code, but that's all:
using System;
class Test
{
static void Main()
{
string duStart = "X";
string duEnd = "X";
if ((duStart.Trim() != "" && duStart.Trim() != null) &&
(duEnd.Trim() != "" && duEnd.Trim() != null))
{
Console.WriteLine("Yes");
}
}
}
Having said that:
If you're going to use the same value (the trimmed version of duStart, for example) multiple times, there seems little point in computing it twice. I'd have used extra local variables (trimmedStart, trimmedEnd) here
Trim never returns null, so those tests are pointless.
Using string.IsNullOrWhitespace is probably a better idea here. Why bother creating strings that you're never going to use?
you can simplify the condition by writing:
if( !string.IsNullOrEmpty(duStart.Trim()) && !string.isNullOrEmpty(duEnd.Trim()) )
{
}
Check for the Null first for duStart and duEnd. Then try Trim the string. Trim cannot be applied on a null value. So, below code block should work for you.
if ((duStart != null && duStart.Trim() != "") && (duEnd != null && duEnd.Trim() != ""))
{
//do this
}
else
{
//do that
}