Skip to content

Instantly share code, notes, and snippets.

View jmbarbone's full-sized avatar

Jordan Mark Barbone jmbarbone

View GitHub Profile
@jmbarbone
jmbarbone / fibonacci.R
Created September 5, 2026 17:12
Fibonacci with TailCall(), cache, and Binet formula
.fibonacci <- function(n) {
if (n <= 0) {
return(0)
}
if (n == 1) {
return(1)
}
Tailcall(.fibonacci, n - 1) + Tailcall(.fibonacci, n - 2)
@jmbarbone
jmbarbone / winner.R
Created September 5, 2026 17:09
Continuous equal probability winners
new_counter <- function() {
local({
winner <- 0
n <- 0L
maybe <- function() {
if (stats::rbinom(1, 1, 1 / n) == 1) {
message("new winner: ", n)
winner <<- n
}
}
@jmbarbone
jmbarbone / object.md
Created July 2, 2026 02:44
object in R
object <- function(...) {
  ..params <- list(...)
  fun <- function() {}
  formals(fun) <- ..params
  body(fun) <- substitute(
    {
      self <- list2env(..params)
      lockEnvironment(self)
 updates &lt;- as.list(match.call()[-1L])
@jmbarbone
jmbarbone / input.R
Created February 11, 2026 04:08
prime factorization
primef <- function(x) {
x <- as.integer(x)
if (x <= 1) {
return(NaN)
}
res <- integer()
while (x %% 2L == 0L) {
res <- c(res, 2L)
@jmbarbone
jmbarbone / options-but-env.R
Last active January 19, 2026 18:11
R options validations through active bindings?
# set in options(name = value)
# retrieved in fuj.options$name
# when retrieved, runs getter
fuj.options <- local({
self <- environment()
optenv <- new.env()
active <- function(name, default, getter) {
makeActiveBinding(
name,
local({
@jmbarbone
jmbarbone / fibonocci.md
Created October 9, 2025 04:20
'fib'-o-nocci; or
.fibs <- new.env()

.fib <- function(n) {
  name <- as.character(n)
  get0(name, envir = .fibs) %||% assign(name, fibonocci(n), envir = .fibs)
}

fibonocci <- function(n = 0) {
 n &lt;- floor(n)
@jmbarbone
jmbarbone / another-version.md
Last active October 18, 2025 23:31
typed functions in R
arg <- function(sym, type, default) {
  sym <- substitute(sym)
  spec <- structure(
    list(
      sym = as.character(sym),
      type = type
    ),
    class = "arg"
  )
@jmbarbone
jmbarbone / iterators.md
Created October 2, 2025 05:55
just some iterations in R
enumerate <- function(x, i = "i", v = "v") {
  lapply(
    seq_along(x),
    function(iteration) {
      structure(
        list(iteration, x[[iteration]]),
        class = c("enumeration", "list"),
        names = c(i, v)
      )
@jmbarbone
jmbarbone / global-calling-handlers.R
Created October 1, 2025 01:06
R environment/class for handling handlers
global_calling_handlers <- local({
. <- environment()
class(.) <- c("cnd:global_calling_handlers", "environment")
.handlers <- NULL
handlers <- NULL
add <- function(...) {
"Adds calling handlers
If handlers with the same name already exist, they are replaced.
pkg_version <- function(package, keep = c("all", "patch", "minor", "major")) {
keep <- match.arg(keep)
version <- utils::packageVersion(package)
version <- unclass(version)[[1L]]
version <- switch(
keep,
all = version,
patch = version[1:3],
minor = version[1:2],
major = version[1L]