This file contains hidden or 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
xml.gsub(/[0-9]{5,15}/) { |y| y =~ /^000/ ? y : y.split('').map{|w|0}.join} |
This file contains hidden or 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
as.list.formula <- function(x) { | |
output <- vector('list', length(x)) | |
for(i in seq_len(length(x))) { | |
could_be_formula <- length(x[[i]]) > 1 && is.element("~", all.names(x[[i]])) | |
output[[i]] <- | |
if (could_be_formula) as.list(as.formula(x[[i]])) | |
else if (is.recursive(x[[i]])) as.list(x[[i]]) | |
else x[[i]] | |
} | |
output |
This file contains hidden or 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
# Reload the suite of Syberia packages. This will be automated later. | |
require(devtools) | |
pkgs <- strsplit("Ramd productivus stagerunner statsUtils mungebitsTransformations mungebits tundra syberia", " ")[[1]] | |
lapply(list( | |
list(function(x) if(x %in% loadedNamespaces()) unloadNamespace(x), rev = TRUE), | |
list(install_github, 'robertzk'), | |
list(library, character.only = TRUE) | |
), function(x) { r <- x$rev; x$rev <- NULL; do.call(lapply, append(list(if (is.null(r)) pkgs else rev(pkgs)), x)) }) |
This file contains hidden or 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
def mystery_method(x) | |
->(z) do | |
x.inject(z) { |f, v| f(v) } | |
end | |
end |
This file contains hidden or 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
exists <- function(name, env = parent.frame(), inherits = TRUE) { | |
if (identical(env, emptyenv())) return(FALSE) | |
if (name %in% ls(envir = env)) TRUE | |
else if (inherits) exists(name, parent.env(env)) | |
else FALSE | |
} |
This file contains hidden or 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
fget <- function(name, env = parent.frame(), inherits = TRUE) { | |
if (identical(env, emptyenv())) stop(name, ' not found') | |
if (exists(name, env = env, inherits = FALSE) && is.function(tmp <- env[[name]])) return(tmp) | |
else if (inherits) fget(name, parent.env(env)) | |
else stop(name, ' not found') | |
} |
This file contains hidden or 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
get <- function(name, env = parent.frame()) { | |
if (identical(env, emptyenv())) stop(name, ' not found') | |
if (exists(name, env = env, inherits = FALSE)) return(env[[name]]) | |
else get(name, parent.env(env)) | |
} |
This file contains hidden or 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
"random<-" <- function(object, position, value) { | |
object[sample(seq_along(object), 1)] <- value | |
object | |
} | |
# Example: | |
# > x <- c(1,2,3,4,5) | |
# > random(x) <- 10 | |
# > x | |
# [1] 1 10 3 4 5 |
This file contains hidden or 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
infixed_names <- list(intersect = '∩', union = 'U', setdiff = '-') | |
create_infix_operator <- function(name, base_fn, e) { | |
name <- paste("%", name, "%", sep = '') | |
assign(name, function(x, y) get(base_fn, envir = e)(x, y), envir = e) | |
} | |
lapply(names(infixed_names), function(name) { | |
create_infix_operator(infixed_names[[name]], name, e = parent.frame(2)) | |
}) |
This file contains hidden or 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
`%xor%` <- function(x,y) (x || y) && !(x && y) |