how to create custom enumerator in Ember? - ember.js

I'm looking for a way to create custom enumerator for collection on the example of sum enumerator - using reduce() several times that only sums value is not a good solution.
The Enumerable API says that an enumerator has to implement nextObject method and have length property. But not all the built-in enumerators seem to have them.
I tried with reopening the Ember.Enumerable class but it failed.
An example how to create a sum enumerator based on reduce (or a resource how to do it) will be a great help.
edit
# custom enumarator I want to have
sum = #get('items').sum(0)
# current solution
sum = #get('items').reduce ((prev, curr) -> prev + curr,) 0
Ember has Em.computed.sum but I couldn't find it as a normal enumerable.

I think you are confusing enumerating functions and enumerable objects.
An object that is enumerable will have to implement nextObject and length. These are things like linked lists, queues, stacks, and sets. Basically, data structures that you would want to iterate.
An enumerating function is something you can apply to an enumerable object, like sum, min, max.
You should not be adding this method to Ember.Enumerable because not all enumerables can be summed. A list of numbers can, but how would you sum a collection of fruits?
That said, this should answer you question:
http://emberjs.jsbin.com/nodojadi/1/edit

Related

How to create a function that returns a list that is a union of the elements of a nested list in Ocaml?

If I was given a set of lists within a list in Ocaml, for example [[3;1;3]; [4]; [1;2;3]], then how can we implement a function to return a list that is a union of the values of the nested list (so the output from the example will return [1;2;3;4])? I tried removing duplicates from the list, but it didn't work as intended. I am also restricted to using the List module only.
Restricted to using the List module only? Sounds like homework with an arbitrary limit like that. so I don't want to give a fully working solution. However, if you look through the List documentation, you'll see a couple of functions that can be combined to do what you want.
concat, which takes a list of lists and flattens them out into a single list, and sort_uniq, which sorts a list and removes duplicates.
So you just have to take your list of lists, turn it into a single list, and sort_uniq that (With an appropriate comparison function) to get your desired results.

How to group duplicates using MapReduce?

I want to use MapReduce to return a list of duplicate tuples. By duplicate tuples, I mean tuples having similar values of a set of attributes.
Could I put the values of this set of attributes as an intermediate key and adjust reduce to process all similar keys as one key?
Yes, I could implement my own class of intermediate key witch implements the interface WritableComparable. So I was forced to implement the function CompareTo which return 0 if the inputs are equals.
In my case, the attributes' class are the attributes of my tuples. So, I just wrote the function "CompareTo" in such a way that it returns 0 when all these attributes are similar. The similarity here can be computed by the Levenshtein Edit Distance.

Usual Terminology to name a list of list

I frequently have to deal with list of list of my item-type in my scripts.
Most of the time, I'm reducing the list of my item-type into items with a plural to indicate a collection. Following this convention, I would consequently name my list of list of my item-type : list_items
However I might as well have to deal with some list of list_items.
So I was wondering if there were any terminology, name, to use so as to indicate the concept list of list of (or even sequence of sequence of or generator of generator of)
I first thought of 2D-Array, but it's not appropriate since all lists may not have the same length.
Any idea ?
struct Value;
Value value
list<Value> values;
typedef list<Value> ValueList;
ValueList valueList;
list<ValueList> valueLists;
I would not go any further. Usually you should not name you data types based on its content. The name should try to express the semantics.
Sure you have a list of values, but that does it mean? Is it a List of Features? then you should call it so. That does the List of Featurelists represent? is it your Groundtruth data of your classifier? Hope you got the point.

Hierarchical filtered lookup in C++

I have been pondering a data structure problem for a while, but can't seem to come up with a good solution. I can not shake off the feeling that the solution is simple and I'm just not seeing it, however, so hopefully you guys can help!
Here is the problem: I have a large collection of objects in memory. Each of them has a number of data fields. Some of the data fields, such as an ID, are unique for each objects, but others, such as a name, can appear in multiple objects.
class Object {
size_t id;
std::string name;
Histogram histogram;
Type type;
...
};
I need to organize these objects in a way that will allow me to quickly (even if the number of objects is relatively large, i.e. millions) filter the collection given a specification of an arbitrary number of object members while all members that are left unspecified count as wildcards. For example, if I specify a given name, I want to retrieve all the objects whose name member equals the given name. However, if I then add a histogram to the query, I would like the query to return only the objects that match in both the name and the histogram fields, and so on. So, for example, I'd like a function
std::set<Object*> retrieve(size_t, std::string, Histogram, Type)
that can both do
retrieve(42, WILDCARD, WILDCARD, WILDCARD)
as well as
retrieve(42, WILDCARD, WILDCARD, Type_foo)
where the second call would return fewer or equally as many objects as the first one. Which data structure allows queries like this and can both be constructed and queried in reasonable time for object counts in the millions?
Thanks for the help!
First you could use Boost Multi-index to implement efficent lookup over differnt members of your Object. This could help to limit the number of elements to consider. As a second step you can simply use a lambda expression to implement a predicate for std::find_if to get first element or use std::copy_if to copy all elements to an target sequence. If you decide to use boost you can use Boost Range with filtering.

Database-like search algorithm in C++

I have a collection of objects. Each object is described by ~4 parameters (let's say two integers and two strings). How can I implement this collection in C++ to be able to quickly find subsets of these objects by specifying search-criterias, i.e. "find all object with first parameter equal to 1", or "search all objects with second parameter equal to 'foo' " (lookup is always performed using one-parameter query: parameter=value). Should I have 4 std::maps, so that each parameter based lookup is performed in O(logn) ? What if I add another parameter and another?
Are there any existing solutions for this problem?
You should try Boost Multi Index, which is meant for just this kind of thing.
One array for the data, four hash tables (std::tr1::unordered_map) for the indexes.