Skip to content

Instantly share code, notes, and snippets.

View pmarreck's full-sized avatar

Peter Marreck pmarreck

  • formerly desk.com, thredup.com and lifebooker.com. currently Director of Engineering @addigence
  • Long Island, NY
  • 01:42 (UTC -04:00)
View GitHub Profile
@pmarreck
pmarreck / unquote_oh_my.exs
Created April 28, 2015 21:52
elixir dynamic method definitions?
[ thousand: 2, million: 3, billion: 4, trillion: 5, quadrillion: 6, quintillion: 7, sextillion: 8, septillion: 9, octillion: 10, nonillion: 11, decillion: 12 ]
|> Enum.each(fn {illion, factor} ->
IO.puts "illion=#{illion}"
IO.puts "factor=#{factor}"
defp to_word(n) when n < unquote(:math.pow(10,factor*3)), do: to_word(div(n,unquote(:math.pow(10,(factor-1)*3)))) <> " " <> "#{unquote(illion)}" <> " " <> to_word(rem(n,unquote(:math.pow(10,(factor-1)*3))))
end)
@pmarreck
pmarreck / i_miss_you_certain_things.md
Last active August 29, 2015 14:20
The Things I See Missing From The Elixir Language

I think that folks are finally showing up to the Elixir party and (for the most part) liking what they see :)

I have only a small handful of criticisms after playing with Elixir for a while (which so far all seem like "Haskell is better in these areas," although I seem to still like Elixir better overall!)

  1. you can't define/override (per lexical scope) arbitrary infix operators, you are restricted to a small set. (In Haskell, for example, you can call any prefix function as an infix function.)

  2. you don't have any automatic currying. That's more Erlang's fault. Haskell has this, of course. In Elixir's defense, it's probably possible to define a curryable function definer using macros, like "defcurryable". Because, you know, what the hell CAN'T you do with macros? :) And Elixir has them.

  3. there is no clear distinction made between code with side effects and code without side effects. This is yet another thing I think Haskell gets right, because you can prove determinism when you can prove no side effe

@pmarreck
pmarreck / rpn.exs
Created May 4, 2015 01:44
no matches in this elixir attempt to rpn calc
# rpn.exs
# A commandline RPN calculator in Elixir... just for practice.
Code.require_file "math_integer_power.exs", __DIR__
defmodule RPN do
def initialize do
initialize(System.argv)
end
@pmarreck
pmarreck / upto100.exs
Last active August 29, 2015 14:20
Last question on the "Five programming problems every software engineer should be able to solve in < 1 hour" from Reddit, in Elixir
defmodule Upto100 do
def go do
explore("1", 1, {1, []})
end
# spillover cases
def explore(_, current_num, _) when current_num > 9 do
end
@pmarreck
pmarreck / fibonacci.rb
Last active August 29, 2015 14:20
Enumerative, procedural ("mutative") and recursive fibonacci in Ruby, with timings
# MRI hack to enable tail-call optimization making the recursive version not blow the stack:
# I'm using ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}
class Fib
def run_enumerative(n)
class Brand < ActiveRecord::Base
(0..5).each do |f|
define_method("flags_#{f}".to_sym) { read_attribute("flags_#{f}".to_sym).to_2s_complement(64) }
define_method("flags_#{f}=".to_sym){ |n| write_attribute("flags_#{f}".to_sym), n.to_2s_complement(64) }
end
# I'm getting syntax errors in the above code. Not sure why.
# ...
##### FILE : lib/words.rb
# Gives you N random word(s) over a certain length
# Only works on unixlike systems where this file exists
class Words
# DICT_PATH = '/usr/share/dict/words'
# DICT_SIZE = 234936
def self.random_word(words = 1, minlength = 6)
@@dict_all ||= File.open("/usr/share/dict/words").readlines.map{|l| l.chomp}
@@dict_size ||= @@dict_all.length
require 'digest/sha1'
require 'tempfile'
class LocalPaperclipTempfile < ::Tempfile
# note: had a ton of trouble using "data" instead of "tmp_data" for naming here:
def initialize(filename, tmp_data, tmpdir = Dir::tmpdir)
@original_filename = filename
@tmp_data = tmp_data
super Digest::SHA1.hexdigest(@tmp_data), tmpdir
module Mathhax
def /(o)
if o==0
puts "I'm sorry, #{`hostname`.chomp}, I'm afraid I can't do that"
else
super
end
end
end
require 'sender'
# For fun, trying to define my own string interpolator so that any symbol in a string (optionally end-delimited with a colon if not at a word boundary) gets substituted for its eval'd value.
# Here's what I have so far. No workie, due to context issue with "eval" call. Help?
class String
def i
self.gsub(/:([\w_]+)/){|match| __sender__.eval $1}
end