473,667 Members | 2,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Newbyish question about Array slice method

I bought Crockford's "JavaScript : The Good Parts" yesterday to help
build my JavaScript foo.

On page 44, he gives an implementation of the curry function:

Function.method ('curry', function() {
var slice = Array.prototype .slice,
args = slice.apply(arg uments),
that = this;

return function() {
return that.apply(null , args.concat(sli ce.apply(argume nts)));
};
});

I'm trying to reconcile the two invocations of slice.apply(arg uments)
with the notion that the slice method in Array's prototype has two
arguments, the second of which is optional.

I thought that this would mean that those invocations would need to be
slice.apply(arg uments,0) in order to supply the start argument, and in
"leap before you look" mode I submitted an erratum to O'Reilly.

Now however, I've actually tried the code in both Firefox and Safari,
and it seems to work as it's given in the book.

So is the first argument to slice really optional despite the various
doc's I've checked (including and besides the book in question), or
are Firefox and Safari both working outside of the JS spec here, or is
there something else I'm missing?
Jun 1 '08 #1
7 1925
VK
On Jun 1, 9:52 pm, RubyRedRick <rick.denat...@ gmail.comwrote:
So is the first argument to slice really optional despite the various
doc's I've checked (including and besides the book in question), or
are Firefox and Safari both working outside of the JS spec here, or is
there something else I'm missing?
The docs are wrong: at least for an engine strictly implementing the
relevant ECMA-262 internal algorithms.

Array slice method production:

15.4.4.10
....
4. Call ToInteger(start ).
5. If Result(4) is negative, use max((Result(3)+ Result(4)),0); else
use min(Result(4),R esult(3)).

Internal ToInteger production:

9.4 ToInteger
1. Call ToNumber on the input argument.
2. If Result(1) is NaN, return +0.
....

This way slice() without arguments still will equal to slice(0) so
still be working. This is what I don't like rather often in
Crockford's approaches: they are bearing to much of mannerism for my
blood, to much of coding just for coding. Respectively sometimes
_unnecessary_ dependence on strict engine implementations . Does it
kill anyone to provide the argument? Why to transform a business
solution into some kind of engine's acid test? It is strictly my
opinion and the book contains an ocean of interesting approaches. Just
be warned that it is not a book I would recomment to anyone to learn
javascript: it is for advanced programmers willing to improve their
skills. Again strictly IMHO.
Jun 1 '08 #2
RubyRedRick wrote:
>I bought Crockford's "JavaScript : The Good Parts" yesterday
to help build my JavaScript foo.

On page 44, he gives an implementation of the curry function:

Function.method ('curry', function() {
var slice = Array.prototype .slice,
args = slice.apply(arg uments),
that = this;

return function() {
return that.apply(null , args.concat(sli ce.apply(argume nts)));
};
});

I'm trying to reconcile the two invocations of
slice.apply(arg uments) with the notion that the slice method in
Array's prototype has two arguments, the second of which is
optional.

I thought that this would mean that those invocations would need
to be slice.apply(arg uments,0) in order to supply the start
argument, and in "leap before you look" mode I submitted an
erratum to O'Reilly.

Now however, I've actually tried the code in both Firefox and
Safari, and it seems to work as it's given in the book.

So is the first argument to slice really optional despite the
various doc's I've checked (including and besides the book in
question), or are Firefox and Safari both working outside of
the JS spec here, or is there something else I'm missing?
Perhaps you are falling to look at the JS Spec itself (ECMA 262, 3rd
Ed.)? the forth step in the algorithm for - Array.prototype .slice - is
"Call ToInteger(start )", and the internal ToInteger function will return
numeric zero when its input is the undefined value (which is what its
input is going to be when the argument is not passed in).

A much more interesting question is why use - apply - when you could
use - call -? Not that the former is a mistake (the handling of the
undefined second argument is fully specified) but the - apply - method
has a length of 2 and the call method has a length of 1, which suggests
it should be the one to use when there is only going to be one argument.

