Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View citizen428's full-sized avatar

Michael Kohl citizen428

View GitHub Profile
@citizen428
citizen428 / ruby_github.rb
Created November 23, 2011 19:33
Create an Atom feed from the new Ruby repos page
require 'open-uri'
require 'nokogiri'
require 'builder'
html = open("https://github.com/languages/Ruby/created")
doc = Nokogiri::HTML.parse(html)
atom = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)
atom.instruct!
atom.feed "xmlns" => "http://www.w3.org/2005/Atom" do
@citizen428
citizen428 / zip_with.rb
Created November 22, 2011 20:46
Haskell's zip_with in Ruby
def zip_with(other, op=nil)
return [] if self.empty? || other.empty?
clipped = self[0..other.length-1]
zipped = clipped.zip(other)
if op
zipped.map { |a, b| a.send(op, b) }
else
zipped.map { |a, b| yield(a,b) }
end
end
@citizen428
citizen428 / map_reduce.rb
Created November 22, 2011 20:46
A snippet I used for explaining the map/reduce paradigm
>> words = "foo bar baz qux foo bar lala"
#=> "foo bar baz qux foo bar lala"
>> # map (user-supplied)
.. mapped = words.split(/ /).map { |w| [w,1] }
#=> [["foo", 1], ["bar", 1], ["baz", 1], ["qux", 1], ["foo", 1], ["bar", 1], ["lala", 1]]
>> # aggregate (built-in)
.. aggregated = mapped.group_by { |w, c| w }
#=> {"foo"=>[["foo", 1], ["foo", 1]], "bar"=>[["bar", 1], ["bar", 1]], "baz"=>[["baz", 1]], "qux"=>[["qux", 1]], "lala"=>[["lala", 1]]}
>> # reduce (user-supplied)
>> aggregated.each_with_object({}) { |(k, v), h| h[k] = v.map(&:last).inject(:+) }
@citizen428
citizen428 / pow-show.rb
Created November 22, 2011 20:43
A little wrapper around Pow's HTTP requests for status.json and config.jsom
#!/usr/bin/env ruby
require 'json'
unless %w(status config).include?(ARGV[0])
puts "Usage: #{File.basename($0)} status|config"
exit 1
end
jj JSON.parse(`curl -s -H host:pow localhost/#{ARGV[0]}.json`)
@citizen428
citizen428 / octopress_redirects.rb
Created September 26, 2011 07:13
Given a WP XML export, this script will generate the necessary 301 redirect rules for all posts for Apache or Nginx.
require 'date'
require 'rexml/document'
include REXML
doc = Document.new(File.new(ARGV[0]))
format = ARGV[1] || "apache"
formats = {
:apache => "RewriteRule ^%s$ /%s [R=301,L]",
@citizen428
citizen428 / wp-xml-import.rb
Created September 25, 2011 16:19 — forked from zanshin/wp-xml-import.rb
WordPress to Textile Ruby script (I used a Textile plugin for WP). Parses all existing WP categories and tags into discreet list of categories. Finally forces all content to be UTF-8.
require 'fileutils'
require 'date'
require 'yaml'
require 'rexml/document'
include REXML
doc = Document.new(File.new(ARGV[0]))
FileUtils.mkdir_p "_posts"

Help:

C-h t Tutorial
C-h m Help for active mode
C-h k Help for key sequence
C-h f Help for function
C-h v Help for variable
C-h a Search help
C-h i Info reader

(ns step1)
(defn fact
[n]
(if (< n 2) 1 (* n (fact (dec n)))))
;; (fact 5) => 120
(use '(incanter core charts io))