Skip to content

Instantly share code, notes, and snippets.

View knapply's full-sized avatar
🕵️

knapply

🕵️
View GitHub Profile
@knapply
knapply / app.R
Created March 19, 2018 14:51
R Shiny Slider Input
library(shiny)
server <- function(input, output) {
output$input_value <- renderPrint({
cat(input$slide) # cat() is an alternative to print(), see: ?cat()
}) ## I'm only using it here to hide the index that
## would be otherwise outputted. For example:
## > print(2)
## [1] 2
## > cat(2)
r_length <- function(x) {
out <- 0L
for (i in x) {
out <- out + 1L
}
out
}
r_vector <- 1:20
r_length(r_vector)
@knapply
knapply / foundational_programming-from_scratch-py_length.py
Last active November 5, 2018 18:50
Python: Length from scratch
def py_length(x):
out = 0
for i in x:
out += 1
return out
py_generator = range(1, 21)
result = py_length(py_generator)
print(result)
#> 20
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int cpp_length(NumericVector x) {
int out = 0;
for (NumericVector::iterator iter = x.begin(); iter != x.end(); iter++) {
out++;
}
return out;
r_vector <- 1:20
length(r_vector)
#> [1] 20
py_generator = range(1, 21)
result = len(py_generator)
print(result)
#> 20
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int cpp_size(NumericVector x) {
return x.size();
}
/*** R
cpp_size(1:20)
#include <iostream> // std::cout, std::endl
#include <vector> // std::vector
using std::cout;
using std::endl;
using std::vector;
int length(vector<double> &x) {
int out = 0;
import numpy as np
numpy_array = np.array(range(1, 21))
result = np.size(numpy_array)
print(result)
#> 20
#include <iostream> // std::cout, std::endl
#include <vector> // std::vector
using std::cout;
using std::endl;
using std::vector;
int main() {
vector<double> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };