How to import keys and values into variables from a map? - dictionary

I need to export the keys and values from map in Dart. In PHP I use for that purpose function extract():
$array=array('one'=>1,'two'=>2,'three'=>3);
extract($array);
But I don't know, how to do the same thing in Dart. Is there any special function or construct for it? Or how can I reach the same result with forEach()? Is there anybody, who could help me?
Update 1: My target is from the map like this, but much complicated (example taken from Dart up and running):
var gifts = {
// Keys Values
'first' : 'partridge',
'second' : 'turtledoves',
'fifth' : 'golden rings'
};
in which I want to rewrite the result by any simple function or forEach() loop:
// something like gifts.forEach()? but how?
into variables:
assert(first=='partridge');
assert(second=='turtledoves');
assert(fifth=='golden rings');
//wow, rewritten! The code can continue and use just the variables:
querySelector('#animal').text=first;

Dart is a statically declared language. You cannot create new variable names at runtime, from a map or in any other way.
In order to refer to a variable, it must already be declared. That means that even if you could introduce new variables, you could not have any references to it in your existing code.
If the variables are already declared, and you just want to assign the values to them, you can use the mirror system, but I wouldn't recommend that. It is much simpler to just access the values directly in the map.

Related

Firebase Function Data Type of "data" in onCall function

I thought i had a simple question but it seems to be somewhat harder and the documentation does not help alot.
What exactly is the type of 'data' in "functions.https.onCall((data, context) {});"
I thought it varies between a simple value, a map or a list.
But even if i call the function with a map object and try to delete a key from it, it fails because it isn't a map.
It also can't be immutable and casting it to a map doesn't work too.
So whatever it is, i just want to remove a key from it. Does anyone know the datatype so i am able to find the correct function?
As #Delwinn stated out, the 'data' object seems to be a 'json object' (if this is a type in typescript) and not a map.
to delete a value from this object, a plain line like
delete json[key]
will do the job.
And yes, 'delete' is written like an operator and not a function.

Assigning to copy of a map seems to be modifying the original

I have a variable decodedToken (type: struct), and I access one of its values called "Claims" through a type assertion:
claims := decodedToken.Claims.(jwt.MapClaims)
I then loop through the claims (type: map[string]interface{}), and modify its values in place:
for key := range claims {
claims[key] = "modified"+key
}
Hence, I expect that the original decodedToken variable would be unchanged, since I have just performed an operation on the claims variable. However, decodedToken is also changed to my modified value.
My question is why is this so, and how do I leave the decodedToken untouched?
Since claims is a reference type, like a map or slice.
The solution is make a deep copy of any referenced data. Unfortunately there are no universal way to make a deep copy of any map in Go. So you should make your own.
Or more practical way to do your job is making a new object(variable) to contain the modified decodedToken.
Also, it's not good to iterated a map and modify its value in a same statement.

Unable to understand extension function in Kotlin Android?

I am not able to understand extension function and use it in my project. Can anyone guide me here please?
From the docs - “Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done via special declarations called extensions.”
To understand this in an easy way, let’s consider the below example:
First things first.
Write 10 and then put a dot(.) after it and then try to write addTwoNumbers().
You’ll be getting errors at this stage as there is no property named addTwoNumbers() for an integer.
Now, write this method:
fun Int.addTwoNumbers(y: Int): Int = this.plus(y) //“this” corresponds to the integer number. (In this example, “this” refers to 10).
Notice how we are using Int.addTwoNumbers().
Let’s try to follow the same thing again:
Write 10.
Put a dot(.) after it.
Try to write addTwoNumbers().
And this time you’ll notice, it’s appearing as if this is the property of integer.
Check the below code:
fun main() {
val sum = 10.addTwoNumbers(20) //here “this” will be assigned “10” and “y” will be assigned “20”
println("sum: $sum")
}
This will print sum: 30 in the console.
This phenomena is known as “Extension Function”.

storing global variables in the window object Javascript

In my application I have multiple variables that need to be accessed globally from the different functions of my script:
var a=1,b=2,c, ...;
Where "c" undefined at the beginning and takes value produced by some of the functions during scripts execution.
In order to declare them from within my "main" function I'm trying to use window object:
window.a=1;
window.b=2;
window.c;
This works, however I'm not sure if such approach is correct.
And is there is a way to avoid creation multiple window objects for each variable and combine them into more compact structure? Something like:
window.a=1,.b=2,.c;//---of course-this doesn't work
I was asking this question today too. It used to be the way when I last wrote javascript (mid-90s) and still seems to be the case see this resource

Trying to make a function generic, but getting hung up

I have a variable in a package (rec in this case) that needs to be set when called from package 3, but it's private. Previously the function set_true only set rec to true, so it wasn't a big deal. But I have another package that does the same processing (I'm giving a simple example, but my literal case is more complex), so I thought, well I could pass in the variable I want modified, and let it get changed. Is the only way to set rec in the below layout, to create a second function in package one, that calls set_true with rec as the parameter? I would like to avoid having to keep creating additional functions to handle the local variables. I can't move the variable to public (spec) as I am trying to follow convention and this "type" of variable isn't public anywhere else, and I don't want anyone to be able to just set it on their own (I want functions to have to set). I don't want to have to create a second function named for example set_local_true, and creating an overloaded function set_true, with no parameters, that calls set_true(value => rec) just seems deceptive, does anyone have any better suggestions with the limitations I have?
My two requirements:
Can't make the local variable public.
Be able to use the function to calculate something both externally and internally.
package one is
procedure set_true(value : out Boolean);
end one;
package body one is
rec : Boolean;
begin
procedure set_true(value : out Boolean)
begin
value := true;
end set_true;
end one;
package body two is
local_rec : Boolean;
begin
procedure call_function is
begin
one.set_true(value => local_rec);
end call_function;
end two;
package body three is
begin
procedure call_function is
begin
one.set_true(value => <PACKAGE ONE'S REC))
end call_function;
end three;
EDIT: Or perhaps, what would be a better naming convention for the functions to specify that they are modifying the variable that is local to that package? Set_Local_True again is deceptive cause if you call it from package 3, you're not setting your local true, you're setting package one's local to true....
First off, this is very silly code. I'll assume it is shorthand for something else. But as presented, I can assure you that your clients can set their own booleans themselves without you writing a routine to do it for them. In fact, they can do it better. For the remainder of this answer, I'll assume you aren't acutally writing variables to set booleans for people, but rather doing something of actual use. If not, ignore the rest of this answer and just delete your silly routines.
Secondly, if you are creating a routine with a single out parameter, then unless the object happens to be very large, you should probably make it a function instead. That will allow your clients to use functional programming if they chose. The way you have it, the poor coder has to stop and create a special variable just to call your routine, even if they only want to do it once.
Thirdly, rather than using a unique set routine for each state, I generally prefer to pass in the requested state.
function Set_Frobnost (New_State : boolean := true) return boolean;
If the state is really and truly boolean (no possible third state in the future), then it is debateable. However, it can be a big advantage to your client if they might already have to store the state in a variable (or loop through it).
Your edit about naming shows me you are on the right track.
You should do one of two things here.
Find the higher-level concept controlled by that variable, and name the "setter" routine after that.
Get the hell out of the way and put the flag variable in the pacakge spec.
If you have to access private variables, you might to do it in a child package.
package One is
procedure Foo (X : Boolean);
private
One_Private : Boolean;
end One;
and then
package body One.Two is
procedure Bar is
One.Foo (One.One_Private);
end Bar;
end One.Two;
Elements in the "private" part of a package are like "protected" entities in C++/Java. Truly private variables (only in package body) are not accessible from anywhere else.

Resources