Skip to content

Instantly share code, notes, and snippets.

@peterhurford
peterhurford / ziptime.rb
Created Oct 29, 2014
Database of US Zipcodes and their Timezones, as a Ruby Hash
View ziptime.rb
This file has been truncated, but you can view the full file.
# This is a database of zipcodes and their time zones
# by Peter Hurford <http://www.peterhurford.com>
# Coded in an array as zitpime[<zip code>] = [<time offset from UTC>, <DST yes (1) or no (0)>]
# Daylight Savings is March 9 to November 2
# Data retrieved from http://www.boutell.com/zipcodes/ (CSV and SQL available), which in turn retrieved data from the CivicSpace US ZIP Code Database.
# Here, I used Regex to sort the data into a Ruby hash.
# You are free to use this array for your own purposes, just provide attribution.
@peterhurford
peterhurford / interview.md
Last active Aug 29, 2015
Ruby Interview Questions (No Rails, for a Prospective Junior Developer / Intern)
View interview.md

What does a ||= b mean?

What is the difference between a symbol and a string?

What is the difference between ' and " in Ruby?

@peterhurford
peterhurford / RCoffeescript.md
Last active Aug 29, 2015
Rspresso: Coffeescript for R
View RCoffeescript.md

Rspresso

(Name and initial idea from robertzk.)

Assignment

number = 42 --> RSPRESSO! --> number <- 42

Functions

@peterhurford
peterhurford / interview.md
Last active Aug 29, 2015
Rails Interview
View interview.md

1a.) Why would one use a symbol instead of a string?

1b.) Why would someone use a string instead of a symbol?

2a.) What is the difference between a lambda, a block and a proc?

2b.) What does -> (a) {p a}["Hello world"] do?

@peterhurford
peterhurford / functionals_need_force.md
Last active Aug 29, 2015
Why do R function operators need to force(f)?
View functionals_need_force.md

While reading Hadley's Advanced R book chapter on function operators (see our work on creating a set of answers here), I came across this fact about lazy evaluation:

The function operators we’ve seen so far follow a common pattern:

funop <- function(f, otherargs) { function(...) { # maybe do something res <- f(...) # maybe do something else

@peterhurford
peterhurford / hate.R
Created Jun 10, 2015
An R Function that Only Works When You Try to Debug it
View hate.R
# Debugging in R is much better than many languages, but sometime it can be
# frustrating. Here's a function that makes it even more frustrating --
# it only works when you try to debug it!
# The challenge:
### Write a function that, if the function has a browser in it, will execute
### every line correctly, but if it does not have a browser in it, it will
### error.
# The solution:
@peterhurford
peterhurford / fledgling-languages.md
Created Feb 16, 2016
Some highlights from the "Fledgling Languages" list
View fledgling-languages.md

The Fledgling Languages list has almost 100 programming languages that are up-and-coming but not widely popular. I looked at them all and here are a few of my favorites:

...This language looks so much like English! http://www.availlang.org/

...This language claims to have the speed of C++, the expressiveness of Python, and tons of additional safety with first-level contracts http://cobra-language.com/

...Code that looks exactly like Ruby, but is statically type-checked and compiled into efficient native code http://crystal-lang.org/

...What it would look like if Haskell and Clojure had a baby https://github.com/LuxLang/lux

@peterhurford
peterhurford / meat_consumption_by_country.md
Last active Apr 17, 2017
Meat consumption by country, kg per capita
View meat_consumption_by_country.md

Total kg per capita consumed of beef, veal, pork, poultry, and sheep meat by country in 2016, adapted from OECD Data on Meat Consumption. Per-animal breakdown is available as a CSV.

IND	2.944374911
BGD	3.358927799
ETH	4.449228451
NGA	6.061917778
TZA	6.986943733
MOZ	7.117845532
SSA	8.798326892
View ddply-nse.md
#' Calculate a dep_var for the iris dataset based on the iris dataset.
#'
#' @param by character. \code{length} to go by \code{Petal.Length} or \code{width} to go by \code{Petal.Width}.
iris_with_dep_var <- validations::ensure(pre = list(by %in% c("length", "width")),
  function(by = "length") {
    if (identical(by, "length")) {
      plyr::ddply(iris, plyr::.(Species), summarize, dep_var = ifelse(any(Petal.Length >= 4), 1, 0))
    } else {
      plyr::ddply(iris, plyr::.(Species), summarize, dep_var = ifelse(any(Petal.Width >= 4), 1, 0))
@peterhurford
peterhurford / r-pkgs.md
Created Jul 6, 2015
Notes on Hadley's "R Packages"
View r-pkgs.md

Notes from reading through R Packages by Hadley Wickham. This is meant to review, not replace, a thorough readthrough. I mainly wrote this as a personal review, since writing summaries and attempting to teach others are some of the best ways to learn things.

Introduction

  • Packages are used to organize code together so that it can be used repeatedly and shared with others.

  • A lot of work with packages is done via the devtools package.