Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / index.html
Last active July 15, 2016 12:41
Markov chain generator in JavaScript (with a Ruby "compiler")
<!doctype html>
<meta charset="utf-8">
<input id="input" type="text"> <a href="#" id="scifi">Sci-Fi it!</a>
<p id="output"></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
function sampleWithFrequency(coll) {
var choice = Math.random(),
length = coll.length;
@mudge
mudge / fast_pagination.rb
Last active October 25, 2016 11:01
Speeding up tens of thousands of pages with PostgreSQL, LIMIT and OFFSET
require 'will_paginate/active_record'
require 'fast_paginator'
module FastPagination
def paginate(per_page: 96, page: 1, total_entries: nil)
WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
pivot = (pager.total_pages / 2.0).ceil
paginator = FastPaginator.new(self, pager)
if pager.current_page <= pivot
@mudge
mudge / colour.rb
Last active March 31, 2016 14:39
A class to convert red, green, blue values into HSL
class Colour
attr_reader :red, :green, :blue
def initialize(red, green, blue)
@red = Float(red)
@green = Float(green)
@blue = Float(blue)
end
def to_hsl
@mudge
mudge / isbnSpec.js
Last active March 17, 2016 08:56
A Sentient program to convert & complete ISBNs with both 10 & 13 digits (with thanks to @tuzz)
/*
* See the bottom of this file for the specs which show how this definition of
* 10 and 13 digit ISBNs allows us to:
*
* - Convert ISBN-10 to ISBN-13
* - Convert ISBN-13 to ISBN-10
* - Automatically fill in missing digits from ISBN-13
* - Automatically fill in missing digits from ISBN-10
*
* Note that as Sentient does not have characters, an 'X' in an ISBN-10 is
@mudge
mudge / isbns.rb
Last active March 16, 2016 16:37
Extract 13-digit ISBNs from some input text
# isbns('978-0-80-506909-9 9780671879198 0-415-82102-9 3319217283')
# => ["9780805069099", "9780671879198", "9789780805067", "9789780671877", "9780415821025", "9783319217284"]
def isbns(text)
thirteen_digit_isbns(text) + ten_digit_isbns(text)
end
def thirteen_digit_isbns(text)
text.tr('-', '').scan(/\b97[89]\d{10}\b/)
end
@mudge
mudge / proc.rb
Last active August 29, 2015 14:22
Composing Procs to explore ideas in Transproc (http://solnic.github.io/transproc/)
class Proc
def compose(g)
proc { |*args, &blk| call(g.call(*args, &blk)) }
end
end
symbolize = ->(x) { x.to_sym }
upcase = ->(x) { x.upcase }
symbolize_and_upcase = symbolize.compose(upcase)
@mudge
mudge / faraday.rb
Last active August 29, 2015 14:19
How to set request timeouts for various network clients
Faraday.new(site, request: { timeout: 30 })
@mudge
mudge / maybe.rb
Last active October 2, 2015 20:27
Implementing Enumerable to simulate Monads
class Maybe
include Enumerable
def initialize(value)
@value = value
end
def each
yield value unless value.nil?
end
@mudge
mudge / threequals.rb
Last active August 29, 2015 14:16
More noodling with #to_proc but, this time, through #===, c.f. http://mudge.name/2014/11/26/data-structures-as-functions.html
require 'set'
# Let's provide a default to_proc in terms of threequals
class Object
def to_proc
method(:===).to_proc
end
end
class Set