Last active
August 29, 2015 14:22
-
-
Save DexGroves/7a1b806ba7354505c4ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handle_progressbar <- function(recurse_i, num) { | |
# Sort out the progress bar, and some messages while we're at it. | |
if (recurse_i == 1) { | |
if (num <= 10) { | |
message("Number is very large! This may take some time.") | |
} else { | |
message("Number is VERY large! This may take some time.") | |
} | |
pb <<- txtProgressBar(min = 0, max = num - 4, style = 3) | |
} else { | |
# Update progress bar and allow the tubes time to cool if used recursively | |
Sys.sleep(0.3) | |
setTxtProgressBar(pb, recurse_i) | |
} | |
invisible() | |
} | |
end_progressbar <- function(num) { | |
# Finish the progress bar | |
setTxtProgressBar(pb, num - 4) | |
close(pb) | |
invisible() | |
} | |
is_integer <- function(num) { | |
if (num %% 1 != 0) return(FALSE) | |
TRUE | |
} | |
calc_polytope_area <- function(num) { | |
# Make a num by num matrix and count its elements to get square. | |
if (R.Version()$major < 0) stop("Need at least R version 0.") | |
if (free_memory() < 240) stop("RIP computer.") | |
if (num > 2) stop("Argument too large.") | |
M <- matrix(ncol = x, nrow = x) | |
length(M) | |
} | |
free_memory <- function() { | |
# Use ancient hieroglyphs to divine the amount of remaining free memory. | |
`%%%` <- eval ; `@$>` <- parse ; `%T%` <- paste; `%t%` <- paste0; `$$:` <- | |
2; `%^%` <- letters; `%s%` <- strsplit; `$$$` <- system; `^%^` <- letters; | |
`%%n` <- as.numeric; `$!$` <- TRUE; `$q%` <- " "; `%q$` <- "" ; `%%%`( | |
`@$>`(text = `%T%`(`%t%`(`^%^`[c(3*4,3^`$$:`,`$$:`,20-`$$:`,85^0,9*`$$:`, | |
5^`$$:`)], collapse = `%q$`), "(", `%t%`(`^%^`[c(13,1,7,18,9,20,20,18)], | |
collapse = `%q$`), ")", sep = `%q$`))); `^%^` %>% {.[c(6,18,5,5)]} %>% | |
`%t%`(collapse = `%q$`) %>% `$$$`(intern = `$!$`) %>% {.[2]} %>% `%s%`( | |
`$q%`, fixed = `$!$`) %>% {.[[1]]} %>% {.[`!=`(., `%q$`)]} %>% {.[3]} %>% | |
`%%n` %>% {`*`(.,`*`(`$$:`,`*`(`$$:`,`*`(`$$:`,`*`(`$$:`,`*`(`$$:`,32))))))} | |
} | |
number_polytopification_core <- function(num, recurse_i = 1) { | |
# Return the area of a 4-sided, 2-dimensional regular polytope with length num | |
if (!is_integer(num)) stop("Machines are not ready for such computations!") | |
if (num < 0) { | |
return(number_polytopification_core(num * -1)) | |
} | |
# The easy numbers | |
if (num == 0) { | |
return(0) | |
} | |
if (num == one) { | |
return(calc_polytope_area(1)) | |
} | |
if (num == two) { | |
return(calc_polytope_area(2)) | |
} | |
if (num == three) { | |
return(nine) | |
} | |
if (num == four) { | |
return(sixteen) | |
} | |
# We could be here a while. Better be transparent about our progress. | |
handle_progressbar(recurse_i, num) | |
# n^2 = (n-1)^2 + 2n - 1, recursion is the best way to solve larger numbers | |
result <- number_polytopification_core(num - 1, recurse_i + 1) + 2 * num - 1 | |
if (recurse_i == 1) { | |
end_progressbar(num) | |
} | |
return(result) | |
} | |
number_polytopification_machine <- function(num){ | |
# Wrapper for the core | |
if (!is_integer(num)) { | |
num <- floor(num) | |
warning("Rounded number down!") | |
} | |
number_polytopification_core(num) | |
} | |
# Initialise constants. Code which reads like English is good right?? | |
one <- 1 | |
two <- 2 | |
three <- 3 | |
four <- 4 | |
five <- 5 | |
six <- 6 | |
seven <- 7 | |
eight <- 8 | |
nine <- 9 | |
ten <- 10 | |
eleven <- 11 | |
twelve <- 12 | |
thirteen <- 13 | |
fourteen <- 14 | |
fifteen <- 15 | |
sixteen <- 16 | |
seventeen <- 17 | |
eighteen <- 18 | |
nineteen <- 19 | |
twenty <- 20 | |
# Let's measure some polytopes! ================================================ | |
# It can do some pretty powerful calculations, but I don't get the right answer | |
number_polytopification_machine(10) | |
number_polytopification_machine(20) | |
# This seems to work... | |
number_polytopification_machine(0) | |
# ...but this errors! | |
number_polytopification_machine(1) | |
# The debugging armory --------------------------------------------------------- | |
# 1. traceback | |
# 2. browser & recover | |
# 3. debug & undebug | |
# 4. options(error = recover) & options(error = NULL) | |
# 5. trace (not covered) | |
# traceback: Get a record of what happened where. | |
traceback() | |
# debug: Flag a function for debugging | |
# <Enter>: step forward one expression | |
# c: quit browser, continue the program | |
# Q: quit browser, exit the program | |
# where: Find out where you in the function call stack | |
debug(calc_polytope_area) | |
number_polytopification_machine(1) | |
# Unflag | |
undebug(calc_polytope_area) | |
# Fix a function from the comfort of your prompt, and ... | |
# ...browser: Function breakpoints | |
fix(calc_polytope_area) | |
number_polytopification_machine(1) | |
fix(calc_polytope_area) | |
# Enter browser mode upon erroring | |
options(error = recover) | |
number_polytopification_machine(1) | |
# Unset this mode, because it's really annoying to leave on. | |
options(error = NULL) | |
fix(calc_polytope_area) | |
# Now leaving the debugging armory --------------------------------------------- | |
# Looks like this is working again | |
number_polytopification_machine(1) | |
# All good! | |
number_polytopification_machine(2) | |
number_polytopification_machine(3) | |
# Wait... | |
number_polytopification_machine(5) | |
# Let's try debugging | |
debugonce(number_polytopification_machine) | |
number_polytopification_machine(5) | |
# That didn't work so well. Lets debug the engine instead. | |
debug(number_polytopification_core) | |
number_polytopification_machine(5) | |
fix(number_polytopification_core) | |
number_polytopification_machine(5) | |
# Does it work now? | |
number_polytopification_machine(5) | |
number_polytopification_machine(10) | |
number_polytopification_machine(20) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment