Skip to content

Instantly share code, notes, and snippets.

Peter Hurford peterhurford

Block or report user

Report or block peterhurford

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@peterhurford
peterhurford / num_rows_csv.R
Last active Oct 4, 2019
What's the fastest way to determine the number of rows of a CSV in R?
View num_rows_csv.R
# What's the fastest way to determine the number of rows of a CSV in R?
# ...Reading the entire CSV to only get the dimensions is likely too slow. Is there a faster way?
# Benchmarks done on a EC2 r3.8xlarge
# Cowritten with Abel Castillo <github.com/abelcastilloavant>
m <- 1000000
d <- data.frame(id = seq(m), a = rnorm(m), b = runif(m))
dim(d)
# [1] 1000000 3
pryr::object_size(d)
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 / programming-checklist.md
Last active Mar 5, 2020
A programming checklist for you to fill out every time you make a pull request to make sure you end up with good code
View programming-checklist.md
  • Did you write tests? Are they mutually exclusive and collectively exhaustive? Do they pass?
  • Did you get a code review?
  • Have you verified that your code works, outside of tests?
  • Is your code DRY?
  • Did you follow the single responsibility principle at different levels of detail throughout all your functions, objects, files, folders, repositories, etc.?
  • Is your code readable? Can someone else tell you what it does?
  • Is your code self-documenting? Did you explain strange choices? Did you write documentation about how it works?
  • Do all your variables have self-explaining names?
  • Did you avoid writing overly long functions?
  • Do you document what your function inputs are? Are you explicit about what preconditions must be true about your function inputs? Are you explicit about what postconditions will hold about your function outputs, if the preconditions hold?
@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 / readable-code.md
Last active Mar 20, 2019
How do you write readable code?: 13 Principles
View readable-code.md

How do you write readable code?: 13 Principles

"Programs should be written for people to read, and only incidentally for machines to execute." -- Structure and Interpretation of Computer Programs

"How would you define good code? [...] After lots of interviews we started wondering if we could come out with a definition of good code following a pseudo-scientific method. [...] The population is defined by all the software developers. The sample consists of 65 developers chosen by convenience. [...] The questionnaire consists in a single question: “What do you feel makes code good? How would you define good code?”. [...] Of those, the most common answer by far was that the code has to be Readable (78.46%), almost 8 of each 10 developers believe that good code should be easy to read and understand." -- "What is Good Code: A Scientific Definition"

@peterhurford
peterhurford / parallelization.md
Created Oct 17, 2015
How does code get parallelized?
View parallelization.md

Computer code is a series of executed statements. Frequently, these statements are executed one at a time. If one part of your code takes a long time to run, the rest of your code won't run until that part is finished.

However, this isn't how it has to be. We can often make the exact same code go much faster through parallelization, which is simply running different parts of the computer code simaltaneously.

Asynchronous Code

The first example of this is asynchronous code. The idea here is that many times you do things like send a call to another computer, perhaps over the internet, using an API. Normally, code then has to simply wait for the other computer to give it a response over the API. But asynchronous code can simply keep on going and then the API call returns later.

This makes code harder to reason about and handle because you don't know when the API call will return or what your code will be like when it returns, but it makes your code faster because you don't have to wait arou

@peterhurford
peterhurford / open-source.md
Last active Nov 29, 2019
What is on an open source project website?: Five case studies
View open-source.md

What is on an open source project website?: Five case studies

Looking at Rails, Angular, jQuery, Prediction.io, and Redis pages to find commonalities.

Lessons Learned

  • Layout matters. A nice layout inspires trust in your project.
  • Layout is similar. All the sites had a top bar with the prominent navigation. All the main pages had introductory text.
  • GitHub Issues is used ubiquitously for bug tracking.
  • IRC seems important for communities. Gitter seems like a good choice.
@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.

@peterhurford
peterhurford / better-programming.md
Last active Mar 17, 2020
One Year Out: How I Became a Better Programmer
View better-programming.md

I've been coding professionally for a year now, having started work on the 30th of June. In that year I've programmed professionally in Ruby on Rails, R, and JavaScript / Coffeescript using both the Knockout and Angular frameworks.

I'd like to think I've become a much better programmer over the past year. Looking back at my old code, I can tell that I grew a lot.

Here's how I did it.

Spend Time Programming

@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:
You can’t perform that action at this time.