I have the following structure of a dictionary in Erlang:
Key: {element_name, a, element_type, type_1}
Value: [list].
Dictionary: (({element_name, a, element_type, type_1},[List]), ({element_name, b, element_type, type_2},[List])).
I would like to update a certain key-value pair and insert some new data into the 'key' tuple (not into 'value' list):
1. Value_list = dict:fetch({element_name, a, element_type, _}, Dict).
2. Dict2 = dict:erase ({element_name, a, element_type, _}, Dict).
3. Dict3 = dict:store ({element_name, a, element_type, New_type}, Value_list, Dict2).
The problem is that at line 1 Erlang says that variable "_" is unbound.
It seems that I cannot fetch a value by providing only a part of the key if the key is a tuple. Is this true?
Is it actually possible to update a key in a dictionary?
Is there any shorter way to do this instead of doing 1,2 and 3?
dict doesn't support what you want to do. you will have to know the key, erase the old key/value pair, and store a new one.
take a look at ets. you can use ets:match to find keys that match your spec. you'll still have to delete the old key/value pair and insert a new one.
If you insist on updating the Key in the dictionary without deleting it and later storing a new value against it, i suggest that you first convert your Dict into a list by this: dict:to_list/1. Consider this piece of code:
Fun_to_match_key = fun({{element_name, a, element_type, _} = Key,Value})->
%% do some stuff here with the Key and value and assuming
%% this fun returns the new Key-Value pair you want
New_Key = update_my_key(Key),
New_Value = update_my_value_if_need_to(Value),
{New_Key,New_Value};
(Any)-> Any
end,
%% Then in one operation, you convert the dict into a list, apply the
%% fun above in a list comprehension and convert the list back to a dict
New_dict = dict:from_list([Fun_to_match_key(Key_Value_Pair) || Key_Value_Pair <- dict:to_list(Old_Dict)]),
New_dict.
Converting the dict into a list will give you a proplist() which is much easier to manipulate either Key or value. You could use any method say, recursion with several clauses in which you pattern match the nature of Key you want to manipulate, in the above example i have chosen to use a fun within a list comprehension.
That should do the trick!
Related
Swiftui dictionaries have the feature that the value returned by using key access is always of type "optional". For example, a dictionary that has type String keys and type String values is tricky to access because each returned value is of type optional.
An obvious need is to assign x=myDictionary[key] where you are trying to get the String of the dictionary "value" into the String variable x.
Well this is tricky because the String value is always returned as an Optional String, usually identified as type String?.
So how is it possible to convert the String?-type value returned by the dictionary access into a plain String-type that can be assigned to a plain String-type variable?
I guess the problem is that there is no way to know for sure that there exists a dictionary value for the key. The key used to access the dictionary could be anything so somehow you have to deal with that.
As described in #jnpdx answer to this SO question (How do you assign a String?-type object to a String-type variable?), there are at least three ways to convert a String? to a String:
import SwiftUI
var x: Double? = 6.0
var a = 2.0
if x != nil {
a = x!
}
if let b = x {
a = x!
}
a = x ?? 0.0
Two key concepts:
Check the optional to see if it is nil
if the optional is not equal to nil, then go ahead
In the first method above, "if x != nil" explicitly checks to make sure x is not nil be fore the closure is executed.
In the second method above, "if let a = b" will execute the closure as long as b is not equal to nil.
In the third method above, the "nil-coalescing" operator ?? is employed. If x=nil, then the default value after ?? is assigned to a.
The above code will run in a playground.
Besides the three methods above, there is at least one other method using "guard let" but I am uncertain of the syntax.
I believe that the three above methods also apply to variables other than String? and String.
I iterate thru items of a dictionary "var_dict".
Then as I iterate in a for loop, I need to update the dictionary.
I understand that is not possible and that triggers the runtime error I experienced.
My question is, do I need to create a different dictionary to store data? As is now, I am trying to use same dictionary with different keys.
I know the problem is related to iteration thru the key and values of a dictionary and attempt to change it. I want to know if the best option in this case if to create a separate dictionary.
for k, v in var_dict.items():
match = str(match)
match = match.strip("[]")
match = match.strip("''")
result = [index for index, value in enumerate(v) if match in value]
result = str(result)
result = result.strip("[]")
result = result.strip("'")
#====> IF I print(var_dict), at this point I have no error *********
if result == "0":
#It means a match between interface on RP PSE2 model was found; Interface position is on PSE2 architecture
print (f'PSE-2 Line cards:{v} Interfaces on PSE2:{entry} Interface PortID:{port_id}')
port_id = int(port_id)
print(port_id)
if port_id >= 19:
#print(f'interface:{entry} portID={port_id} CPU_POS={port_cpu_pos} REPLICATION=YES')
if_info = [entry,'PSE2=YES',port_id,port_cpu_pos,'REPLICATION=YES']
var_dict['IF_PSE2'].append(if_info)
#===> *** This is the point that if i attempt to print var_dict, I get the Error during olist(): dictionary changed size during iteration
else:
#print(f'interface:{entry},portID={port_id} CPU_POS={port_cpu_pos} REPLICATION=NO')
if_info = [entry,'PSE2=YES',port_id,port_cpu_pos,'REPLICATION=NO']
var_dict['IF_PSE2'].append(if_info)
else:
#it means the interface is on single PSE. No replication is applicable. Just check threshold between incoming and outgoing rate.
if_info = [entry,'PSE2=NO',int(port_id),port_cpu_pos,'REPLICATION=NO']
var_dict['IF_PSE1'].append(if_info)
I did a shallow copy and that allowed me to iterate a dictionary copy and make modifications to the original dictionary. Problem solved. Thanks.
(...)
temp_var_dict = var_dict.copy()
for k, v in temp_var_dict.items():
(...)
I am trying to create an empty map, that will be then populated within a for loop. Not sure how to proceed in Rascal. For testing purpose, I tried:
rascal>map[int, list[int]] x;
ok
Though, when I try to populate "x" using:
rascal>x += (1, [1,2,3])
>>>>>>>;
>>>>>>>;
^ Parse error here
I got a parse error.
To start, it would be best to assign it an initial value. You don't have to do this at the console, but this is required if you declare the variable inside a script. Also, if you are going to use +=, it has to already have an assigned value.
rascal>map[int,list[int]] x = ( );
map[int, list[int]]: ()
Then, when you are adding items into the map, the key and the value are separated by a :, not by a ,, so you want something like this instead:
rascal>x += ( 1 : [1,2,3]);
map[int, list[int]]: (1:[1,2,3])
rascal>x[1];
list[int]: [1,2,3]
An easier way to do this is to use similar notation to the lookup shown just above:
rascal>x[1] = [1,2,3];
map[int, list[int]]: (1:[1,2,3])
Generally, if you are just setting the value for one key, or are assigning keys inside a loop, x[key] = value is better, += is better if you are adding two existing maps together and saving the result into one of them.
I also like this solution sometimes, where you instead of joining maps just update the value of a certain key:
m = ();
for (...whatever...) {
m[key]?[] += [1,2,3];
}
In this code, when the key is not yet present in the map, then it starts with the [] empty list and then concatenates [1,2,3] to it, or if the key is present already, let's say it's already at [1,2,3], then this will create [1,2,3,1,2,3] at the specific key in the map.
I'm messing around a bit with F# and I'm not quite sure if I'm doing this correctly. In C# this could be done with an IDictionary or something similar.
type School() =
member val Roster = Map.empty with get, set
member this.add(grade: int, studentName: string) =
match this.Roster.ContainsKey(grade) with
| true -> // Can I do something like this.Roster.[grade].Insert([studentName])?
| false -> this.Roster <- this.Roster.Add(grade, [studentName])
Is there a way to insert into the map if it contains a specified key or am I just using the wrong collection in this case?
The F# Map type is a mapping from keys to values just like ordinary .NET Dictionary, except that it is immutable.
If I understand your aim correctly, you're trying to keep a list of students for each grade. The type in that case is a map from integers to lists of names, i.e. Map<int, string list>.
The Add operation on the map actually either adds or replaces an element, so I think that's the operation you want in the false case. In the true case, you need to get the current list, append the new student and then replace the existing record. One way to do this is to write something like:
type School() =
member val Roster = Map.empty with get, set
member this.Add(grade: int, studentName: string) =
// Try to get the current list of students for a given 'grade'
let studentsOpt = this.Roster.TryFind(grade)
// If the result was 'None', then use empty list as the default
let students = defaultArg studentsOpt []
// Create a new list with the new student at the front
let newStudents = studentName::students
// Create & save map with new/replaced mapping for 'grade'
this.Roster <- this.Roster.Add(grade, newStudents)
This is not thread-safe (because calling Add concurrently might not update the map properly). However, you can access school.Roster at any time, iterate over it (or share references to it) safely, because it is an immutable structure. However, if you do not care about that, then using standard Dictionary would be perfectly fine too - depends on your actual use case.
I have two lists. lst contains ViewState Data i.e All the records of the gridview & second lstRank contains the list of integer(i.e ID) for only those records which are marked as checked (i.e Gridview contains a columns for checkbox). Now i want to update the lst bool status depending upon integer ID of lstRank. How it can be achieved by lambda expression
List<Tuple<int, string, bool>> lst = (List<Tuple<int, string,bool>>)ViewState["gvData"];
List<int> lstRank = gvDetails.Rows.OfType<GridViewRow>().Where(s => ((CheckBox)s.FindControl("chkSelect")).Checked)
.Select(s => Convert.ToInt32(((Label)s.FindControl("lblRankCD")).Text)).ToList();
Your question isn't clear, but I'm guessing you want to change lst's contents so that the boolean values are true if the int exists in lstRank, or false if not?
Obviously, tuples are immutable, so if you want to change one of the values, you would have to generate new tuple instances. It's not clear what you mean when you say you specifically want to do this with a lambda expression, but I assume you probably mean that you don't want a solution that involves an explicit loop. So how about this:
lst = lst.Select(oldValues =>
Tuple.Create(oldValues.Item1,
oldValues.Item2,
lstRank.Contains(oldValues.Item3))).ToList();
If lstRank is large, you might want to optimize by first building a HashSet out of it, since you'll be doing a lot of Contains calls.