pike.git / lib / modules / Array.pmod

version» Context lines:

pike.git/lib/modules/Array.pmod:11:   constant sort = __builtin.sort;   constant everynth = __builtin.everynth;   constant splice = __builtin.splice;   constant transpose = __builtin.transpose;   constant uniq = __builtin.uniq_array;      constant filter=predef::filter;   constant map=predef::map;   constant permute = __builtin.permute;    + //! @[reduce()] sends the first two elements in @[arr] to @[fun], + //! then the result and the next element in @[arr] to @[fun] and + //! so on. Then it returns the result. The function will return + //! @[zero] if @[arr] is the empty array. If @[arr] has + //! only one element, that element will be returned. + //! + //! @seealso + //! @[Array.rreduce()] + //!   mixed reduce(function fun, array arr, mixed|void zero)   {    if(sizeof(arr))    zero = arr[0];    for(int i=1; i<sizeof(arr); i++)    zero = ([function(mixed,mixed:mixed)]fun)(zero, arr[i]);    return zero;   }    -  + //! @[rreduce()] sends the last two elements in @[arr] to @[fun], + //! then the third last element in @[arr] and the result to @[fun] and + //! so on. Then it returns the result. The function will return + //! @[zero] if @[arr] is the empty array. If @[arr] has + //! only one element, that element will be returned. + //! + //! @seealso + //! @[Array.reduce()] + //!   mixed rreduce(function fun, array arr, mixed|void zero)   {    if(sizeof(arr))    zero = arr[-1];    for(int i=sizeof(arr)-2; i>=0; --i)    zero = ([function(mixed,mixed:mixed)]fun)(arr[i], zero);    return zero;   }    -  + //! @[shuffle()] gives back the same elements, but in random order. + //! + //! @seealso + //! @[Array.permute()] + //!   array shuffle(array arr)   {    int i = sizeof(arr);       while(i) {    int j = random(i--);    if (j != i) {    mixed tmp = arr[i];    arr[i] = arr[j];    arr[j] = tmp;    }    }    return(arr);   }    -  + //! @[search_array()] works like @[map()], only it returns the index + //! of the first call that returnes true instead. + //! + //! If no call returns true, -1 is returned. + //! + //! @seealso + //! @[Array.sum_arrays()], @[map()] + //!   int search_array(array arr, mixed fun, mixed ... args)   {    int e;       if(stringp(fun))    {    for(e=0;e<sizeof(arr);e++)    if(([function(mixed...:mixed)]([array(object)]arr)[e][fun])(@args))    return e;    return -1;
pike.git/lib/modules/Array.pmod:75:    if(([array(function(mixed...:mixed))]arr)[e](@args))    return e;    return -1;    }    else    {    error("Bad argument 2 to filter().\n");    }   }    - array sum_arrays(function foo, array(mixed) ... args) + array sum_arrays(function sum, array(mixed) ... args)   {    array ret;    int e,d;    ret=allocate(sizeof(args[0]));    for(e=0;e<sizeof(args[0]);e++) -  ret[e]=([function(mixed...:mixed)]foo)(@ column(args, e)); +  ret[e]=([function(mixed...:mixed)]sum)(@ column(args, e));    return ret;   }    - array sort_array(array foo,function|void cmp, mixed ... args) + //! This function sorts an array after a compare-function @[cmp] + //! which takes two arguments and should return @tt{1@} if the first argument + //! is larger then the second. + //! + //! The remaining arguments @[args] will be sent as 3rd, 4th etc. argument + //! to @[cmp]. + //! + //! If @[cmp] is omitted, @[`>()] is used instead. + //! + //! @seealso + //! @[map()], @[sort()], @[`>()] + //! + array sort_array(array foo, function|void cmp, mixed ... args)   {    array bar,tmp;    int len,start;    int length;    int foop, fooend, barp, barend;       if(!cmp || cmp==`>)    {    foo+=({});    sort(foo);