Skip to content

Instantly share code, notes, and snippets.

Avatar

Robert Krzyzanowski robertzk

View GitHub Profile
View gist:7816312
`||` <- function(a,b) if(is.null(a)) b else base::`||`(a,b)
@robertzk
robertzk / gist:7816384
Created Dec 6, 2013
R exercise: http://adv-r.had.co.nz/Functions.html Create a list of all the replacement functions found in the base package. Which ones are primitive functions?
View gist:7816384
x <- ls("package:base")
x[substring(x, nchar(x)-1, nchar(x)) == '<-']
# "[[<-" "[<-" "@<-" "<-" "<<-" "$<-" "attr<-" "attributes<-" "body<-" "class<-" "colnames<-" "comment<-" "diag<-" "dim<-"
# "dimnames<-" "Encoding<-" "environment<-" "formals<-" "is.na<-" "length<-" "levels<-" "mode<-" "mostattributes<-" "names<-" "oldClass<-" "parent.env<-" "regmatches<-" "row.names<-"
# "rownames<-" "split<-" "storage.mode<-" "substr<-" "substring<-" "units<-"
@robertzk
robertzk / xor.r
Created Dec 6, 2013
R exercise: Create an infix xor() operator. http://adv-r.had.co.nz/Functions.html
View xor.r
`%xor%` <- function(x,y) (x || y) && !(x && y)
@robertzk
robertzk / infixed.r
Created Dec 6, 2013
R exercise: Create infix versions of set functions: intersect(), union(), setdiff() http://adv-r.had.co.nz/Functions.html
View infixed.r
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))
})
@robertzk
robertzk / randomreplace.r
Created Dec 6, 2013
R exercise: Create a replacement function that modifies a random location in vector. http://adv-r.had.co.nz/Functions.html
View randomreplace.r
"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
@robertzk
robertzk / get.r
Last active Dec 30, 2015
R exercise: Write your own version of get() using a function written in the style of where(). http://adv-r.had.co.nz/Environments.html
View get.r
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))
}
@robertzk
robertzk / fget.r
Created Dec 6, 2013
R exercise: Write a function called fget() that finds only function objects. It should have two arguments, name and env, and should obey the regular scoping rules for functions: if there's an object with a matching name that's not a function, look in the parent. (This function should be a equivalent to match.fun() extended to take a second argum…
View fget.r
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')
}
@robertzk
robertzk / exists.r
Created Dec 6, 2013
R exercise: Write your own version of exists(inherits = FALSE) (Hint: use ls()). Write a recursive version that behaves like inherits = TRUE. http://adv-r.had.co.nz/Environments.html
View exists.r
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
}
View mystery_method.rb
def mystery_method(x)
->(z) do
x.inject(z) { |f, v| f(v) }
end
end
View gist:9627546
# 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)) })