# Writing Pipe-friendly Functions

Pipes have been a fundamental aspect of computer programming for many decades. In short, the semantics of pipes can be thought of as taking the output from the left-hand side and passing it as input to the right-hand side. For example, in a linux shell, you might `cat example.txt | sort | uniq` to take the contents of a text file, then sort the rows, then take one copy of each distinct value. `|` is a common, but not universal, pipe operator and on U.S. Qwerty keyboards, is found above the RETURN key along with the backslash: `\`.

Languages that don’t begin by supporting pipes often eventually implement some version of them. In R, the magrittr package introduced the `%>%` infix operator as a pipe operator and is most often pronounced as “then”. For example, “take the `mtcars` data.frame, THEN take the `head` of it, THEN…” and so on.

# Three Deep Truths About R

So, what are the implications of these statements?

# step 0: assume a malicious universe

Here’s a thought puzzle for you… given the following line of computer code, “what could go wrong?” That is, what kinds of issues could arise from submitting that code to your favorite programming language interpreter (you do have a favorite… right?)

`n + 4`

# I’m ‘not in’ right now…

Checking whether an item is in a vector or not in a vector is a common task. The notation in R is a little inelegant when expressing the “not in” condition since the negation operator (`!`) is separated from the comparison operator (`%in%`):

```5 %in% c(1, 2, 3, 4, 5)  # TRUE
!5 %in% c(1, 2, 3, 4, 5) # FALSE```

R is a language where you can easily extend the set of built in operators:

````%!in%` <-
function(needle, haystack) {
!(needle %in% haystack)
}```

Now, I can express my intentions reasonably clearly with my new, compact, infix operator `%!in%`:

```5 %in% c(1, 2, 3, 4, 5)  # TRUE
5 %!in% c(1, 2, 3, 4, 5) # FALSE```

Moral: bend your tools to your will, not the other way ’round.

# Defensively install packages in R

Often, your R code will rely on having one or more R packages available. A little defensive coding will save users of your code—including future-you—from having to figure out which packages you’re using and then having to manually install them. This lowers the extraneous cognitive load associated with running older or unfamiliar code.

`if (!"tidyverse" %in% installed.packages()) install.package("tidyverse", deps = TRUE)`

Or, if you prefer to always use blocks with IF statements:

```if (!"tidyverse" %in% installed.packages()) {
install.package("tidyverse", deps = TRUE)
}```

With a little persistence, you can extend this to dealing with multiple packages:

```pkgs <- c("tidyverse", "openxlsx")
install.packages(pkgs[!pkgs %in% installed.packages()], deps = TRUE)
```

# Getting started with R

R provides the backend: the programming language specification and the interpreter.

RStudio provides the frontend: the user interface that allows you to interact with R, visualize data, and manage the files associated with your analyses.

R for Data Science introduces you to the tidyverse way of programming. There are basically methods of programming in R: “base R”, which has been around since the R language was first conceived (and before, since R is itself based on the S language), and the tidyverse, a newer approach that focuses on leveraging a consistent structure to your data and developing a grammar for data ingest, data wrangling, data visualization, and data storage.

Base R tends to be dense in meaning where the Tidyverse tends to be consistent and to breakdown complex processes into a set of discrete steps:

 base R Tidyverse `mtcars[2, "cyl"]` ```library(tidyverse) mtcars %>% select(cyl) %>% slice(2)``` `mtcars[mtcars\$cyl == 4, c("hp", "mpg")]` ```library(tidyverse) mtcars %>% filter(cyl == 4) %>% select(hp, mpg)```

# Programming Languages are Only the Beginning

Programming languages are tools to express programmer intentions. Why, then, do we suffer the indignities of inelegant notation when we might, instead, bend the language to capture our meaning better?

If you’ve written code, you’ve likely accessed the first and last elements of an array:

```var grades = [80, 90, 85];

How many times have you written `[0]`? `[arr.length - 1]`? Or worse, `[arr.length]`, resulting in an off-by-1 error?

What we mean here is “the first element” and “the last element”. Unfortunately, JavaScript doesn’t provide a method on Array objects to extract the first or last elements.

```> grades.first()

