Hi, All!

I belive I have found an error in the code in the working of
Enumerable.eachSlice!
I went into a deeper analysis when calling eachSlice on the example in
the documentation just failed to produce the given result there!

Here it is:

var students = [
  { name: 'Sunny', age: 20 },
  { name: 'Audrey', age: 21 },
  { name: 'Matt', age: 20 },
  { name: 'Amelie', age: 26 },
  { name: 'Will', age: 21 }
];
/*Then calling eachSlice should give the following result- according
to the documentation:*/

 var result = students.eachSlice(3, function(student) {
  return student.name;
});
// -> [['Sunny', 'Audrey', 'Matt'], ['Amelie', 'Will']]

/*If you catch the return value in the result variable and then do
something like:*/

result.each(function(outerValue){

alert(outerValue.constructor);

 /*gives 'native code Array  something' as expected for the two member
arrays. So far so good!
But walk through the content of these member arrays and you will get
'undefined' for any value you want to  check,
instead of the names of the kids in the example!*/

outerValue.each(function(value){
      alert(value) //gives 'undefined' for good reason!

});
});

/*The problem comes from the fact that eachSlice gives the wrong feed
to the collect method internally! Collect goes through each member of
an array and tries to processes them through the iterator function.
If you use the original eachSlice method, the object literals from the
students array are first separated into two separate member arrays in
slices.This is wrong, because collect just iterates over these members
AFTERWARDS to feed each array to the iterator it was given. In the
example the no-name iterator tries to read the name property value of
each array member which they obviously do not have. This is the reason
why I get 'undefined' values instead of the names!!*/

/*THE ORIGINAL eachSlice METHOD*/

 function eachSlice(number, iterator, context) {

var index = -number, slices = [], array = this.toArray();
    if (number < 1) return array;

 while ((index += number) < array.length)
      slices.push(array.slice(index, index+number));

    return slices.collect(iterator, context);
  }
/*ORIGINAL eachSlice  ENDS*/

/* MODIFIED CODE TO PROCESS EACH MEMBER ARRAY, FIRST, BEFORE PUSHING
THEM INTO slices!*/

function eachSlice(number, iterator, context){
      var index = -number, slices = [], array = this.toArray();
      if(number < 1) return array;

      while((index += number) < array.length){
          slices.push((array.slice(index, index
+number)).collect(iterator, context));
      }
      return slices;
};

Now if you walk through the members of the result variable and alert
the outerValue and the value arguments, you will  get the expected
results!

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to