Newbyish question about Array slice method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RubyRedRick

    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?
  • VK

    #2
    Re: Newbyish question about Array slice method

    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.

    Comment

    • Richard Cornford

      #3
      Re: Newbyish question about Array slice method

      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.

      Comment

      • RubyRedRick

        #4
        Re: Newbyish question about Array slice method

        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.

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Newbyish question about Array slice method

          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

          Comment

          • VK

            #6
            Re: Newbyish question about Array slice method

            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.

            Comment

            • RubyRedRick

              #7
              Re: Newbyish question about Array slice method

              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.

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Newbyish question about Array slice method

                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

                Comment

                Working...