So let’s update the language to clarify that meaning. JavaScript is a prototypal language: There is an Array prototype which all instances of arrays are based on. By adding methods to the Array prototype, we immediately add those methods to every instance of an array.

```Array.prototype.first = function() { return(this[0]); }
Array.prototype.last = function() { return(this[ this.length - 1 ]); }```

Now, we can easily and without fear of off-by-1 errors access the first and last elements:

```> grades.first()
< 80
< 85```

But let’s not stop there… what other functions might it be useful to have? How would you enhance the language to provide those functions?

Any function you write provides an opportunity to make your intentions clearer and to create a domain specific language that allows you to express solutions to problems that interest you more naturally. Use it to your advantage.

# FizzBuzz in JavaScript

Functions are first class objects. Functions establish closures.

Problem: Given a range of positive, non-zero integers, output “Fizz” if the number is evenly divisible by 3, output “Buzz” is the number is evenly divisible by 5, and output “FizzBuzz” if the number is evenly divisible by both 3 and 5; otherwise, output the number.

```divisor = function(number, string) {
return(function(d) {
if (d % number === 0) {return(string)} else {return("")};
});
}

mod3er = divisor(3, "Fizz");
mod5er = divisor(5, "Buzz");

for(i = 1; i <= 100; i = i + 1) {
res = mod3er(i) + mod5er(i);
console.log(res === "" ? i : res);
}
```

# FizzBuzz in R

Functions are first class objects in R. Functions establish closures also known in R as environments. So, you can use functions to create other functions in creative ways.

Here, I’ve written a function called `divisor` that returns a function that checks whether a given input, `d`, is evenly divisible by `number` and if so, returns `string`. Then I use `divisor` to create a test for divisibility by 3 and another for divisibility by 5.

Problem: Given a range of positive, non-zero integers, output “Fizz” if the number is evenly divisible by 3, output “Buzz” if the number is evenly divisible by 5, and output “FizzBuzz” if the number is evenly divisible by both 3 and 5; otherwise, output the number.

Solution:

```divisor <-
function(number, string) {
function(d) {
if (d %% number == 0) string else ""
}
}

mod3er <- divisor(3, "Fizz")
mod5er <- divisor(5, "Buzz")

fizzbuzz <-
function(i) {
res <- paste0(mod3er(i), mod5er(i))
ifelse(res == "", i, res)
}

sapply(1:100, fizzbuzz)
```

# Mike Monteiro @ WebStock ’13: How Designers Destroyed the World

Mike offers some blunt and intense advice about maintaining absolute integrity in one’s work. While he’s addressing his concerns to designers, I take his advice to apply equally well to computer programmers, UX, UI, teachers… any profession where you’re creating… and really, shouldn’t that be all professions?

# Bret Victor Speaking on Inventing on Principle

Bret offers some interesting insights into the importance of immediate, direct feedback while learning to program – really, while programming at all in target=”_blank”>his CUSEC talk from early 2012.

# Automatically Generating an HTML5-style Cache Manifest from the Command Line

HTML5 introduces the ability to cache content client-side so that often-used resources can be used without re-downloading them. This also enables a site to be viewed from the client when no network connection is available (i.e., offline viewing of the site).

In order for this to work, there are a few things one must do:

1. Create a plain text file listing all of the resources that should be cached by the user agent (e.g., a web browser)– the cache manifest.
2. Refer to that file in the opening html tag of every page that will use cached resources.
3. Configure the web server so that the file is sent to the user agent with a specific MIME type: text/cache-manifest
4. Regenerate the cache manifest any time you change the files in your site.
Once everything is setup properly, you can visit the site using your favorite web browser. Then, to test whether the caching has worked, you can turn off the network connection to your web browser’s computer and try reloading the page.

# Thinking Through A Basic Pong Game in Processing

