Adding meta data to an object returns a new object with the metadata attached:
(def plain-data [1 2 3])
(def decorated-data (with-meta plain-data {:url "http://stackoverflow.com"}))
;; returns false
(identical? plain-data decorated-data)
How do I recover the original plain-data from the decorated-data?
I'd like to do something like,
(def undecorated-data (with-meta decorated-data nil))
;; how can I make this return true?
(identical? plain-data undecorated-data)
Adding metadata doesn't wrap the value, it returns a clone of the value with metadata attached - there's no way to recover the original thing. However it's not clear why you would ever need to do that, use = if you need to check for equality.
Related
I have a File which contains all the names of all the files I've created and I decode it into a Map which then has the following formate:
{"fileList.json":"false",
"testwert_0_.json":"false",
"jakob_0_.json":"false",
"jakobjokoobus_0_.json":"true",
"jakobjokoobussss_0_.json":"false",
"jakobjokoobusssssss_0_.json":"true"}
Now I would like to delete all the entries from the map, which have the value set to "true".
It should be a simple problem, but I just can't wrap my head around the map.removeWhere() function.
Could someone please help me with this?
This should do it:
// assumes your map is declared by 'm'
m.removeWhere((k, v) => v == "true");
The function is called for each key-value pair. You remove the ones with value "true".
For other handy methods available for Maps and Objects in Dart this article is worth a read.
Let's say I have a triple store full of video metadata. A video can have an arbitrary selection of pre-defined tags, like this:
v1 ex:hasTag A.
v2 ex:hasTag B.
v3 ex:hasTag A;
ex:hasTag B.
So for the sake of this example, there are only the two pre-defined tags A and B.
Now I would like to get an overview of which video has which tags attached, in a matrix like this (or similar):
A B
v1 true false
v2 false true
v3 true true
However, I do not know how to achieve this. First of all, I would need a boolean test, e.g. something like isTrue(?video ex:hasTag A) or whatever it looks like. Even then, I do not know where to put it. If I put the previous statement in the WHERE clause, the query result only contains the videos having the tag A, of course.
Coming from SQL, I can imagine I need to use subqueries in some way, but these seem not to be standardized at the moment. I also saw the FILTER keyword, but I don't feel that is what I want.
I'm currently at a loss but always willing to learn. Any help is appreciated.
Subqueries are in SPARQL 1.1 and that is frozen now. There several complete implementations already.
As well as nested-SELECT subqueries, SPARQL 1.1 has the EXISTS and NOT EXISTS functions for testing whether a pattern matches or not. It#'s a form of subquery. You can use it in a FILTER or if you really want to return true/false:
BIND(EXISTS{?video ex:hasTag "A"} AS ?a)
or even:
SELECT ?video (EXISTS{?video ex:hasTag "A"} AS ?a) (EXISTS{?video ex:hasTag "B"} AS ?b)
{
?video a ex:Video .
}
Try starting with something like this:
SELECT ?video ?tagA ?tagB {
?video a ex:Video .
OPTIONAL { ?video ex:hasTag ?tagA . FILTER (?tagA = "A") }
OPTIONAL { ?video ex:hasTab ?tagB . FILTER (?tagB = "B") }
}
That will you roughly what you want. If you really must have boolean values rather than simple checking for unbound vals use:
SELECT ?video (bound(?tagA) as ?a) (bound(?tagB) as ?b) { ... }
P.S. Subqueries are (very nearly, proposed recommendation at time of writing) standardised in SPARQL 1.1.
I am trying to reference an element buried within a structure that I did not create (hence I don't know the exact way in which it was built).
Having loaded the structure, if I type:
dataFile.RECORDINGS.eye
I receive the following output:
ans =
2
ans =
2
Both of those variables will always be the same, but they could be at any time 1, 2 or 3. What I'd like to do is check with a switch statement which looks like this:
switch dataFile.RECORDINGS.eye
case {1, 2}
% action A
case 3
% action B
end
Of course, the above throws up an error because 'case' cannot check whether dataFile.RECORDINGS.eye contains a given value since there are two elements stored under that address. So, my question is: how do I reference just one of the elements? I thought it would be as simple as replacing the first line with:
switch dataFile.RECORDINGS.eye(1)
...But, this gives the error:
??? Field reference for multiple structure elements that is followed by more reference blocks is an error.
Similarly, I can't access the element like this:
switch dataFile.RECORDINGS.eye.1
...As I get the following error:
??? Dot name reference on non-scalar structure.
If the values are really always the same, you can try the following to get a scalar that can be used in the switch command:
unique([dataFile.RECORDINGS.eye])
By the way, did you try to index RECORDINGS, i.e.,
dataFile.RECORDINGS(1).eye
dataFile.RECORDINGS(2).eye
Perhaps instead of eye having multiple elements, you have multiple elements of RECORDINGS that each have a single value of eye? You might want dataFile.RECORDINGS(1).eye or dataFile.RECORDINGS(2).eye.
using Clojure 1.3.0 with CongoMongo 0.1.7 and doing a simple (fetch-one :my_collection) return something like the following:
{:_id #<ObjectId 4f10d46c56bad557a7729f95>,
:key2 "value2",
:etc "etc
}
and that '#<ObjectId' breaks everything but I'm surprised I cannot find anyone with the issue anywhere.
In fact I do a simple (type *the_result_above*) I get clojure.lang.PersistentArrayMap while manually removing the # bit I correctly get clojure.lang.PersistentHashMap
Any idea? Thanks!
You are not the only one with this issue. In one of my projects I created a workaround by adding the following function:
(defn string-id [map]
(assoc map :_id (str (map :_id))))
Feed it with any map from CongoMongo, and it will replace the _id with it's string representation and return the updated map.
I'm writing a tutorial on Rebol's Object persistence but I'm not sure if my way is the best
suppose %config.txt contains
a: 1
b: 2
We can then load it with
config: construct load %config.txt
To save it back to file I use this
save %config.txt (pick to-block mold config 3)
But I'm not sure this is the most elegant syntax to do this in Rebol so do you have another suggestion ?
Some would say its more elegant to save the entire object. But that would lead to a less easy to edit text file. (I assume you may have humans editing the text file).
A shorter form of your save:
save %config.txt mold third config
or unnecessarily shorter
save %config.txt body-of config
I don't think mold is necessary, if you mold it then it will be a string and you will need to load it twice
save %config.txt mold third config
t: load %config.txt
? t
>> T is a string of value: {[a: 1 b: 2]} ;you need to load this string to make it a block
t: load load %config.txt
? t
>> T is a block of value: [a: 1 b: "x"] ;so t can be used to construct an object
So, simply don't use mold.