More functional approach to R LED

Note

This is my functional programming and unit testing exercise using Blag's LED example. '

References

Load packages

library(magrittr)
library(testthat)

Code with unit tests

## Define a number/number string to individual digit converter
IndividualDigits <- function(number) {
    as.character(number) %>%
        strsplit(split = "") %>%
        `[[`(1) %>%
        as.numeric( )

}
## Unit testing
test_that(IndividualDigits(1231),    equals(c(1,2,3,1)))
test_that(IndividualDigits("01231"), equals(c(0,1,2,3,1)))


## Define a fuction to return appropriate string for a given line and digit
NthLineFor1Digit <- function(nth, digit) {

    line  <- rep(1:3, each = 10)
    index <- rep(0:9, 3)

    map <- c(" _  ", "  ", " _  ", "_  ", "    ", " _  ", " _  ", "_   ", " _  ", " _  ",
             "| | ", "| ", " _| ", "_| ", "|_| ", "|_  ", "|_  ", " |  ", "|_| ", "|_| ",
             "|_| ", "| ", "|_  ", "_| ", "  | ", " _| ", "|_| ", " |  ", "|_| ", " _| ")

        map[line == nth & index == digit]
}
## Unit testing
test_that(NthLineFor1Digit(1,0), equals(" _  "))
test_that(NthLineFor1Digit(1,9), equals(" _  "))
test_that(NthLineFor1Digit(2,9), equals("|_| "))
test_that(NthLineFor1Digit(3,9), equals(" _| "))


## Define a function to return the whole line
NthLineForAllDigits <- function(nth, digits) {

    lapply(digits, function(digit) {

        NthLineFor1Digit(nth, digit)
    })  %>%
        unlist( . )  %>%
        paste0( . , collapse = "")
}
## Unit testing
test_that(NthLineForAllDigits(1, 0:9),
          equals(paste0(" _  ", "  ", " _  ", "_  ", "    ", " _  ", " _  ", "_   ", " _  ", " _  ", collpase = "")))
test_that(NthLineForAllDigits(2, 0:9),
          equals(paste0("| | ", "| ", " _| ", "_| ", "|_| ", "|_  ", "|_  ", " |  ", "|_| ", "|_| ", collpase = "")))
test_that(NthLineForAllDigits(3, 0:9),
          equals(paste0("|_| ", "| ", "|_  ", "_| ", "  | ", " _| ", "|_| ", " |  ", "|_| ", " _| ", collpase = "")))


## Define a function to return the whole thing as a vector of 3 elements
AllLinesForAllDigits <- function(digits) {

    lapply(1:3, function(nth) {

        NthLineForAllDigits(nth, digits)
    })
}
## Unit testing
expectedList <-
    list(paste0(" _  ", "  ", " _  ", "_  ", "    ", " _  ", " _  ", "_   ", " _  ", " _  ", collpase = ""),
         paste0("| | ", "| ", " _| ", "_| ", "|_| ", "|_  ", "|_  ", " |  ", "|_| ", "|_| ", collpase = ""),
         paste0("|_| ", "| ", "|_  ", "_| ", "  | ", " _| ", "|_| ", " |  ", "|_| ", " _| ", collpase = ""))
test_that(AllLinesForAllDigits(0:9), equals(expectedList))


## Define a function to convert a given number to three lines of strings
AllLinesFor1Number <- function(number) {

    number %>%
        IndividualDigits( . ) %>%
        AllLinesForAllDigits( . )
}
## Unit testing
test_that(AllLinesFor1Number(paste0(0:9, collapse = "")), equals(expectedList))


## Define a user frontend function
DisplayDigits <- function(number) {

    number %>%
        AllLinesFor1Number( . ) %>% 
        lapply( . , function(line) {
            paste0(line, "\n") %>%
                cat
        }) -> discard
}

DisplayDigits(paste0(0:9, collapse = ""))
##  _     _  _       _   _  _    _   _  
## | | |  _| _| |_| |_  |_   |  |_| |_| 
## |_| | |_  _|   |  _| |_|  |  |_|  _|
DisplayDigits(2014)
##  _   _        
##  _| | | | |_| 
## |_  |_| |   |
DisplayDigits("08")
##  _   _  
## | | |_| 
## |_| |_|
DisplayDigits(31)
## _    
## _| | 
## _| |