Skip to content

Instantly share code, notes, and snippets.

library(ggplot2)
n <- 500
z_all <- complex(
re = rep(seq(-2, 0.75, len = n), times = n),
im = rep(seq(-1.25, 1.25, len = n), each = n))
# vectorized code for just counting
# http://www.everydayanalytics.ca/2014/12/the-mandelbrot-set-in-r.html
mndl_count <- function(z0, iter_max = 100) {
make.stack <- function() {
val <- list()
list(
push = function(x) {
val <<- c(val, list(x))
},
pop = function() {
len <- length(val)
res <- val[len]
val <<- val[-len]
@TobCap
TobCap / Transpose.cs
Last active August 29, 2015 14:01
transpose a jagged array by linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TransposeArray
{
public static class MyClass
{
static IEnumerable<T> lscan<T>(Func<T, T, T> f, T init, IEnumerable<T> x)
{
return x.Any() ? new[] { init }.Concat(lscan(f, f(init, x.First()), x.Skip(1))) : new [] { init };
}
static IEnumerable<T> lscan1<T>(Func<T, T, T> f, IEnumerable<T> xs)
{
return lscan(f, xs.First(), xs.Skip(1));
}
@TobCap
TobCap / CsvSerializer.cs
Last active August 29, 2015 14:03
Minimal functionalities are implemented.
namespace CsvSerializer
{
// see also
// http://blog.recyclebin.jp/archives/1731
// http://stackoverflow.com/questions/1179816/best-practices-for-serializing-objects-to-a-custom-string-format-for-use-in-an-o
// http://nine-works.blog.ocn.ne.jp/blog/2011/05/post_13ea.html
public interface ICsvParser
{
object AsConvertible(string str);
@TobCap
TobCap / MakeAccdb.ps1
Last active August 29, 2015 14:06
create a MS-Access file by ADOX and ADODB
# execute on 32-bit(x86) version
# %windir%\sysWOW64\WindowsPowerShell\v1.0\PowerShell_ISE.exe
# C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
Get-Location | Out-Host
(Get-ChildItem Env:\HOMEPATH).value
$FileName = "hoge.accdb"
$ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FileName"
# create access file
@TobCap
TobCap / get_base_fun_name.r
Last active August 29, 2015 14:13
return character of original builtin
get_base_fun_name <- function(x) {
base_funs <- as.list(baseenv(), all = TRUE)
for (i in seq_along(base_funs)) {
if(identical(x, base_funs[[i]])) return(names(base_funs[i]))
}
NULL
}
s <- substitute
# > get_base_fun_name(s)
@TobCap
TobCap / curry_special.r
Created January 20, 2015 21:33
currying for substitute(): semantics is I think almost emulated but not so smart code...
library(lazyeval)
# for substitute
curry_special <- function(fun, p_f = parent.frame()) {
fun_env <- environment(fun)
if (is.null(fun_env)) fun_env <- .GlobalEnv
new_env <- new.env(parent = fun_env)
search_fun <- function(x) {
base_funs <- as.list(baseenv())
@TobCap
TobCap / pipe_stream.r
Last active August 29, 2015 14:14
making call objects and evaluate at once instead of applying closure in each steps
replace_symbol <- function(expr, before, after) {
stopifnot(is.language(expr), is.symbol(before))
eval(substitute(substitute(e, `names<-`(list(after), as.character(before))), list(e = expr)))
}
pp <- function(expr_, env_ = parent.frame()) {
right_bin_ops <- c("%>>%", "%|>%")
iter <- function(x) {
if (length(x) <= 1) x
else if (length(x) == 3 && as.character(x[[1]]) %in% right_bin_ops)
@TobCap
TobCap / fib.r
Last active August 29, 2015 14:18
fib <- function(n) {
if (n <= 1) n
else fib(n - 1) + fib(n - 2)
}
fib_rec <- function(n, a = 0, b = 1) {
if (n <= 1) b
else fib_rec(n - 1, b, a + b)
}