On Sat, Aug 3, 2013 at 12:49 PM, Paul Davis <[email protected]>wrote:
> That's mostly right. The truth table you describe isn't quite right > for the value assigned to the key variable. Its album.title unless its > falsy in which case its album.name. Here falsy can mean more than the > strict "null" value you mention. I was suspecting that. Thanks for confirming my initial hunch. > The second bit is that the value > assigned to the variable named "value" is an object and not a tuple. > Gotcha. I was thinking in terms of Erlang, hence the choice of the word. But thank you for correcting me and letting me know the name of the specific data type. > > On Fri, Aug 2, 2013 at 8:55 PM, Yves S. Garret > <[email protected]> wrote: > > This is the piece of Javascript code that I'm working with: > > > > function(doc) { > > if('name' in doc && 'albums' in doc) { > > doc.albums.forEach(function(album) { > > var key = album.title || album.name; > > var value = {by: doc.name, album: album}; > > > > emit(key, value); > > //emit(key, key); > > }); > > } > > } > > > > Now, this is my interpretation of how I view this code. > > - The function checks if there is a name and an albums field in the doc. > > - Then, using the doc, it accesses the list of albums and executes for > each > > element in the list a > > method. > > - That method accepts one element -- by design -- and only one element. > > That element is the > > individual value that's stored in the list. In this case, the format of > > the list is assumed to be uniform and known in advance (but this is not > > guaranteed and would need extra checking to take into > > account). > > - In the function, the first variable is created by checking either > > album.title or album.name and > > selecting the one that is not null (if both are null, then you have a > > problem, but would assign to the > > key a null). > > - The value is created in a similar fashion by returning a tuple. > > - The results are outputted. > > > > Is my understanding of this correct? Did I read the code correctly? >
