rfunctional-programmingtacit-programming

Is tacit programming possible in the statistical programming language R?


Is tacit programming also known as point-free style - an option in R?


Solution

  • Check magrittr package since it seems closest to what you are asking. Wikipedia quotes an example:

    For example, a sequence of operations in an applicative language like the following:

    def example(x):
       y = foo(x)
       z = bar(y)
       w = baz(z)
       return w
    

    ...is written in point-free style as the composition of a sequence of functions, without parameters:

    def example: baz bar foo

    In R with magrittr it could be written as

    x %>% foo %>% bar %>% baz
    

    where %>% operator is used to compose a chain of functions, so that the output of a previous function is passed as a first argument of subsequent function. See the magrittr vignette for learning more.

    The function could be defined

    # explicitly
    example <- function(x) x %>% foo %>% bar %>% baz
    
    # or simply (as @bergant noticed)
    example <- . %>% foo %>% bar %>% baz