> You cannot re-do function above using each().
Well, you *can*, but given the richness of the Enumerable API, you're
not likely to need to -- as you point out, any() is exactly what you
want in the situation you describe, and if it weren't then all(),
detect(), or collect() would probably suit.
But if you need something that isn't covered by an existing Enumerable
API, you can use a closure to achieve it. Most of the time (I
suspect) people are using closures with each() already, so in the
typical case you're not adding a bunch of new overhead.
Again, you're right that any() is exactly what you want, but much to
illustrate using a closure to get information out of your iterator:
function checkSomething(arr)
{
var result;
result = "Succ";
arr.each(function(elm) {
if (elm == 15)
{
result = "Fail";
throw $break;
}
});
return result;
}
FWIW,
--
T.J. Crowder
tj / crowder software / com
On Oct 9, 11:38 am, Tomasz Kalkosiński <[EMAIL PROTECTED]>
wrote:
> On Oct 9, 12:23 pm, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
>
> > Or, if there's a situation where any() or all() doesn't suit, you can
> > break each() loops by throwing $break.
>
> Yes I can do it, but I cannot determine afterwards whether I've exited
> iteration with throwing $break or I've iterated thru all elements.
>
> Also scenatio of returning something different than true/false using
> any() and all() is undoable with each():
>
> function checkSomething (arr)
> var succ = "Succ";
> var fail = "Fail"
> for (i=0 ; i < arr.length ; i++) {
> var c = arr[i];
> if (c == 15) return fail;
> }
> return succ;
>
> }
>
> You cannot re-do function above using each(). All I know you can do $
> (arr).all(function (c) {...}) ? "Succ" : "Fail";.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---