[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Iternating over multiple lists



On Tue, 11 Apr 2000, Bernie Cosell wrote:
> What perl needed to make the solution to this problem trivial (and 
> elegant) is "multiple map" (or, if you rather "parallel map").  That 
> is, imagine if you could do:
> 
>    mmap {BLOCK} list1 list2 list3 list4 ....
> 

I agree with the objection to the name mmap = memory map to me.

But why not borrow from R, a luffly stats scripting language with Scheme
like semantics... (Visit CRAN http://www.stat.cmu.edu/R/CRAN/ to find
out more.) Needless to say, CRAN performs the exact role in the R world as
CPAN does in the P... world.

R has such things as apply, lapply, sapply, tapply, map....

I once wrote a fairly major program in R that didn't have a single looping
statement in it!

I fairly sure one can emulate all these functions in Perl.


?apply

   apply {base}                                 R Documentation

   Apply Functions Over Array Margins

   Usage:

        apply(x, MARGIN, FUN, ...)

   Arguments:

          x: the array to be used.

     MARGIN: a vector giving the subscripts which the function
             will be applied over.  `1' indicates rows, `2'
             indicates columns, `c(1,2)' indicates rows and
             columns.

        FUN: the function to be applied.  In the case of func-
             tions like `+', `%*%', etc., the function name
             must be quoted.
                                                           
        ...: optional arguments to `FUN'.

   Value:

        If each call to `FUN' returns a vector of length `n',
        then `apply' returns an array of dimension
        `c(n,dim(x)[MARGIN])' if `n > 1'.  If `n' equals `1',
        `apply' returns a vector if `MARGIN' has length 1 and
        an array of dimension `dim(x)[MARGIN]' otherwise.

        If the calls to `FUN' return vectors of different
        lengths, `apply' returns a list of length `dim(x)[MAR-
        GIN]'.

   See Also:

        `lapply', `tapply', and conveniency functions `sweep'
        and `aggregate'.

   Examples:

        ## Compute row and column sums for a matrix:
        x <- cbind(x1 = 3, x2 = c(4:1, 2:5))           
        dimnames(x)[[1]] <- letters[1:8]
        apply(x, 2, mean, trim = .2)
        col.sums <- apply(x, 2, sum)
        row.sums <- apply(x, 1, sum)
        rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums,
sum(col.sums)))

        all( apply(x,2, is.vector)) # TRUE [was not in R <= 0.63.2]

        ## Sort the columns of a matrix
        apply(x, 2, sort)

        ##- function with extra args:
        cave <- function(x, c1,c2) c(mean(x[c1]),mean(x[c2]))
        apply(x,1, cave,  c1="x1", c2=c("x1","x2"))

        ma <- matrix(c(1:4, 1, 6:8), nr = 2)
        ma
        apply(ma, 1, table)  #--> a list of length 2
        apply(ma, 1, quantile)# 5 x n matrix with rownames

        all(dim(ma) == dim(apply(ma, 1:2, sum)))## wasn't ok before R
0.63.1
 

John Carter

Work Email : john@netsys.co.za Private email : cyent@mweb.co.za 
Yell Phone : 083-543-6915      Phone         : 27-12-348-4246

Death is nature's gentle way of saying, "Relax".


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe