Null-aware operator when traversing a nested map [duplicate] - dart

This question already has answers here:
Null-aware operator with Maps
(7 answers)
Closed 3 years ago.
How do you check for nulls when accessing second level map elements?
E.g.
var clientName = item['client']['name'];
will throw an exception if item does not contain client
I'd like something like
item['client']?['name']
or
item['client']?.['name']
But that won't compile.
Surely I can do item['client'] twice or introduce a local var for it but that feels subpar.

I think that this is similar to a question I asked some time ago.
Basically, this would be the solution for your case:
(item['client'] ?? const {})['name']
This makes use of the null-aware ?? operator which just returns an empty map in the case that 'client' is not present in item.

With putIfAbsent, you can use ? operator.
item.putIfAbsent("client", (){})
?.putIfAbsent("name", (){});
https://api.dartlang.org/stable/2.3.1/dart-core/Map/putIfAbsent.html

Related

not able to complete the program [duplicate]

This question already has answers here:
"The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()
(3 answers)
Closed 1 year ago.
I'm new to dart and having a hard time figuring out things. so, I just need some help completing the program below. I have no idea where I'm going wrong.
All I'm getting is an error related to null safety
question :-
Write a program to obtain a number N and increment its value by 1 if the number is divisible by 4 otherwise decrement its value by 1.
import 'dart:io';
void main(){
String? input = stdin.readLineSync();
int number = int.parse(input);
}
This is all that came to my mind, I know the logic, but I'm stuck at getting the user input and converting it.
As the comment suggest, in this answer is explained how Dart will handle if stdin.readLineSync does give a null value. So this should work, notice the ! at the end of stdin.readLineSync.
import 'dart:io';
void main() {
var input = stdin.readLineSync()!;
var number = int.parse(input);
}

What is the "||=" construct in Ruby? [duplicate]

This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 8 years ago.
In my readings about structuring methods with options hashes for ruby, I've run into a coding "motif" a few times that I can't explain. Since I don't know what it's called, I'm having a lot of difficulty looking it up to learn more about it.
Here's an example:
1 def example_method(input1, options={})
2
3 default_options = {
4 :otherwise => "blue",
5 :be_nil => nil
6 }
7
8 options[:be_nil] ||= options[:otherwise]
9
10 # other code goes down here
11 end
So above, on line 8, you can see what I'm talking about. From what I can put together, the line of code acts similarly to a tertiary operator. Under one condition, it sets a variable to one value... under a different condition, it sets the variable to a different value. In this case, however, the code updates a hash that's stored in the "options" variable. Is that a correct assumption? Furthermore, what is this style/operator/functionality called?
THis is a conditional assignment. The variable on the left will be assigned a value if it is nil or false. This is the short for of saying:
unless options[:be_nil]
options[:be_nil] = options[:otherwise]
end

Table elements executing a function [duplicate]

This question already has answers here:
Search for an item in a Lua list
(12 answers)
Lua find a key from a value
(3 answers)
Closed 9 years ago.
I have this table:
maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
How can I make a script so if 1702169 (for example) is picked from the table, it prints ''That's the number''?
The easiest way to do what (i think) you want is with pairs() function. This is a stateless iterator which you can read more about here: http://www.lua.org/pil/7.3.html
If you simply want to scan through the entire table and see if it contains a value, then you can use this simple code:
local maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
local picked = 1702169
for i, v in pairs(maps) do
if v == picked then
print("That's the number")
break
end
end
The above code will iterate through the whole table where i is the key and v is the value of the table[key]=value pairs.
I am slightly unclear about your end goal, but you could create this into a function and/or modify it to your actual needs. Feel free to update your original post with more information and I can provide you with a more specific answer.

F# how to extended the generic array type? [duplicate]

This question already has answers here:
How to define a type extension for T[] in F#?
(2 answers)
Closed 9 years ago.
Following this question I wonder how (or if) i can extend the generic F# Array Type.
I could do this:
type System.Array with
member a.Last = a.GetValue(a.Length - 1)
but as Tomas mentioned it is non generic. Next I tried this but it does not work:
type Microsoft.FSharp.Collections.Array with // Error: Array is not defined
member a.Last = a.[a.Length - 1]
In the F# scource I found this namespace, but it does not work either:
type Microsoft.FSharp.Primitives.Basics.Array with // Error: Array is not defined
member a.Last = a.[a.Length - 1]
This is a bit confusing - but I was recently looking for something in the F# specification and came across this:
type 'T ``[]`` with
member a.Last = a.[a.Length - 1]
[| 1 .. 10 |].Last
The double-backtick encoding is normally used to turn reserved keywords into valid F# identifiers (e.g. if you want to have a property that has a space in the name, or is named let). Here, it probably means that the compiler needs to treat [] as an ordinary type "name" rather than as a special syntax for arrays.

how to compare if float variable value is infinite? [duplicate]

This question already has an answer here:
Check for inf - Objective-C
(1 answer)
Closed 9 years ago.
debugger says me float value is inf
I just see in debug window section of xcode is this
floatVar1=(float)inf
inf means infinite ?
so how can I compare it ?
something like this:
if (floatVar1==INFITINE){
[self doBlah];
}
To test whether a value is either positive infinity or negative infinity:
if (isinf(floatVar1)) …
To test only whether a value is positive infinity:
if (floatVar1 == INFINITY) …
In either case, use #include <math.h>.
I guess this should work, though it's not objective-c put plain c:
if(isinf(floatVar1)) { ... }
Also, you need to include math.h. For more info see http://www.cplusplus.com/reference/cmath/isinf/

Resources