What’s new in futile.paradigm 2.0.4

February 6, 2012

(This article was first published on Cartesian Faith » R, and kindly contributed to R-bloggers)

Well this certainly took a while but the latest installment of my functional dispatching library for R is finally released and pushed to CRAN. As a major point release, it has some breaking changes, although the older style of 1.x is supported via an option (more later).

The 2.x series of futile.paradigm introduces a cleaner syntax that doesn’t clutter the environment. All function variants are attached to a single function reference, which means multi-part definitions are much cleaner than in 1.x.

fact %when% (length(x) == 1)
fact %as% function(x) { fact(1:x) }

fact %when% (length(x) > 1)
fact %as% function(x) { prod(x) }

Adding additional guards is done by using the %also% operator.

fact %when% is.numeric(x)
fact %also% (length(x) == 1)
fact %as% function(x) { fact(1:x) }

Default function variants can now be defined in case all others fail. This only works when the argument counts match, although you’re safe if you use the ellipsis argument here.

fact %default% function(x) { fact(as.numeric(x) }

Now calling the function is exactly how you would expect. Functional polymorphism works as expected and all function variants have clear code paths, making each definition remarkably concise.

> fact(5)
[1] 120
> fact("5")
[1] 120
> fact(1:4)
[1] 24

What about optional arguments? I’ve teetered back and forth on whether this is a good idea (always deciding on no), but there are circumstances where variables should be passed along for compatibility with other dispatching systems. When this need arises, it’s easiest to simply pass in a list as an extra parameter and pass it along as necessary. Sometimes I’ll just attach() the list to the environment inside the function definition, which makes it easy to access any variables specified.

This post is a brief introduction to futile.paradigm and how to structure code in a functional way in R. There are other fun features in version 2.0 of futile.paradigm that I will discuss in later posts, but this covers the bare essentials.

Code Listing

Here’s a complete code listing for convenience.
fact %when% is.numeric(x)
fact %also% (length(x) == 1)
fact %as% function(x) { fact(1:x) }

fact %when% (length(x) > 1)
fact %as% function(x) { prod(x) }

fact %default% function(x) { fact(as.numeric(x)) }

To leave a comment for the author, please follow the link and comment on their blog: Cartesian Faith » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)