zoukankan      html  css  js  c++  java
  • 下划线的学习3

        到现在为止,已经大半了.感觉这个_还是蛮基础(不是简单)的,基本上把函数式编程发挥的很尽兴,除了pluck本人不是很喜欢外,不过当成select算了.多记个单词.

        接下来是Array,其实这里不得不说下,JS的Array的对象,Json有点纠缠不清,这就是广义相对论,离大统一场论只差一步啊.

        这里同时不得不提到一下,函数式编程的真谛,正在逐步浮出,我们发现函数式往往是一个集合,附加上一个操作,或者说是一种规则.当然还有一个可选环境,这就是函数式的世界啊.


    Array Functions

    Note: All array functions will also work on the arguments object. However, Underscore functions are not designed to work on "sparse" arrays.

    first_.first(array, [n]) Alias: headtake 
    Returns the first element of an array. Passing n will return the first n elements of the array.

    _.first([5, 4, 3, 2, 1]);
    => 5
    

    initial_.initial(array, [n]) 
    Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the last n elements from the result.

    _.initial([5, 4, 3, 2, 1]);
    => [5, 4, 3, 2]
    

    last_.last(array, [n]) 
    Returns the last element of an array. Passing n will return the last n elements of the array.

    _.last([5, 4, 3, 2, 1]);
    => 1
    

    rest_.rest(array, [index]) Alias: tail, drop 
    Returns the rest of the elements in an array. Pass an index to return the values of the array from that index onward.

    _.rest([5, 4, 3, 2, 1]);
    => [4, 3, 2, 1]
    

    compact_.compact(array) 
    Returns a copy of the array with all falsy values removed. In JavaScript, falsenull0,""undefined and NaN are all falsy.

    _.compact([0, 1, false, 2, '', 3]);
    => [1, 2, 3]
    

    flatten_.flatten(array, [shallow]) 
    Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level.

    _.flatten([1, [2], [3, [[4]]]]);
    => [1, 2, 3, 4];
    
    _.flatten([1, [2], [3, [[4]]]], true);
    => [1, 2, 3, [[4]]];
    

    without_.without(array, [*values]) 
    Returns a copy of the array with all instances of the values removed.

    _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
    => [2, 3, 4]
    

    union_.union(*arrays) 
    Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays.

    _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
    => [1, 2, 3, 101, 10]
    

    intersection_.intersection(*arrays) 
    Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.

    _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
    => [1, 2]
    

    difference_.difference(array, *others) 
    Similar to without, but returns the values from array that are not present in the otherarrays.

    _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
    => [1, 3, 4]
    

    uniq_.uniq(array, [isSorted], [iterator]) Alias: unique 
    Produces a duplicate-free version of the array, using === to test object equality. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iterator function.

    _.uniq([1, 2, 1, 3, 1, 4]);
    => [1, 2, 3, 4]
    

    zip_.zip(*arrays) 
    Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. If you're working with a matrix of nested arrays, zip.apply can transpose the matrix in a similar fashion.

    _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
    => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
    

    object_.object(list, [values]) 
    Converts arrays into objects. Pass either a single list of [key, value] pairs, or a list of keys, and a list of values.

    _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
    => {moe: 30, larry: 40, curly: 50}
    
    _.object([['moe', 30], ['larry', 40], ['curly', 50]]);
    => {moe: 30, larry: 40, curly: 50}

    目测可能常用

    indexOf_.indexOf(array, value, [isSorted]) 
    Returns the index at which value can be found in the array, or -1 if value is not present in the array. Uses the native indexOf function unless it's missing. If you're working with a large array, and you know that the array is already sorted, pass truefor isSorted to use a faster binary search ... or, pass a number as the third argument in order to look for the first matching value in the array after the given index.

    _.indexOf([1, 2, 3], 2);
    => 1

    目测可能常用

    lastIndexOf_.lastIndexOf(array, value, [fromIndex]) 
    Returns the index of the last occurrence of value in the array, or -1 if value is not present. Uses the native lastIndexOf function if possible. Pass fromIndex to start your search at a given index.

    _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
    => 4
    

    sortedIndex_.sortedIndex(list, value, [iterator], [context]) 
    Uses a binary search to determine the index at which the value should be inserted into the list in order to maintain the list's sorted order. If an iterator is passed, it will be used to compute the sort ranking of each value, including the value you pass.

    _.sortedIndex([10, 20, 30, 40, 50], 35);
    => 3
    

    range_.range([start], stop, [step]) 
    A function to create flexibly-numbered lists of integers, handy for each and map loops.start, if omitted, defaults to 0step defaults to 1. Returns a list of integers from start tostop, incremented (or decremented) by step, exclusive.

    _.range(10);
    => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    _.range(1, 11);
    => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    _.range(0, 30, 5);
    => [0, 5, 10, 15, 20, 25]
    _.range(0, -10, -1);
    => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
    _.range(0);
    => []

    目测可能常用

    对于Array的这些扩展,目测实用性不太强,当然不排除对别人很有用.而且通过函数式使用Array,不如在Array上扩展,比如象Linq在enumable上的扩展,这样子干实用得多.也方便得多.


  • 相关阅读:
    Powershell 的自己主动部署
    Python 爬虫批量下载美剧 from 人人影视 HR-HDTV
    c :函数指针具体解释
    云计算设计模式(二十二)——静态内容托管模式
    Bash 脚本 getopts为什么最后一个參数取不到
    清理SYSAUX表空间的WRH$_LATCH_CHILDREN表
    Linux配置防火墙,开启80port、3306port 可能会遇到的小问题
    Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件
    Nginx 笔记与总结(3)配置虚拟主机
    用SPSS做时间序列
  • 原文地址:https://www.cnblogs.com/DSharp/p/3142408.html
Copyright © 2011-2022 走看看