Skip to content

Instantly share code, notes, and snippets.

@photomattmills
photomattmills / Dockerfile
Last active August 29, 2015 14:26
Dockerfile for running a phoenix app.
# Current debian stable for our base.
FROM debian:jessie
ENV LANG C.UTF-8
# Install base package, plus some Erlang deps
RUN apt-get update
RUN apt-get install -y wget git build-essential libwxbase3.0-0 libwxgtk3.0-0
# Install Erlang. This package is the current stable.
auspost = { #table containing all the alphanumeric codes for auspost barcodes
"222" => "0",
"300" => "1",
"301" => "2",
"302" => "3",
"310" => "4",
"311" => "5",
"312" => "6",
"320" => "7",
"321" => "8",
@photomattmills
photomattmills / note.markdown
Last active August 29, 2015 14:19
A note about multiple assignment in ruby (with mandelbrot!)

I was looking at Facebook's 'this day in your timeline' thingy when I came across this tweet that I'd retweeted way back in prehistory: https://twitter.com/dbrady/status/12546255974 . It contains a one-liner that prints an ASCII mandelbrot set: 60.times{|a|puts((0..240).map{|b|x=y=i=0;until(x*x+y*y>4||i==99);x,y,i=x*x-y*y+b/120.0-1.5,2*x*y+a/30.0-1,i+1;end;i==99?'#':'.'}*'');}

I wanted to understand it, so the first thing I did was unfold all the blocks, and turn the multiple assignment into three separate lines:

60.times do |a|
  puts((0..240).map do |b|
    x=y=i=0
    until(x*x+y*y>4||i==99)
      x = x*x-y*y+b/120.0-1.5
@photomattmills
photomattmills / gist:20696ef01d563ed38912
Last active August 29, 2015 14:16
morse translator
# BUCKET = ::AWS::S3::Bucket.new(AWS_CONFIG["medium_bucket"])
#
# derivatives = Derivative.where(label: 'high_res_proxy_mp4', upload_complete: nil)
#
# derivatives.inject([]) do |array, derivative|
# s3_file = BUCKET.objects[derivative.file_path] rescue nil
# if s3_file && s3_file.exists? && s3_file.content_length == derivative.file_size
# array << derivative
# end
# array
http://www.erlang-in-anger.com/
http://learnyousomeerlang.com/content
require 'faraday'
require 'json'
class Temp
def temp_request
@temp_request ||= Faraday.post('https://api.spark.io/v1/devices/51ff70065067545733280187/analogread', {
access_token: '',
params: 'A0'
})
end
class Phrase
def initialize(words)
@words = words
end
def word_count
@words.downcase.scan(/\w+/).inject(Hash.new(0)) { |list,word| list.merge!({ word => list[word]+1 }) }
end
end
@photomattmills
photomattmills / cheater.rb
Created May 7, 2013 06:22
how to cheat at letterpress
#this uses /usr/share/dict/words, which isn't terribly compatible with letterpress. This is just a proof of concept, please don't cheat. Also, it's fairly slow; there's probably a better way to exclude things quickly.
def list_words(letters, length)
words = File.read("/usr/share/dict/words").split "\n"
word_arrays = words.map{|word| word.downcase.split("") }
accepted_word_arrays = word_arrays.select{|word_array| check_word(word_array, letters, length)}
words = accepted_word_arrays.map{|word_array| word_array.join("") }.compact
words.select!{|word| word.length >= length }
words.sort!{|wa,wb| wa.length <=> wb.length }
end
class Dir
def map(&block)
output = []
self.each do |entry|
output << block.call(entry)
end
return output
end
def select(&block)

So this is a fast way to test primality of a number, called a sieve. The PRIMES constant is a list of all the prime numbers below sqrt(1000000000). Computing the values in the sieve actually takes a relatively long time, on the order of seconds; someone cleverer than me could probably make it recursive, such that it would construct itself on the fly, but all of my attempts to do this took much longer than a constant.

Also, protip, don't do expensive calculations like sqrt(n) more than once if you can avoid it. That actually was a huge performance gain, the reason this is an order of magnitude (or more) faster and not just 70% faster.