Richard.

Jun 1 '08 #3
On Jun 1, 2:37*pm, "Richard Cornford" <Rich...@litote s.demon.co.uk>
wrote:
RubyRedRick wrote:
I bought Crockford's "JavaScript : The Good Parts" yesterday
to help build my JavaScript foo.
On page 44, he gives an implementation of the curry function:
Function.method ('curry', function() {
var slice = Array.prototype .slice,
args = slice.apply(arg uments),
that = this;
*return function() {
* *return that.apply(null , args.concat(sli ce.apply(argume nts)));
};
});
A much more interesting question is why use - apply - when you could
use - call -? Not that the former is a mistake (the handling of the
undefined second argument is fully specified) but the - apply - method
has a length of 2 and the call method has a length of 1, which suggests
it should be the one to use when there is only going to be one argument.
I don't see how call could be used here. The method is being used as
a trick to obtain a real array object from arguments which is a pseudo-
array and lacks methods.

Jun 1 '08 #4
RubyRedRick wrote:
"Richard Cornford" wrote:
>RubyRedRick wrote:
>>I bought Crockford's "JavaScript : The Good Parts" yesterday
to help build my JavaScript foo.
On page 44, he gives an implementation of the curry function:
Function.meth od('curry', function() {
var slice = Array.prototype .slice,
args = slice.apply(arg uments),
[1]--------------^^^^^
>>that = this;
return function() {
return that.apply(null , args.concat(sli ce.apply(argume nts)));
[2]----------------------------------------------^^^^^
>>};
});
>A much more interesting question is why use - apply - when you could
use - call -? Not that the former is a mistake (the handling of the
undefined second argument is fully specified) but the - apply - method
has a length of 2 and the call method has a length of 1, which suggests
it should be the one to use when there is only going to be one argument.

I don't see how call could be used here. The method is being used as
a trick to obtain a real array object from arguments which is a pseudo-
array and lacks methods.
You can use Function.protot ype.call() instead of Function.protot ype.apply()
at [^1] and [^2] because there is no second argument there.
PointedEars
Jun 1 '08 #5
VK
On Jun 1, 9:52 pm, RubyRedRick <rick.denat...@ gmail.comwrote:
So is the first argument to slice really optional despite the various
doc's I've checked (including and besides the book in question), or
are Firefox and Safari both working outside of the JS spec here, or is
there something else I'm missing?
The docs are wrong: at least for an engine strictly implementing the
relevant ECMA-262 internal algorithms.

Array slice method production:

15.4.4.10
....
4. Call ToInteger(start ).
5. If Result(4) is negative, use max((Result(3)+ Result(4)),0); else
use min(Result(4),R esult(3)).

Internal ToInteger production:

9.4 ToInteger
1. Call ToNumber on the input argument.
2. If Result(1) is NaN, return +0.
....

This way slice() without arguments still will equal to slice(0) so
still be working. This is what I don't like rather often in
Crockford's approaches: they are bearing to much of mannerism for my
blood, to much of coding just for coding. Respectively sometimes
_unnecessary_ dependence on strict engine implementations . Does it
kill anyone to provide the argument? Why to transform a business
solution into some kind of engine's acid test? It is strictly my
opinion and the book contains an ocean of interesting approaches. Just
be warned that it is not a book I would recomment to anyone to learn
javascript: it is for advanced programmers willing to improve their
skills. Again strictly IMHO.
Jun 27 '08 #6
On Jun 1, 2:37*pm, "Richard Cornford" <Rich...@litote s.demon.co.uk>
wrote:
RubyRedRick wrote:
I bought Crockford's "JavaScript : The Good Parts" yesterday
to help build my JavaScript foo.
On page 44, he gives an implementation of the curry function:
Function.method ('curry', function() {
var slice = Array.prototype .slice,
args = slice.apply(arg uments),
that = this;
*return function() {
* *return that.apply(null , args.concat(sli ce.apply(argume nts)));
};
});
A much more interesting question is why use - apply - when you could
use - call -? Not that the former is a mistake (the handling of the
undefined second argument is fully specified) but the - apply - method
has a length of 2 and the call method has a length of 1, which suggests
it should be the one to use when there is only going to be one argument.
I don't see how call could be used here. The method is being used as
a trick to obtain a real array object from arguments which is a pseudo-
array and lacks methods.

