Skip to content

Instantly share code, notes, and snippets.

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)))
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
# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
# Find the sum of all the multiples of 3 or 5 below 1000.
defmodule Multiples do
def get(n), do: sum(n, 0)
defp sum(0, accum), do: accum
defp sum(n, accum) when rem(n, 3) == 0, do: sum(n-1, accum + n)
(ns clojure-euler.core
(:gen-class))
(defn -main
[& args]
)
; If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
; Find the sum of all the multiples of 3 or 5 below 1000.
defmodule Calculator do
def add(a, b) do
a + b
end
def add(a, b), do: a + b
end
### One place you might see pattern matching
def handle_result(:success, result) do
IO.puts "Success: #{result}"
end
def handle_result(:failure, _) do
IO.puts "Failure!"
end
defmodule Calculator do
use Tracer
def concat(a, b) do
IO.puts a <> b
end
def puts_sum_three(a, b, c) do
IO.puts a + b + c
end
@mzemel
mzemel / macros.ex
Last active October 21, 2015 23:17
defmodule Scope do
defmacro update_local(val) do
local_var = "first"
result = quote do
local_var = unquote(val)
IO.puts "In macro usage: #{local_var}"
end
IO.puts "In macro definition: #{local_var}"
result
end
defmodule Tracer do
defmacro def(definition, do: _content) do
IO.inspect definition
quote do: {}
end
end
defmodule Calculator do
import Kernel, except: [def: 2]
import Tracer, only: [def: 2]
defmodule Tracer do
# ...
defmacro __using__(_opts) do
quote do
import Kernel, except: [def: 2]
import Tracer, only: [def: 2]
end
end