Constructing a map from a vector in Clojure - clojure

I have a vector that looks like this:
["Config" "{}" "Auth" "{}" "Other" "{}"]
I'd like to take each key value pair and turn it into the following map:
{"Config" "{}", "Auth" "{}", "Other" "{}"}
How can I do this with Clojure? Is there a built in function that does this?

Use apply to apply the map constructor of desired type to the vector, ie :
(apply hash-map ["Config" "{}" "Auth" "{}" "Other" "{}"])
edit
According to this answer you can get different map types depending on the way you evaluate {}, so use the map constructor suitable to your needs.
edit
Looking at this the different object types returned by literal {} appears to be a bug.

Related

How to convert an array of data to a list of widgets with expand or fold in flutter/dart?

Really frustrated about not solving it myself but finally gave up.
I have an array of fields, each being a map with title and value. I want to iterate through it and create a list of text widgets along with some padding between them. I tried the following:
fields.expand((field)=>
[ Text(field['title']),
Padding(padding: const EdgeInsets.only(bottom: 4.0))
]
).toList()
But this gives the following error:
type 'List<dynamic>' is not a subtype of type 'List<Widget>'
I tried adding <Widget> after expand but then I get:
type 'MappedListIterable<Map<String, String>, dynamic>' is not a subtype of type 'List<Widget>'
I also tried adding <Widget> after expand( but then I get:
type '<Widget>(dynamic) => List<Widget>' is not a subtype of type '(Map<String, String>) => Iterable<dynamic>' of 'f'
Not sure what else to do. How the heck can I force it to realize this is a list of widgets?
I also tried using fold but got similar typing issues.
I think what you're looking for is .map(), but there's a cleaner way to do it :
items: [
for(var field in fields)
...[
Text(field['title']),
Padding(padding: const EdgeInsets.only(bottom: 4.0)),
]
]
Make sure your minimum SDK version is 2.6.0 or above in the pubspec.yaml
As #christopher-moore noted, adding <Widget> after expand does work. The reason I thought it was not working was that I kept getting the error, but for a different place where I still had the same statement but without that addition.
As I said in my comment, one of your solutions appears to should have work, but since it didn't I'm providing an alternate solution.
You can use a for-each loop to "manually" create an output List that you can pass to your Column widget. Ex.
List<Widget> columnList = List();
for(Map field in fields) {
columnList.add(Text(field['title']);
columnList.add(Padding(padding: const EdgeInsets.only(bottom: 4.0)));
}

Iterate over an existing map against a list of tuples scala

I have a list of tuples that I must change the values for in a map that contains those tuples. So if I have a list such as List((0,2), (0,3)) with a map that looks like this: Map((0,2) => List(1,2,3), (0,3) => List(1,2)), I need to access the matching map tuples with the tuples listed in the list, then remove a number from the mapping.
So in the example above, if I wanted to remove 2 from the mapping, I would get Map((0,2) => List(1,3), (0,3) => List(1)).
Design wise, I was thinking of pattern matching the map, but I've read some answers that said that may not be the best way. The tough part for me is that it has to be immutable, so I was thinking of pattern matching the list, getting the map value, change the value, then recreate the map and recursively call the function again. What do you think of this implementation?
This could be a way to remove 2 from your Map:
val newMap = oldMap.mapValues(list => list.filter(_ != 2))
Or more generally:
def filterInMap(element: Int, oldMap: Map[(Int,Int),List[Int]]) =
oldMap.mapValues(list => list.filter(_ != element))
This way there's no need to mutate anything at all. mapValues transforms just the values of your Map and returns a copy of the original without mutating it at all. filter then gets the job done by only allowing elements that don't match the element we would like to remove.
Bonus: even more generally:
def filterInMap[A](element: A, oldMap: Map[(A,A),List[A]]) =
oldMap.mapValues(list => list.filter(_ != element))

Clojure get the value inside a map

How do I get openid.claimed_id or any other field in a map like this?
:openid.claimed_id won't work.
{"openid.response_nonce" "2015-07-25T09:31:45ZXrcrR0Lk35St5ESZQ0tg40PbBXU=", "openid.identity" "http://steamcommunity.com/openid/id/xxx", "openid.ns" "http://specs.openid.net/auth/2.0", "openid.op_endpoint" "https://steamcommunity.com/openid/login", "openid.mode" "id_res", "openid.sig" "zuiyNzf/QLP9Ci/czElIo1Z3nE0=", "openid.signed" "signed,op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle", "openid.assoc_handle" "1234567890", "openid.claimed_id" "http://steamcommunity.com/openid/id/xxx", "openid.return_to" "http://localhost:3000/resp"}
The keys in your map are Strings and not Keywords.
You can either use:
(get m "openid.claimed_id")
or you can first convert the String keys to Keywords and then lookup based on a Keyword:
(:openid.claimed_id (clojure.walk/keywordize-keys m))
Since a map is also a function that can do lookup on itself, the simplest way to do this is
(m "openid.claimed_id")
with m being your map.

difference between [] and list() in python3

I thought that [] and list() were two equal ways to create a list. But if you want a list with dictionnary keys,
var = [a_dict.keys()]
doesn't work since type(var) is [dict_keys], correct syntax is :
var = list(a_dict.keys())
I couldn't find an good explanation on this behaviour. Do you have one ?
TL;DR:
list() is the same as []
list(obj) is not the same as [obj]
a_dict.keys() is a dictionary view object, it returns an object which can be iterated to yield the keys of a_dict. So this line:
[a_dict.keys()]
is saying in python "I'm making a list with one element in it" and that one element is the dict keys iterator. It's a list literal in the syntax.
Now this line:
list(a_dict.keys())
is a call to the list builtin function. This function list attempts to iterate the argument and produce a list. It's a function call in the grammar.
The equivalent list literal (actually list comprehension) would be instead:
[key for key in a_dict.keys()]
Finally, note that dictionary objects iterate by keys anyway,
list(a_dict.keys()) would usually be written more simply as as list(a_dict) instead.
Hope this helps.
[a_dict.keys()]
This one puts a single element in the list. Just as if you were to write [1]. In this case that one element is going to be a list.
list(a_dict.keys())
The constructor accepts a sequence and will add all elements of the sequence to the container.

How can I get property name and property value from json-cpp parser?

I'm using jsoncpp parser (http://jsoncpp.sourceforge.net) to parse JSON data.
So, if we have the following JSON:
{ "name": "Joseph", "age": 20 }
How can I get the property name name and value Joseph, ... after age and 20?
OK, we can do universally this:
string e = root.get(propertyName, defaultValue).asString();
But really what we want is something like this:
string e = root.get(name, "Mark").asString();
Now, variable e is Joseph, it works. But I have to take/write "name". I do not want to QUERY (without questioning the function I want to get "name" (name of property) and "Joseph" (value of property)).
After it would be best to store in a field (C/C++ for example):
property[name][0] = "Joseph"
property[age][0] = 20
How can I do that? Or any other ideas?
You can get all the member names of a Json::Value object using its getMemberNames() function. That returns an object that you can iterate over using .begin() and .end(), like any other standard library container. (In fact, the return type is an alias for std::vector<std::string>.)
After you have the member names, you should be able to iterate through them and use .get(std::string &, const ValueType &) as you already are doing to get the values for each of the object's keys.
Note that JSON objects are inherently unordered, so you can't necessarily rely on that name list having any ordering whatsoever. If you want an ordered object, you should be using JSON arrays, not JSON objects.