Skip to content

Instantly share code, notes, and snippets.

Avatar
🤖

Michael Kohl citizen428

🤖
View GitHub Profile
@citizen428
citizen428 / zip_with.rb
Created Nov 22, 2011
Haskell's zip_with in Ruby
View zip_with.rb
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 Nov 22, 2011
A snippet I used for explaining the map/reduce paradigm
View map_reduce.rb
>> 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 Nov 22, 2011
A little wrapper around Pow's HTTP requests for status.json and config.jsom
View pow-show.rb
#!/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 Sep 26, 2011
Given a WP XML export, this script will generate the necessary 301 redirect rules for all posts for Apache or Nginx.
View octopress_redirects.rb
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 Sep 25, 2011 — 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.
View wp-xml-import.rb
require 'fileutils'
require 'date'
require 'yaml'
require 'rexml/document'
include REXML
doc = Document.new(File.new(ARGV[0]))
FileUtils.mkdir_p "_posts"
View Emacs.markdown

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

View ycombinator-step1.clj
(ns step1)
(defn fact
[n]
(if (< n 2) 1 (* n (fact (dec n)))))
;; (fact 5) => 120
View gist:518090
(use '(incanter core charts io))
View gist:465950
;;; Ruby/Rails development
;; RVM support
;; Ruby Version Manager
(require 'rvm)
(rvm-use-default)
;; automatically add 'end' after class, module, def etc.
;; automatically pair braces, single and double quotes
(require 'ruby-electric)
You can’t perform that action at this time.