How to translate a lambda expression to normal code? c# [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to use this code in .net Framework 2.0 but the problem is that labmdas(WHERE) can't be used in this version, is there a way?
var serializer = new JavaScriptSerializer();
retorno = serializer.Deserialize<RespuestaCvt>(respuesta);
var soloServidores = retorno.Rows.Where(x => listaEstados.Contains(x.EstadoId)).Where(x => listaTipos.Contains(x.TipoId)).ToList();
return soloServidores;

Since Lambda expressions were not available until C# 3 you will have to change the way you do the actual row filtering. Since you're returning List<> we can just go back to the pre-LINQ way of doing things:
var soloServidores = new List<TheRowType>();
foreach (var row in retorno.Rows)
{
if (listaEstados.Contains(row.EstadoId) && listaTipos.Contains(x.TopiId))
soloServidores.Add(row);
}
return soloServidores;
Of course you could do it the hard way and extract the lambda to a static method then implement a suitable Where(...) extension... but if you're going to try to emulate C# features from a later language version then you're usually better off migrating your project to that later version.

Related

Is there a way to use python and java in the same program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to use both python and java in the same program. Since the print() function of python is better, but java's int variable; is more efficient.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.

Use the value that a function returns as the name of a new Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Good I am trying to create a function that generates new Array, and trying to use a getter method that returns a name for example ArrayD5, ArrayD10 and use these as the name of the new arrays to generate.
I have tried to do this:
int length = 5;
String (SeatIbiza.nombres(length))[] = new String[length];
String nombres (int length) {
return "ArrayD"+length;
}
If you are looking for a way to declare something like e.g.
String "ArrayD" + 2
so that you have a variable
String ArrayD2
then this is not possible in Java. Dynamic naming of variables is not supported.
I don't think if that's possible. The variable names are required to be declared during compile time in JAVA and dynamic naming isn't supported... yet.
Correct me if I'm wrong as I'm still learning about JAVA

Is anyone still using Java? Can I have this translated please? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I was looking for how to detect if an Android Q device has Dark Mode enabled, and the only result I found was in Kotlin:
fun isDarkTheme(activity: Activity): Boolean {
return activity.resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}
IF it is correct, then can I please have it in Java? :)
Here is the java Version
private Boolean isDarkTheme(Activity activity) {
return (activity.getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
Java getResources() ->Kotlin resources
Java getConfiguration() ->Kotlin configuration
Java & ->Kotlin and
as you see it's simple Setters and Getters in koltin are accessed by property name
I realised there might be a little more going on here to someone who's completely unfamiliar with Kotlin, so just in case:
boolean isDarkTheme(Activity activity) {
return activity.getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}
all the get calls get converted to property syntax in Kotlin, so you can just access them like fields. The rest of the stuff just moves around a bit

Groovy remove value from collection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In groovy is there a specific way to remove a value from a collection. For example I have a list of form fields but two of them are hidden fields and I'm trying to figure out how to remove them from the collection. The two parameters I'm trying to remove are salesKey and topicSelection. Groovy newbie so code samples are most helpful
request.requestParameterMap.collect { key, value -> "$key: ${value[0].string}" }.join("\n")
key.remove("salesKey")
key.remove("topicSelection")
I think you could use findAll:
request.requestParameterMap.findAll { key, value ->
!( key in ["salesKey", "topicSelection"] )
}
Check out this answer.
Also, depending on your specific aims, there are a couple of other ways to remove a pair, including dropWhile (which is more or less iterating over your data struct) and minus (which isn't so much removing a pair as creating a new structure without the specified pair). Official doc here.

Looking for an elegant way to convert a copy book in Swift in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm doing a conversion from a COBOL copybook to a SWIFT message. First I'm parsing the copybook with a copybookHelper class and then I write a giant set of if statements to test all the fields and populate my SWIFT string. Is there a less cumbersome way to implement it (without the gigantic set of ifs)?
SWIFT = Society for Worldwide Interbank Financial Telecommunication
Would an Enum of the different message types help? With an enumeration in place, parsing a particular type and populating the swift string could be as simple as:
SwiftMessage msg = Enum.valueOf(SwiftMessage.class, "MT001");
msg.populateString(/* parameters? */);
An example of how you would write the enum implementation:
enum SwiftMessage {
MT001,
MT002 { void populateString() { /* override implementation */ } },
...
void populateString() { /* default implementation */ }
}
It's hard to be more specific without any details of what you are trying to do.

Categories

Resources