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
| # Monkeys, Shakespeare, typewriters, &c. ---- | |
| # Based on Andrew Heiss' https://gist.github.com/andrewheiss/461c749fce2783551f31611d8f0d7548 | |
| library(tidyverse) | |
| # Use 9 CPUs | |
| mirai::daemons(9) | |
| # Loop through just enough seeds to find both 'HELLO' and 'WORLD' |
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
| library(vec) | |
| x <- as_vec(c(apple = 1, banana = 2, carrot = 3)) | |
| is_named <- function(x, y) names(x) %in% y | |
| x[Negate(is_named), "banana"] | |
| #> apple carrot | |
| #> 1 3 |
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
| ## R - just vectorised (JIT) ################################################### | |
| nd_R <- function(x) { | |
| floor(log10(abs(x))) + 1 | |
| } | |
| nd_R(c(123456, 234, -72)) | |
| #> [1] 6 3 2 | |
| ## Fortran - {quickr} (compiled) ############################################### |
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
| ## global counters | |
| reset <- function() { | |
| assign("counts_lt", 0, .GlobalEnv) | |
| assign("counts_gt", 0, .GlobalEnv) | |
| assign("counts_eq", 0, .GlobalEnv) | |
| assign("counts_as", 0, .GlobalEnv) | |
| } | |
| reset() |
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
| using Distances | |
| using Plots | |
| function ninradius(indiv, radius, distances) | |
| findall(distances[indiv, :] .< radius) | |
| end | |
| function getdists(particlelist) | |
| pairwise(Euclidean(), reduce(hcat, [[p.x, p.y] for p in particlelist]), dims=2) | |
| 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
| >>> def make_a_list(val1 = 0, val2 = 0, lst = []): | |
| ... lst.append(val1) | |
| ... lst.append(val2) | |
| ... return lst | |
| >>> # you need to know that you're modifying base_list | |
| >>> base_list = [1, 2, 3, 4] | |
| >>> extend_list = make_a_list(5, 6, base_list) | |
| >>> print(extend_list) | |
| [1, 2, 3, 4, 5, 6] |
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
| partition <- function(x, cut) { | |
| v <- rep(0, length(x)) | |
| v[cut] <- 1 | |
| cumsum(v) | |
| } | |
| cumsum_cut <- function(x, cut = NULL) { | |
| cut <- sort(cut) | |
| vs <- split(x, partition(x, cut)) | |
| vs <- sapply(vs, \(x) {x[1] <- 0; 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
| `[.vec` <- function(x, i, ...) { | |
| UseMethod("[.vec", i) | |
| } | |
| `[<-.vec` <- function(x, i, value, ...) { | |
| UseMethod("[<-.vec", i) | |
| } | |
| `[.vec.default` <- function(x, i, ...) { | |
| class(x) <- setdiff(class(x), "vec") |
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
| ts=0.07;ps=0.02;A=B=0; | |
| while(1){ j=i=0;z=rep(0,1760); | |
| b=rep(' ',1760); while(j < 6.28){ | |
| j=j+ts;i=0;while(i<6.28){i=i+ps;c= | |
| sin(i);l=cos(i); d=cos(j);f=sin(j); | |
| e=sin(A);g=cos(A );h=d+2;D=1/(c*h*e+ | |
| f*g+5);m=cos(B) ;n=sin(B);t=c*h*g- | |
| f*e;x=as.integer (40+30*D*(l*h*m-t*n)); | |
| y=as.integer(12+ 15*D*(l*h*n+t*m)); | |
| o=as.integer(x+( 80*y));N=as.integer( |
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
| enum <- function(...) { | |
| vals <- if (is.null(...names())) { | |
| setNames(seq_len(...length()), c(...)) | |
| } else { | |
| c(...) | |
| } | |
| out <- new.env(parent = emptyenv()) | |
| list2env(as.list(vals), out) | |
| lapply(names(vals), lockBinding, out) | |
| lockEnvironment(out) |
NewerOlder