Skip to content

Commit

Permalink
some refactor and syntax changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Qile0317 committed May 24, 2024
1 parent ca08ad0 commit f6a0617
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: FastUtils
Type: Package
Title: What the Package Does in One 'Title Case' Line
Title: Fast, readable utility functions.
Version: 0.1.0
Date: 2024-05-23
Authors@R: c(person("Qile", "Yang", email = "[email protected]", role = c("cre","aut","cph")))
Expand All @@ -15,6 +15,7 @@ Imports:
data.table,
dplyr,
ggplot2,
hash,
Rcpp (>= 1.0.12)
LinkingTo: Rcpp
Suggests:
Expand Down
25 changes: 25 additions & 0 deletions R/color.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' @export
#' @source https://stackoverflow.com/questions/649454
#'
getAvgHex <- function(...) {
Reduce(function(hex1, hex2) {
grDevices::rgb(
t((grDevices::col2rgb(hex1) + grDevices::col2rgb(hex2)) / 2),
maxColorValue = 255
)
}, unlist(list(...)))
}

#' @export
scaleHex <- function(hex, scaleFactor) {

if (all(scaleFactor == 1)) return(hex)

hsv_color <- hex %>%
grDevices::col2rgb() %>%
grDevices::rgb2hsv()

hsv_color["v", ] <- bound(hsv_color["v", ] * scaleFactor, 0, 1)

grDevices::hsv(hsv_color["h", ], hsv_color["s", ], hsv_color["v", ])
}
43 changes: 32 additions & 11 deletions R/dataInitialization.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
#' @export
EmptyTable <- function() {
initV <- function(typeFunc, x, initVal = NULL) {
if (is(typeFunc, "character")) {
v <- vector(typeFunc, x)
} else {
v <- typeFunc(x)
}
if (is.null(initVal)) return(v)
sapply(v, function(i) initVal)
}

#' @export
initEmptyTable <- function() {
structure(
integer(0),
dim = 0L,
Expand All @@ -9,16 +20,26 @@ EmptyTable <- function() {
}

#' @export
initList <- function(x, initVal = NULL) {
l <- vector("list", x)
if (!is_an_integer(x)) names(l) <- as.character(x)
if (is.null(initVal)) return(l)
lapply(l, function(x) initVal)
tableToNumeric <- function(x) {
structure(as.numeric(x), names = names(x))
}

#' @export
namedNumericToTable <- function(x) {
output <- as.integer(x)
names(output) <- names(x)
output <- as.table(output)
names(dimnames(output)) <- ""
output
}

#' @export
getlast <- function(x, n = 1) {
index <- length(x) - n + 1
if (is.list(x)) return(x[[index]])
x[index]
#' @export
createHash <- function(keys, init_vals = NULL) {
keys <- unique(keys)
numkeys <- length(keys)
switch(as.character(numkeys),
"0" = hash::hash(),
"1" = hash::hash(keys, init_vals),
hash::hash(keys, initList(numkeys, init_vals))
)
}
7 changes: 7 additions & 0 deletions R/iteration.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ getUniquePairsUpTo <- function(x, oneIndexed = TRUE) {
rcppGetUniquePairsUpTo(as.integer(x), oneIndexed = oneIndexed)
}

#' @export
getlast <- function(x, n = 1) {
index <- length(x) - n + 1
if (is.list(x)) return(x[[index]])
x[index]
}

#' @export
zip <- function(...) mapply(list, ..., SIMPLIFY = FALSE)

Expand Down
22 changes: 17 additions & 5 deletions R/math.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
bound_num <- function(num, lowerbound, upperbound) {
min(max(num, lowerbound), upperbound)
#' @export
bound <- function(num, lowerbound, upperbound) {
sapply(num, function(x) min(max(x, lowerbound), upperbound))
}

is_bound_between <- function(num, lowerbound, upperbound) {
#' @export
isBound <- function(num, lowerbound, upperbound) {
(num >= lowerbound) & (num <= upperbound)
}

#' @export
add <- function(x, y) x + y

#' @export
subtract <- function(x, y) x - y

#' @export
multiply <- function(x, y) x * y

#' @export
divide <- function(x, y) x / y

is_even <- function(x) x %% 2 == 0
is_odd <- function(x) x %% 2 == 1
#' @export
isEven <- function(x) x %% 2 == 0

#' @export
isOdd <- function(x) x %% 2 == 1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![MIT license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/Qile0317/FastUtils/blob/main/LICENSE.md)
<!-- badges: end -->

R package for readable, and fast utility functions.
R package for fast, readable utility functions.

## Installation

Expand Down

0 comments on commit f6a0617

Please sign in to comment.