Jeff Iacono jeffreyiacono

View foreman.rb
1 2 3 4 5 6 7 8 9 10
class Foreman
attr_accessor :units_of_work, :workers_count
 
def initialize attrs = {}
@units_of_work = attrs[:units_of_work]
@workers_count = attrs[:workers_count]
end
 
def fair_shares
even_shares = Array.new(@workers_count) { @units_of_work / @workers_count }
View forking_and_waiting.rb
1 2 3 4 5 6 7 8 9 10
pids = []
[0, 1, 2].each do |i|
pids << fork do
sleep 5 - i
puts "Hi from #{i}\n"
end
end
 
puts "done forking" # note: if you >> this file to another file, you will need $stdout.flush to ensure proper write ordering
pids.each { |pid| Process.wait pid }
View git_mv_a_bunch_of_files.sh
1
for f in `ls ./*.png`; do git mv $f $(echo $f | sed s/control/ctrl/); done
View running_average_generator.rb
1 2 3 4 5 6 7 8 9 10
class RunningAverageGenerator
def initialize seed = []
@sum = seed.reduce(:+) || 0
@count = seed.count || 0
end
 
def << number
@count += 1
@sum += number
@sum / @count.to_f
View color_ramp_palette_plots.R
1 2 3 4 5 6 7 8 9
pal <- colorRampPalette(c("red", "blue"))
 
plot(rnorm(1000), seq(1, 1000, by = 1)
, col = pal(1000)
, xlab = "x"
, ylab = "y"
, main = "Fun with rnorm + colorRampPalette")
 
# => view Plot 1
View lattice_plots_with_medians_and_lm.R
1 2 3 4 5 6 7 8 9 10
library(lattice)
 
x <- rnorm(100)
y <- x + rnorm(100, sd = 0.5)
f <- gl(2, 50, labels = c("Group 1", "Group 2"))
 
xyplot(y ~ x | f,
panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
panel.lmline(x, y, col = 2)
View closures.R
1 2 3 4 5 6 7 8 9 10
> make.power <- function(n) {
pow <- function(x) {
x ^ n
}
pow
}
> cube <- make.power(3)
> square <- make.power(2)
> cube(3)
[1] 27
View lexical.R
1 2 3 4 5 6 7 8 9 10
> y <- 10
> f <- function(x) {
y <- 2
y^2 + g(x)
}
> g <- function(x) {
x * y
}
> f(3)
[1] 34
Something went wrong with that request. Please try again.