Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am mattdenner on github.
  • I am mattdenner (https://keybase.io/mattdenner) on keybase.
  • I have a public key whose fingerprint is 7177 7706 1497 3A2C B930 3921 E120 A382 2201 460F

To claim this, I am signing this object:

@mattdenner
mattdenner / gist:9d0e03fb24be92ae7c78
Last active August 29, 2015 14:11
From imperative to functional: error handling

In my last post I took a piece of imperative code for parsing a comma separated string and generating an instance of a structure, and turned it into a functional piece. At the end we had a bunch of functions that related to code working with Optional, some related to Array, and a bunch that dealt with the actual application code. The "top level" functions we've written looked like this:

func build(fields: Array<String>) -> Optional<MyRecord> {
  let field1Value = fields[0]
  let field2Value = lift(stringToInt)(fields[1])
  return .Some(createMyRecord) <*> field1Value <*> field2Value
}
 
func build(data: String) -> Optional<MyRecord> {
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://learnyousomeerlang.com/content'))
urls = [ 'http://learnyousomeerlang.com/content' ] + doc.xpath('//h3/a').map { |n| n.attribute('href').to_s }
exec("wkhtmltopdf -s A4 #{ urls.join(' ') } learnyousomeerlang.pdf")
$ rake metrics:all
(in /home/matt/brute_force_capatcha)
saikuro --filter_cyclo 0 --warn_cyclo 5 --error_cyclo 7 --output_directory tmp/metric_fu/scratch/saikuro --formater text --input_directory "lib" --cyclo
sh: saikuro: not found
Saikuro failed with exit status: 127
@mattdenner
mattdenner / gist:340143
Created March 22, 2010 14:47
Ruby quine
_="_=%p;puts _%%_";puts _%_
#!/usr/bin/env ruby
require 'rubygems'
require 'amqp'
require 'mq'
require 'uuid'
exchange_type = ARGV.first or raise "Usage: publisher <exchange type>"
[ :direct, :fanout, :topic ].include?(exchange_type.to_sym) or raise "You can only use direct, fanout or topic"
KEYS = [ 'even', 'odd' ]
@mattdenner
mattdenner / publisher.rb
Created March 27, 2010 10:54
RabbitMQ publisher & subscriber in Ruby
#!/usr/bin/env ruby
require 'rubygems'
require 'amqp'
require 'mq'
require 'uuid'
exchange_type = ARGV.first or raise "Usage: publisher <exchange type>"
[ :direct, :fanout, :topic ].include?(exchange_type.to_sym) or raise "You can only use direct, fanout or topic"
KEYS = [ 'even', 'odd' ]
@mattdenner
mattdenner / gist:356359
Created April 5, 2010 13:57
Immutable after construction pattern
class ConstructorHelper
class << self
def construct(target, writers_for = target.instance_variables, &block)
self.new(target, writers_for).instance_eval(&block) if block_given?
end
end
def initialize(target, writers_for = target.instance_variables)
writers_for.each do |variable|
self.class.send(:define_method, :"#{ variable.to_s.sub(/^:?@/, '') }=") do |v|
class MyBase
def do_something
end
end
class MyDerived < MyBase
def do_something
do_something_else
super
end
@mattdenner
mattdenner / vows.rake
Created August 15, 2010 08:59
Snippets for testing Javascript with nodejs and vows (see http://mattdenner.github.com/2010/08/15/testing-javascript.html)
var vows = require('vows'), assert = require('assert'), path = require('path');
var application = require(path.join(process.cwd(), 'public', 'javascripts', 'application'));