Skip to content

Instantly share code, notes, and snippets.

test "/index returns a list of contacts" do
contacts_as_json =
%Contact{name: "Gumbo", phone: "(801) 555-5555"}
|> Repo.insert
|> List.wrap
|> Poison.encode!
response = conn(:get, "/api/contacts") |> send_request
assert response.status == 200
defmodule Math do
def times_three(n), do: n * 3
def odd?(n), do: rem(n, 2) != 0
end
Enum.sum(
Enum.filter(
Enum.map(1..100_000, &(Math.times_three/1)), &(Math.odd?/1)))
defmodule Recursive do
def sum([head | []]), do: head
def sum([head | [head2 | tail]]), do: sum([head + head2 | tail])
end
Recursive.sum([1,2,3]) # => 6
# Functions can put assignment logic in the arguments
[head | tail] = [1, 2, 3]
head
# => 1
tail
# => [2, 3]
defmodule D do
def add(a, b, c), do: a + b + c # Arity 3
def add(b, b), do: 3 * b # Pattern matching
def add("i", b), do: "#{b}i" # Pattern matching
def add(a, _), do: a + a # Strange default, but we'll go with it
def add(_), do: IO.puts "Can't add one number, silly"
end
# Basic function
defmodule A do
def add(a, b) do
a + b
end
end
A.add(1, 2)
# => 3
@mzemel
mzemel / generate.py
Created July 13, 2015 17:06
Generate deepdream
# imports and basic notebook setup
from cStringIO import StringIO
import numpy as np
import scipy.ndimage as nd
import PIL.Image
import sys
from IPython.display import clear_output, Image, display
from google.protobuf import text_format
import caffe
@mzemel
mzemel / generate.py
Last active August 29, 2015 14:24
Copy of Google's ipynb file
# imports and basic notebook setup
from cStringIO import StringIO
import numpy as np
import scipy.ndimage as nd
import PIL.Image
from IPython.display import clear_output, Image, display
from google.protobuf import text_format
import caffe
@mzemel
mzemel / birds.rb
Last active August 29, 2015 14:23
Bird sounds for the Raspberry Pi
require 'open-uri'
puts "URL: " + (url_string = "http://www.xeno-canto.org/164#{rand(100..999)}/download")
begin
File.open("sound.mp3", "wb") do |f|
open(url_string) do |url|
url.each_line {|u| f.write u}
end
end
@mzemel
mzemel / not.rb
Created May 14, 2015 22:11
not.rb
class NotObject
def initialize(obj)
@obj = obj
end
def method_missing(method, *arguments)
[email protected](method, *arguments)
end
end