Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some functions wrongly marked as pure #232

Closed
vsimko opened this issue Nov 10, 2021 · 2 comments
Closed

Some functions wrongly marked as pure #232

vsimko opened this issue Nov 10, 2021 · 2 comments

Comments

@vsimko
Copy link
Collaborator

vsimko commented Nov 10, 2021

corrplot/R/corrplot.R

Lines 1047 to 1067 in 430e501

#' @note pure function
#' @noRd
draw_method_square = function(coords, values, asp_rescale_factor, fg, bg) {
symbols(coords, add = TRUE, inches = FALSE,
squares = asp_rescale_factor * abs(values) ^ 0.5,
bg = bg, fg = fg)
}
#' @note pure function
#' @noRd
draw_method_color = function(coords, fg, bg) {
symbols(coords, squares = rep(1, nrow(coords)), fg = fg, bg = bg,
add = TRUE, inches = FALSE)
}
#' @note pure function
#' @noRd
draw_grid = function(coords, fg) {
symbols(coords, add = TRUE, inches = FALSE, fg = fg, bg = NA,
rectangles = matrix(1, nrow = nrow(coords), ncol = 2))
}

If these functions are drawing something, this means they cannot be pure because they have a side effect.

@taiyun
Copy link
Owner

taiyun commented Nov 10, 2021

What is pure function?

@vsimko
Copy link
Collaborator Author

vsimko commented Nov 10, 2021

Well, a pure function is such a function which does not have any side effects and for any input it always returns the same output. For example, a mathematical function such as f(x) = x^2 is pure. However, function f(x) = sample(1:10, x) isn't pure because it returns different random values for the same input. Also a function f(x) = print(x) isn't pure because it causes a side effect (printing to console).

Pure functions behave nicely, they can be easily tested and reason about.

@taiyun taiyun closed this as completed in d717c42 Nov 11, 2021
taiyun added a commit that referenced this issue Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants