Skip to content

Instantly share code, notes, and snippets.

require(dplyr) # version 0.4.3
f1 <- function(x, y) { if (x < 5) y else if (x >= 5) y + 10 }
f2 <- function(x, y) { if (x < 5) y + 0 else if (x >= 5) y + 10 }
d <- data.frame(x=1:10, y=1:10)
d %>% rowwise() %>% mutate(z=f1(x, y))
# Error: incompatible types, expecting a integer vector
d %>% rowwise() %>% mutate(z=f2(x, y))
# Source: local data frame [10 x 3]
# Groups: <by row>
def decorator(f):
def f_new():
print "pre"
f()
print "post"
try:
f.decorated
return f
except AttributeError:
f_new.decorated=True
def decorator(fn):
def f():
return "dec" + fn()
f.decorated=True
f.original_func = fn
return f
def decorator_override(fn):
def f():
if fn.decorated:
(defn fact [n]
(cond (zero? n) 1
:else (* n (fact (dec n)) )))
(defn fact-tail [n]
(loop [n n acc 1]
(cond (zero? n) acc
:else (recur (dec n) (* acc n)))))
@rasmusto
rasmusto / gist:9925149
Created April 1, 2014 23:29
:TLsource error
file looks like this:
; ras/test.tim
(ns ras.test)
; here's the error:
; Error detected while processing function timl#loader#source..timl#compiler#build..<SNR>95_emit..timl#call..150..timl#type#apply..timl#lazy_seq#seq..<SNR>97_val..timl#call..289..<SNR>72_predicate
..timl#coll#chunked_seqp:
@rasmusto
rasmusto / dither.py
Created February 10, 2014 06:36
dither
import Tkinter
import Image, ImageTk
im = Image.open('pic.bmp')
root = Tkinter.Tk()
dither8 = [[ 0, 32, 8, 40, 2, 34, 10, 42],
[48, 16, 56, 24, 50, 18, 58, 26],
[12, 44, 4, 36, 14, 46, 6, 38],
[60, 28, 52, 20, 62, 30, 54, 22],
[ 3, 35, 11, 43, 1, 33, 9, 41],
@rasmusto
rasmusto / rhizome-fun.clj
Created February 4, 2014 16:36
random graphs w/ rhizome (graphviz) visualization
(ns rhizome-fun.core
; [rhizome "0.2.0"]
(:require [rhizome.viz :refer [view-graph save-graph]]))
(def alpha-nodes (mapv (comp keyword str)
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
(defn random-from [nodes]
(let [n (rand-int (count nodes))
pick (nodes n)
@rasmusto
rasmusto / init.el
Created August 17, 2013 22:57
simple emacs init.el
(require 'package)
(setq package-archives
'(("marmalade" . "http://marmalade-repo.org/packages/")))
(package-initialize)
(mapc
(lambda (package)
(or (package-installed-p package)
(if (y-or-n-p (format "Package %s is missing. Install it? " package))
(package-install package))))
(ns clj-async-tests.core
(:require [clojure.core.async :as async
:refer [go chan >! <! >!! <!! timeout close! thread alts! alts!!]]))
(defn walk [t]
(cond (nil? t)
nil
:else
(let [[l v r] t]
(concat (walk l) [v] (walk r)))))
@rasmusto
rasmusto / fireplace.vim
Last active December 16, 2015 10:59
run clojure code from vim-fugitive buffer
function! s:repl.includes_file(file) dict abort
let is_zipfile = matchstr(a:file, '\C^zipfile:.*::')
let is_fugitive = matchstr(a:file, '\C^fugitive:\/\/.*')
if !empty(is_zipfile)
let file = substitute(a:file, '\C^zipfile:\(.*\)::', '\1/', '')
elseif !empty(is_fugitive)
let file = substitute(a:file, '\C^fugitive:\/\/\(.*\)', '\1', '')
let file = substitute(file, '\/\.git\/\/[^\/]\+', '', '')
else
let file = a:file