The Problem: Create a basic 1970s style Pong game for one player using the Processing programming language. The paddle will be on the right and the ball will bounce off of the three other sides. If the ball passes the paddle while the ball is traveling to the right, game play ends. The paddle will be controlled by the keyboard’s UP and DOWN arrow keys.

# Computational Efficiency

I’d like to give a brief overview of computational efficiency, since it’s a topic that has come up in a few conversations recently. The super short version is this: it’s often helpful to understand the resource (time, space, or power) needs for a given algorithm. Why? Because we want the fastest algorithm, or the one that uses the least amount of storage on our hard drive. In extreme computing environments (think Mars Rover, Apollo capsules, etc), we many have very limited resources available. For example, your digital wrist watch almost certainly has more memory than the Apollo capsules, which only had about 32KB of RAM. How can you possibly land a person on the moon with only 32KB?!!

# Goodbye, Hello World?

Alfred Thompson questioned on his blog today whether the customary first programming exercise, Hello World, should be replaced with something that’s more flexible and calls on students to engage in a short, non-trivial first act as a programmer. I admit, I’ve used Hello World myself with students, but usually not as a first activity. Instead, I use Hello World to help students who have had some hours or days of programming instruction understand that they now know quite a bit about how programming languages express an intention. I ask students to visit the ACM Hello World web page and compare and contrast that simple program in different languages. How are code blocks started and ended? How is output generated? How is an infinite loop expressed? How are strings represented?

# Learning to Write in English like Learning to Program

Imagine for a moment that you were going to teach writing in standard English in the same way we tend to teach computer programming.

Alright… Let’s learn to write. Before you can write, you need to know about the fundamentals of the language we’re going to use. A language is a collection of words and rules for how you combine those words. Words can be thought of as being of different types that determine the purpose and meaning of the words. For example, two types we’ll work with are nouns and interjections. There are other types, too, but we’ll get to those later.

For now, let’s write your first sentence. A sentence is a valid sequence of words. By valid we mean that the sentence would be recognized by an expert speaker of the language as being acceptable.

So, we need an example of a noun and an interjection to get us started… One frequently used noun is the word WORLD and a common interjection is HELLO.

# Media-Propelled introduction to Computational Thinking

Eric Freudenthal of the iMPaCT: a Media-Propelled introduction to Computational Thinking project spoke at SIGCSE 2010 about how to engage students who are math phobic with computation and, thereby, with math. Using Python and computation about dynamic systems, students work to understand how code == math == concepts. One issue raised was how ethical it is to mislead students initially about whether they’re learning “math”. Eric’s argument: if students know they’re learning math, they fallback on unsuccessful rote memorization techniques. If, however, they believe they are working with dynamic systems to understand how the system changes as parameters are adjusted, then students engage and experiment.

# Programming Style Guide

Every programmer and programming language has a preferred variation on how to format code. Here are my best suggestions for the languages I tend to code.

# iPhone Web App Development

This 4-week workshop, hosted by CreativeTechs and O’Reilly Media is an excellent introduction to how to create website-based applications for use on the iPhone. Website-based apps don’t need to go through the notorious Apple review process for applications, since they are essentially web pages built using standard HTML, CSS, and JavaScript tools and are accessed using Mobile Safari. Once on your app page, users can choose to add the page to their iPhone apps list as an icon, which allows the page to appear without the browser’s chrome (the standard widgets that appear in a web browser: address bar, back/forward buttons, etc.).

Led by Elisabeth Robson, coauthor of Head First HTML with CSS & XHTML, these sessions have been wonderful and are well worth your time. Until 2010 February 5, the entire set of recorded sessions + sample book chapters + sample code can be purchased either from Creative Techs or O’Reilly for just 35\$.

# KUIT245: Web Development

Location: Kaplan University, Online
Terms: 3
Class size: ~ 20 students/term

Students in IT245 create websites using standards-compliant technologies: XHTML, CSS, and JavaScript. Students learn about site planning, page design, site development, and workflow control. They also implement basic forms processing using hosted forms solutions.