I was trying to iterate a list and after googling, I found one solution like below:
value([0,1,2,3,4,5,6].sort{new Random()}?.take(1)[0])
I did not understand this part:
sort{new Random()}
Can someone explain this?
And which class the take method is belonging to?
sort receives a closure to determine the order that you wish. In this example, it makes a random sort.
take comes from Iterable and returns the first n elements.
Related
I'm trying to sort a list for int type, so that the first element is the lowest and the last is the highest value.
I can't use sort function or remove/del.
only simple methods like for, while and or /append and so on.
I tried using for loop but don't know what to do exactly.
thank you for your help
Using datomic and clojure I query a database and get a list of nine elements.
I would like to draw these nine elements on the page, and at this point they are guaranteed to be distinct elements.
However, every time I call the function, it redoes the query, returns a new list, and then gets an element from the new list. This is very inefficient and also introduces the possibility of duplicates.
I would like to memoize this list and have it be nth-indexable. Suggestions and ideas welcome.
Instead of calling "the function", you should only call it if you have not called it already. If you call it, make sure to store the result. If you don't call it, look up the result.
memoize (http://crossclj.info/fun/clojure.core/memoize.html) might help you to achieve that. Depending on your cache requirements, you might want to study its implementation and implement something more suitable.
You might want to refer to https://github.com/clojure/core.memoize for more sophisticated memoization needs on the serverside.
Any list is nth-indexable with O(n). For O(log 32 n) performance, create a vector from it using vec.
currently I am trying to solve two problems when working with iterators.
1 When using something like
forAllIter(PtrDictionary<phaseModel>, phases_, iter)
{
phaseModel& phase = iter();
.
.
.
}
is it possible to get the actual position/index of iterator "iter" (which pointer is the iterator refering to at the moment)?
I know that for the vector class I could use something like described in the following link
What is the most effective way to get the index of an iterator of an std::vector?
but unfortunately the class "PtrDictionary" which I have to use does not offer a method "begin()". Furthermore "PtrDictionary" is basically of type "intrusive doubly linked list".
2
Old Question:
If I define a dictionary like
const dictionary& subDict_Droplets;
subDict_Droplets = properties.subDict("currentValue");
Can I create an iterator for the dictionary, set this iterator to a
specific position of the dictionary (the position I would get from the
first iterator if possible -> see code snippet 1) and get the content
of the dictionary at that position?
I have read the following thread
how to get iterator to a particular position of a vector
but again class "dictionary" does not have any method "begin()".
Edit/New: I have used now a list (which is of "type" vector) like
const List<dimensionedScalar> List_Droplets;
List_Droplets = properties.subDict("currentValue");
The elements of the vector should be accessable like an array as far as I have read :).
However, the type of the object phases_ which is of type PtrDictionary<phaseModel> (parents: DictionaryBase< IDLListType, T > and parent of this: IDLListType) which is mentioned in question 1 I can t change.
Any hints, ideas or code solutions are welcome :)
greetings streight
Sometimes an iterator corresponds to an index that can easily be found, for example a vector iterator. Here you use distance(vector.begin(), iter);
Sometimes an iterator corresponds to an index that cannot be easily found, for example a list iterator. Here you have to start at begin() and work through, counting as you go. distance() will do this if needed but its obviously a lot slower.
Sometimes the idea of an 'index' does not really apply - I think an unsorted map might be a good example of this. distance will still return a value but it doesn't mean much I think. It might even be implementation dependent - I would have to look that up.
For certain reasons, I have a linked list of objects, with the Object containing a string.
I might be required to search for a particular string, and in doing so, retrieve the object, based on that string.
The starting header of the list is the only input I have for the list.
Though the number of objects I have, is capped at 3000, and that's not so much, I still wondered if there was an efficient way to do this, instead of searching the objects one by one for a matching string.
The Objects in the list are not sorted in any way, and I cannot expect them to be sorted, and the entry point to the linked list is the only input I have.
So, could anyone tell me if there's an efficient way ( search algorithm perhaps ) to achieve this?
Separately for this kind of search, if required, what would be the sort of data structure recommended, assuming that this search be the most data intensive function of the object?
Thanks..
Use a std::map<std::string, YourObjectType>. You can still iterate all objects. But they are sorted by the string now.
If you might have multiple objects with the same string, use a multimap instead.
If you can't switch to any different structure/container, then there is no way to do this better than linear to size of list.
Having 3000 you would like to use a unordered map instead of a linked list, which will give you average O(1) lookup, insertion, and deletion time.
I have a problem that requires me to add elements to a list that are spread across various predicates. Rather than doing via argument based lists I've opted to use a dynamic list predicate. I simple example can be seen below. When I initially used it it worked fine but now if I use any argument as X it keeps retrieving previous argument data from the list even after closing the program and recompilation. Does anybody know what's wrong with it?
//list declarations
:- dynamic listD/1.
listD([]).
//pushes X onto the list then retrieves the entire list for verification
sample(X):-
assert(listD(X)),
listD(Y),
write(Y).
Example usage
sample([adam]).
//prints adam fine
sample([fred]).
//prints adam again
Use retractall to clean up the state when you start.
sample(X):-
retractall(listD(_)),
assert(listD(X)),
listD(Y),
write(Y).