_.initial比Underscore.js中需要的更详细吗?

时间:2017-07-21 16:22:20

标签: javascript underscore.js

这是In[1]: img.shape Out[1]: (128, 128) In[2]: all_imgs.shape Out[2]: (1000, 10, 128, 128) In[3]: img[None,None,:,:].shape Out[3]: (1, 1, 128, 128) In[4]: np.append(all_imgs, img[None,None,:,:], axis=0) Traceback (most recent call last): File "/Users/blah/anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-29-b82fcbdc4f61>", line 1, in <module> np.append(all_imgs, img[None,None,:,:], axis=0) File "/Users/blah/anaconda/lib/python3.5/site-packages/numpy/lib/function_base.py", line 5003, in append return concatenate((arr, values), axis=axis) ValueError: all the input array dimensions except for the concatenation axis must match exactly 的{​​{1}}代码:

underscore.js

在我看来,以下内容不那么冗长和等同:

_.initial

询问这样的细节并不是不合适的,因为 // Returns everything but the last entry of the array. Especially useful on // the arguments object. Passing **n** will return all the values in // the array, excluding the last N. _.initial = function(array, n, guard) { return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n))); }; 试图尽可能多地保存字节。举例来说,这个评论:

  _.initial = function(array, n, guard) {
    return slice.call(array, 0, Math.min(0, -(n == null || guard ? 1 : n)));
  };

是否有更详细的代码?

1 个答案:

答案 0 :(得分:0)

我现在看到它在n0的极端情况下有所不同。不过,在此之前,这似乎是一个引人注目的问题。写极端情况是没用的:_.initial(collection, 0)因为它等同于collection(给予或成为真正的数组);但是,某些需要变化n的代码很可能会使用n = 0的极端情况。