Skip to content

Instantly share code, notes, and snippets.

View nickserv's full-sized avatar

Nicky McCurdy nickserv

View GitHub Profile
@nickserv
nickserv / enumerable_guide.md
Last active September 6, 2024 19:05
Ruby Enumerable Guide

A before-and-after guide to Ruby's Enumerable module

Basics

each

Ruby's for keyword is an anti pattern! In fact, it's actually slower than each and uses each in the background.

Before

for n in [1, 2, 3, 4]
 puts n
@nickserv
nickserv / grep.go
Created February 25, 2014 06:09
A simple grep implementation in Go
package main
import(
"bufio"
"fmt"
"log"
"os"
"regexp"
)
@nickserv
nickserv / nested_sum.hs
Last active August 29, 2015 13:57
Nested sums in Ruby and Haskell
mapSum :: Num a => (a -> a) -> [a] -> a
mapSum f xs = sum $ map f xs
nestedSum :: Num a => [a] -> [a] -> (a -> a -> a) -> a
nestedSum xs ys f = mapSum (\ x -> mapSum (f x) ys) xs
@nickserv
nickserv / env.go
Created March 13, 2014 00:22
List all environment variables in Go
package main
import (
"os"
"fmt"
)
func main() {
for _, pair := range os.Environ() {
fmt.Println(pair)
@nickserv
nickserv / README.md
Last active August 29, 2015 13:57
Go benchmark: Various methods of loading maps

Go benchmark: Various methods of loading maps

This benchmark was used for run.

Running

go test -bench=.

Note that there are no dependencies besides Go itself.

@nickserv
nickserv / web_cat.rb
Last active August 29, 2015 13:57
A simple script that shows the text contents of any web page
# usage: ruby web_cat.rb URL
require 'nokogiri'
require 'open-uri'
uri = ARGV[0]
doc = Nokogiri::HTML open uri
puts doc.text
@nickserv
nickserv / angular_codenames.rb
Last active August 29, 2015 13:57
Script to print out all AngularJS releases with version numbers and codenames
require 'open-uri'
changelog = open 'https://raw.githubusercontent.com/angular/angular.js/master/CHANGELOG.md'
headlines = changelog.grep(/^# /)
headlines.each do |headline|
version, codename = headline.match(/(\S+) +(\S+) +\([\d-]+\)/).captures
puts "#{version}: #{codename}"
end
@nickserv
nickserv / lookup_prime.hs
Last active February 22, 2022 17:17
A lookup' function for Haskell that does the opposite of lookup in Prelude
-- | lookup' @key assocs@ looks up a value in an association list. While lookup
-- finds a tuple where the first value matches and returns the second, lookup'
-- does the opposite.
lookup' :: (Eq b) => b -> [(a,b)] -> Maybe a
lookup' _ [] = Nothing
lookup' key ((x,y):xys)
| key == y = Just x
| otherwise = lookup' key xys
-- Prints "Just 2".
@nickserv
nickserv / cat.hs
Last active August 29, 2015 14:00
A minimal cat implementation in Haskell
main = getContents >>= putStr
#!/bin/bash
# Sometimes when you use AirPlay, your Mac's audio may die.
# If so, you can run this script to restart coreaudiod, which should fix your problem.
# Note that this may be slightly interactive because it uses sudo.
sudo killall coreaudiod