This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Calculator do | |
def add(a, b) do | |
a + b | |
end | |
def add(a, b), do: a + b | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Tracer do | |
# ... | |
defmacro __using__(_opts) do | |
quote do | |
import Kernel, except: [def: 2] | |
import Tracer, only: [def: 2] | |
end | |
end |