Jun 27 '08 #7
RubyRedRick wrote:
"Richard Cornford" wrote:
>RubyRedRick wrote:
>>I bought Crockford's "JavaScript : The Good Parts" yesterday
to help build my JavaScript foo.
On page 44, he gives an implementation of the curry function:
Function.meth od('curry', function() {
var slice = Array.prototype .slice,
args = slice.apply(arg uments),
[1]--------------^^^^^
>>that = this;
return function() {
return that.apply(null , args.concat(sli ce.apply(argume nts)));
[2]----------------------------------------------^^^^^
>>};
});
>A much more interesting question is why use - apply - when you could
use - call -? Not that the former is a mistake (the handling of the
undefined second argument is fully specified) but the - apply - method
has a length of 2 and the call method has a length of 1, which suggests
it should be the one to use when there is only going to be one argument.

I don't see how call could be used here. The method is being used as
a trick to obtain a real array object from arguments which is a pseudo-
array and lacks methods.
You can use Function.protot ype.call() instead of Function.protot ype.apply()
at [^1] and [^2] because there is no second argument there.
PointedEars
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1932
by: Maarten van Reeuwijk | last post by:
Hello, I am using the Numeric package, and I need to strip edge cells off an array (dimension unknown) in an a-priori unknown direction. I implented this as follows: def el_remove_bcells(var, axis): """ elementary routine to strip the edge cells of an array for the given direction. """
0
1627
by: Maarten van Reeuwijk | last post by:
I am constructing a "placeholder array" to mimick Numeric arrays. I have a 3D regular structured domain, so I can describe the axis with 3 1D arrays x, y and z. All the variables defined on this field (temperature, velocity) are 3D-fields, and to treat everything the same, I want to have x,y and z also as a 3d-array. However, as one field is typically 100 Mb, I do not want to create these arrays, but instead define a class that mimicks...
2
8027
by: Dave Bazell | last post by:
I have a multidimensional array, say 20 x 6, and I want to take a subset of the rows. I have an index array @indx=(1,3,12,17) for example, and what I want to say is @new_array = $data I know that does not work. It has something to do with using the appropriate reference to the array elements. How should I do this? And why does it work?
40
2599
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end indexing) symmetrical and more consistent. So to find out if this is indeed a possibility, it would be nice to get a few opinions at this point. So blast away... or hopefully tell me what you like about it instead. ;-) (Any suggestions or...
65
4195
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the difference is between these and: (3) mylist =
1
2051
by: Ivan Vinogradov | last post by:
Hello All, this seems like a trivial problem, but I just can't find an elegant solution neither by myself, nor with google's help. I'd like to be able to keep an array representing coordinates for a system of points. Since I'd like to operate on each point's coordinates individually, for speed and ufuncs numarray fits the bill perfectly, especially since system.coordinates
1
9652
by: fahd | last post by:
Hi, I'm trying to communicate with an unmanaged c++ dll that takes two 2d float arrays, one as input with data in it and the other will have the result of the operation in it as follows: bool Evaluate(float** input,float** output,int nFrames, int featureWidth); note: nFrames and featureWidth will determine the sizes of input and
12
2055
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); return function(){ return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
9
1532
by: lorlarz | last post by:
I still have a question regarding the following code, in a commonly used routine. First, Here's the code in question: Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); return function(){ return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
0
8458
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8565
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.