├── .gitignore ├── .travis.yml ├── README.md ├── code-reuse-hexpm.md ├── codecov_example ├── .formatter.exs ├── .gitignore ├── README.md ├── config │ └── config.exs ├── lib │ └── codecov_example.ex ├── mix.exs ├── mix.lock └── test │ ├── codecov_example_test.exs │ └── test_helper.exs ├── concurrency.png ├── examples ├── README.md ├── animals │ └── animals.ex ├── basic-types │ ├── with-match.exs │ └── with-scope.exs ├── control │ ├── case.ex │ ├── case1.exs │ ├── case2.exs │ ├── case3.exs │ ├── fizzbuzz.ex │ ├── fizzbuzz1.ex │ ├── fizzbuzz2.ex │ └── fizzbuzz3.ex ├── enum │ ├── countdown.exs │ ├── longest_line.exs │ ├── pipeline.exs │ ├── stream1.exs │ ├── stream2.exs │ └── stream3.exs ├── exceptions │ ├── catch.ex │ ├── defexception.ex │ └── exception.ex ├── first_steps │ ├── fizzbuzz.exs │ ├── handle_open.exs │ └── prefix.exs ├── functions │ ├── pin.ex │ └── pin.exs ├── intro │ └── hello.exs ├── lists │ ├── alphabetise.exs │ ├── mylist.exs │ ├── mylist1.exs │ ├── played.exs │ ├── reduce.exs │ ├── reduce1.exs │ ├── sum.exs │ ├── sum1.exs │ ├── sum2.exs │ ├── swap.exs │ ├── weather.exs │ ├── weather1.exs │ ├── weather2.exs │ └── weather3.exs ├── macros │ ├── dumper.exs │ ├── dumper1.exs │ ├── eg.exs │ ├── eg1.exs │ ├── eg2.ex │ ├── expr.exs │ ├── hygiene.ex │ ├── macro_binding.exs │ ├── macro_no_binding.exs │ ├── myif.ex │ ├── operators.ex │ ├── quote.ex │ ├── quote_binding.exs │ └── times.ex ├── maps │ ├── access1.exs │ ├── access2.exs │ ├── access3.exs │ ├── book_room.exs │ ├── defstruct.exs │ ├── defstruct1.exs │ ├── derive.exs │ ├── dynamic_nested.exs │ ├── get_in_func.exs │ ├── keywords.exs │ ├── nested.exs │ ├── query.exs │ └── use_dict.exs ├── mm │ ├── attributes.exs │ ├── attributes1.exs │ ├── debug.exs │ ├── default_params.exs │ ├── default_params1.exs │ ├── default_params1.exs.exs │ ├── default_params2.exs │ ├── factorial1-bad.exs │ ├── factorial1.exs │ ├── factorial2.exs │ ├── guard.exs │ ├── guess_number_in_range.exs │ ├── import.exs │ ├── sum_n.exs │ ├── times.exs │ └── times1.exs ├── nodes │ └── ticker.ex ├── odds │ ├── color.exs │ ├── eval │ │ ├── apps │ │ │ ├── evaluator │ │ │ │ ├── .gitignore │ │ │ │ ├── config │ │ │ │ │ └── config.exs │ │ │ │ ├── lib │ │ │ │ │ └── evaluator.ex │ │ │ │ ├── mix.exs │ │ │ │ └── test │ │ │ │ │ ├── evaluator_test.exs │ │ │ │ │ └── test_helper.exs │ │ │ └── line_sigil │ │ │ │ ├── .gitignore │ │ │ │ ├── config │ │ │ │ └── config.exs │ │ │ │ ├── lib │ │ │ │ └── line_sigil.ex │ │ │ │ ├── mix.exs │ │ │ │ └── test │ │ │ │ ├── line_sigil_test.exs │ │ │ │ └── test_helper.exs │ │ ├── config │ │ │ └── config.exs │ │ └── mix.exs │ ├── line_sigil.exs │ ├── old_eval │ │ ├── apps │ │ │ ├── evaluator │ │ │ │ ├── .gitignore │ │ │ │ ├── lib │ │ │ │ │ └── evaluator.ex │ │ │ │ ├── mix.exs │ │ │ │ └── test │ │ │ │ │ ├── evaluator_test.exs │ │ │ │ │ └── test_helper.exs │ │ │ └── line_sigil │ │ │ │ ├── .gitignore │ │ │ │ ├── lib │ │ │ │ └── line_sigil.ex │ │ │ │ ├── mix.exs │ │ │ │ └── test │ │ │ │ ├── line_sigil_test.exs │ │ │ │ └── test_helper.exs │ │ └── mix.exs │ ├── simple │ │ ├── .gitignore │ │ ├── lib │ │ │ ├── client.ex │ │ │ ├── no_specs.ex │ │ │ └── simple.ex │ │ ├── mix.exs │ │ └── test │ │ │ ├── simple_test.exs │ │ │ └── test_helper.exs │ └── simple_csv.exs ├── otp-app │ ├── sequence │ │ ├── lib │ │ │ ├── sequence.ex │ │ │ └── sequence │ │ │ │ ├── server.ex │ │ │ │ ├── stash.ex │ │ │ │ ├── subsupervisor.ex │ │ │ │ └── supervisor.ex │ │ ├── mix.exs │ │ └── test │ │ │ ├── sequence_test.exs │ │ │ └── test_helper.exs │ ├── sequence_v0 │ │ ├── lib │ │ │ ├── sequence.ex │ │ │ └── sequence │ │ │ │ ├── server.ex │ │ │ │ ├── stash.ex │ │ │ │ ├── subsupervisor.ex │ │ │ │ └── supervisor.ex │ │ ├── mix.exs │ │ ├── mix.lock │ │ └── test │ │ │ ├── sequence_test.exs │ │ │ └── test_helper.exs │ ├── sequence_v1 │ │ ├── Sequence.Server.next_numberlocalhost │ │ ├── lib │ │ │ ├── sequence.ex │ │ │ └── sequence │ │ │ │ ├── server.ex │ │ │ │ ├── stash.ex │ │ │ │ ├── subsupervisor.ex │ │ │ │ └── supervisor.ex │ │ ├── mix.exs │ │ ├── mix.lock │ │ └── test │ │ │ ├── sequence_test.exs │ │ │ └── test_helper.exs │ └── sequence_v2 │ │ ├── Sequence.Server.next_numberlocalhost │ │ ├── lib │ │ ├── sequence.ex │ │ └── sequence │ │ │ ├── server.ex │ │ │ ├── stash.ex │ │ │ ├── subsupervisor.ex │ │ │ └── supervisor.ex │ │ ├── mix.exs │ │ ├── mix.lock │ │ └── test │ │ ├── sequence_test.exs │ │ └── test_helper.exs ├── otp-fsm │ └── 1 │ │ └── listen │ │ ├── .gitignore │ │ ├── lib │ │ ├── listen.ex │ │ └── listen │ │ │ └── prism.ex │ │ ├── mix.exs │ │ └── test │ │ ├── listen_test.exs │ │ └── test_helper.exs ├── otp-server │ ├── 1 │ │ └── sequence │ │ │ ├── .gitignore │ │ │ ├── lib │ │ │ ├── sequence.ex │ │ │ └── sequence │ │ │ │ ├── server.ex │ │ │ │ └── supervisor.ex │ │ │ ├── mix.exs │ │ │ └── test │ │ │ ├── sequence_test.exs │ │ │ └── test_helper.exs │ └── 2 │ │ └── sequence │ │ ├── lib │ │ ├── sequence.ex │ │ └── sequence │ │ │ └── server.ex │ │ ├── mix.exs │ │ └── test │ │ ├── sequence_test.exs │ │ └── test_helper.exs ├── otp-supervisor │ ├── 1 │ │ └── sequence │ │ │ ├── .gitignore │ │ │ ├── config │ │ │ └── config.exs │ │ │ ├── lib │ │ │ ├── sequence.ex │ │ │ └── sequence │ │ │ │ └── server.ex │ │ │ ├── mix.exs │ │ │ └── test │ │ │ ├── sequence_test.exs │ │ │ └── test_helper.exs │ └── 2 │ │ └── sequence │ │ ├── lib │ │ ├── sequence.ex │ │ └── sequence │ │ │ ├── server.ex │ │ │ ├── stash.ex │ │ │ ├── sub_supervisor.ex │ │ │ ├── subsupervisor.ex │ │ │ └── supervisor.ex │ │ ├── mix.exs │ │ └── test │ │ ├── sequence_test.exs │ │ └── test_helper.exs ├── project │ ├── 0 │ │ └── issues │ │ │ ├── .gitignore │ │ │ ├── lib │ │ │ ├── issues.ex │ │ │ └── issues │ │ │ │ └── cli.ex │ │ │ ├── mix.exs │ │ │ ├── mix.lock │ │ │ └── test │ │ │ ├── cli_test.exs │ │ │ ├── issues_test.exs │ │ │ └── test_helper.exs │ ├── 1 │ │ └── issues │ │ │ ├── .gitignore │ │ │ ├── config │ │ │ └── config.exs │ │ │ ├── lib │ │ │ ├── issues.ex │ │ │ └── issues │ │ │ │ ├── cli.ex │ │ │ │ └── github_issues.ex │ │ │ ├── mix.exs │ │ │ ├── mix.lock │ │ │ └── test │ │ │ ├── cli_test.exs │ │ │ ├── issues_test.exs │ │ │ └── test_helper.exs │ ├── 2 │ │ └── issues │ │ │ ├── .gitignore │ │ │ ├── config │ │ │ └── config.exs │ │ │ ├── lib │ │ │ ├── issues.ex │ │ │ └── issues │ │ │ │ ├── cli.ex │ │ │ │ └── github_issues.ex │ │ │ ├── mix.exs │ │ │ ├── mix.lock │ │ │ └── test │ │ │ ├── cli_test.exs │ │ │ ├── issues_test.exs │ │ │ └── test_helper.exs │ ├── 3 │ │ └── issues │ │ │ ├── .gitignore │ │ │ ├── config │ │ │ └── config.exs │ │ │ ├── lib │ │ │ ├── issues.ex │ │ │ └── issues │ │ │ │ ├── cli.ex │ │ │ │ └── github_issues.ex │ │ │ ├── mix.exs │ │ │ ├── mix.lock │ │ │ └── test │ │ │ ├── cli_test.exs │ │ │ ├── issues_test.exs │ │ │ └── test_helper.exs │ ├── 4 │ │ └── issues │ │ │ ├── .gitignore │ │ │ ├── config │ │ │ └── config.exs │ │ │ ├── lib │ │ │ ├── issues.ex │ │ │ └── issues │ │ │ │ ├── cli.ex │ │ │ │ ├── github_issues.ex │ │ │ │ └── table_formatter.ex │ │ │ ├── mix.exs │ │ │ ├── mix.lock │ │ │ └── test │ │ │ ├── cli_test.exs │ │ │ ├── issues_test.exs │ │ │ ├── table_formatter_test.exs │ │ │ └── test_helper.exs │ └── 5 │ │ └── issues │ │ ├── .gitignore │ │ ├── config │ │ └── config.exs │ │ ├── lib │ │ ├── issues.ex │ │ └── issues │ │ │ ├── cli.ex │ │ │ ├── github_issues.ex │ │ │ └── table_formatter.ex │ │ ├── mix.exs │ │ ├── mix.lock │ │ └── test │ │ ├── cli_test.exs │ │ ├── doc_test.exs │ │ ├── issues_test.exs │ │ ├── table_formatter_test.exs │ │ └── test_helper.exs ├── protocols │ ├── basic.exs │ ├── bitmap.exs │ ├── bitmap_access.exs │ ├── bitmap_algebra.exs │ ├── bitmap_collectable.exs │ ├── bitmap_enumerable.exs │ ├── bitmap_inspect.exs │ ├── bitmap_string.exs │ ├── collect.exs │ ├── enumerable.ex │ ├── is_collection.exs │ ├── pid.exs │ └── range.ex ├── records │ ├── person.ex │ ├── person1.ex │ ├── person2.ex │ └── play.ex ├── spawn │ ├── chain.exs │ ├── fact_tr.exs │ ├── fib.exs │ ├── fib_agent.exs │ ├── fib_tr.ex │ ├── link1.exs │ ├── link2.exs │ ├── link3.exs │ ├── monitor1.exs │ ├── pmap.exs │ ├── pmap1.exs │ ├── spawn-basic.ex │ ├── spawn1.exs │ ├── spawn2.exs │ ├── spawn3.exs │ └── spawn4.exs ├── strings │ ├── nextcodepoint.ex │ ├── parse.exs │ └── utf-iterate.ex ├── tasks │ ├── agent_dict.exs │ ├── anagrams.exs │ ├── anagrams_dist.exs │ ├── fib.rb │ ├── tasks1.exs │ ├── tasks2.exs │ └── words │ │ ├── list1 │ │ ├── list2 │ │ ├── list3 │ │ └── list4 ├── tooling │ ├── describe.exs │ ├── issues │ │ ├── .gitignore │ │ ├── config │ │ │ └── config.exs │ │ ├── issues │ │ ├── lib │ │ │ ├── issues.ex │ │ │ └── issues │ │ │ │ ├── cli.ex │ │ │ │ ├── github_issues.ex │ │ │ │ └── table_formatter.ex │ │ ├── mix.exs │ │ ├── mix.lock │ │ └── test │ │ │ ├── cli_test.exs │ │ │ ├── doc_test.exs │ │ │ ├── issues_test.exs │ │ │ ├── table_formatter_test.exs │ │ │ └── test_helper.exs │ ├── pbt │ │ ├── .gitignore │ │ ├── config │ │ │ └── config.exs │ │ ├── lib │ │ │ ├── pbt.ex │ │ │ └── stats.ex │ │ ├── mix.exs │ │ └── test │ │ │ ├── describe.exs │ │ │ ├── pbt_test.exs │ │ │ ├── stats_property_test.exs │ │ │ └── test_helper.exs │ └── webapp │ │ ├── .gitignore │ │ ├── brunch-config.js │ │ ├── config │ │ ├── config.exs │ │ ├── dev.exs │ │ ├── prod.exs │ │ └── test.exs │ │ ├── lib │ │ ├── webapp.ex │ │ └── webapp │ │ │ ├── endpoint.ex │ │ │ └── repo.ex │ │ ├── mix.exs │ │ ├── mix.lock │ │ ├── package.json │ │ ├── priv │ │ ├── gettext │ │ │ ├── en │ │ │ │ └── LC_MESSAGES │ │ │ │ │ └── errors.po │ │ │ └── errors.pot │ │ └── repo │ │ │ └── seeds.exs │ │ ├── test │ │ ├── controllers │ │ │ └── page_controller_test.exs │ │ ├── support │ │ │ ├── channel_case.ex │ │ │ ├── conn_case.ex │ │ │ └── model_case.ex │ │ ├── test_helper.exs │ │ └── views │ │ │ ├── error_view_test.exs │ │ │ ├── layout_view_test.exs │ │ │ └── page_view_test.exs │ │ └── web │ │ ├── channels │ │ └── user_socket.ex │ │ ├── controllers │ │ └── page_controller.ex │ │ ├── gettext.ex │ │ ├── router.ex │ │ ├── static │ │ ├── assets │ │ │ ├── favicon.ico │ │ │ ├── images │ │ │ │ └── phoenix.png │ │ │ └── robots.txt │ │ ├── css │ │ │ ├── app.css │ │ │ └── phoenix.css │ │ └── js │ │ │ ├── app.js │ │ │ └── socket.js │ │ ├── templates │ │ ├── layout │ │ │ └── app.html.eex │ │ └── page │ │ │ └── index.html.eex │ │ ├── views │ │ ├── error_helpers.ex │ │ ├── error_view.ex │ │ ├── layout_view.ex │ │ └── page_view.ex │ │ └── web.ex ├── typespecs │ ├── nospecs │ │ ├── .gitignore │ │ ├── config │ │ │ └── config.exs │ │ ├── lib │ │ │ └── nospecs.ex │ │ ├── mix.exs │ │ └── test │ │ │ ├── nospecs_test.exs │ │ │ └── test_helper.exs │ ├── simple │ │ ├── .gitignore │ │ ├── lib │ │ │ ├── simple.ex │ │ │ └── simple │ │ │ │ └── client.ex │ │ ├── mix.exs │ │ └── test │ │ │ ├── simple_test.exs │ │ │ └── test_helper.exs │ └── simple1 │ │ ├── .gitignore │ │ ├── config │ │ └── config.exs │ │ ├── lib │ │ ├── simple1.ex │ │ └── simple1 │ │ │ └── client.ex │ │ ├── mix.exs │ │ └── test │ │ ├── simple1_test.exs │ │ └── test_helper.exs ├── unix │ └── sales.csv └── use │ ├── puts.ex │ ├── tracer.ex │ ├── tracer1.ex │ ├── tracer2.ex │ ├── tracer3.ex │ ├── tracer4.ex │ └── use.ex ├── install-mac.md └── learn-elixir-on-livebook.livemd /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | erl_crash.dump 5 | *.ez 6 | .DS_Store 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: elixir 2 | elixir: 3 | - 1.8 4 | env: 5 | - MIX_ENV=test 6 | before_install: 7 | - cd codecov_example 8 | script: 9 | - mix coveralls.json 10 | after_success: 11 | - bash <(curl -s https://codecov.io/bash) 12 | -------------------------------------------------------------------------------- /codecov_example/.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /codecov_example/.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | codecov_example-*.tar 24 | 25 | -------------------------------------------------------------------------------- /codecov_example/lib/codecov_example.ex: -------------------------------------------------------------------------------- 1 | defmodule CodecovExample do 2 | 3 | def add_one(n), do: 1 + n 4 | 5 | def add_two(n), do: 2 + n 6 | 7 | def add_three(n), do: 3 + n 8 | end 9 | -------------------------------------------------------------------------------- /codecov_example/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule CodecovExample.MixProject do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :codecov_example, 7 | version: "0.1.0", 8 | elixir: "~> 1.7", 9 | start_permanent: Mix.env() == :prod, 10 | deps: deps(), 11 | test_coverage: [tool: ExCoveralls], 12 | preferred_cli_env: [coveralls: :test] 13 | ] 14 | end 15 | 16 | # Run "mix help compile.app" to learn about applications. 17 | def application do 18 | [ 19 | extra_applications: [:logger] 20 | ] 21 | end 22 | 23 | # Run "mix help deps" to learn about dependencies. 24 | defp deps do 25 | [ 26 | {:excoveralls, "~> 0.10", only: :test} 27 | ] 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /codecov_example/test/codecov_example_test.exs: -------------------------------------------------------------------------------- 1 | defmodule CodecovExampleTest do 2 | use ExUnit.Case 3 | doctest CodecovExample 4 | 5 | describe "testing the add functions in CodecovExample" do 6 | test "adds one" do 7 | assert CodecovExample.add_one(10) == 11 8 | end 9 | 10 | test "adds two" do 11 | assert CodecovExample.add_two(10) == 12 12 | end 13 | 14 | test "adds three" do 15 | assert CodecovExample.add_three(10) == 13 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /codecov_example/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /concurrency.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dwyl/learn-elixir/e7c3df9a12c31052821577a9b2caac03555e760e/concurrency.png -------------------------------------------------------------------------------- /examples/basic-types/with-match.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | 10 | result = with {:ok, file} = File.open("/etc/passwd"), 11 | content = IO.read(file, :all), 12 | :ok = File.close(file), 13 | [_, uid, gid] <- Regex.run(~r/xxx:.*?:(\d+):(\d+)/, content) 14 | do 15 | "Group: #{gid}, User: #{uid}" 16 | end 17 | 18 | IO.puts inspect(result) #=> nil 19 | -------------------------------------------------------------------------------- /examples/basic-types/with-scope.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | content = "Now is the time" 10 | 11 | lp = with {:ok, file} = File.open("/etc/passwd"), 12 | content = IO.read(file, :all), 13 | :ok = File.close(file), 14 | [_, uid, gid] = Regex.run(~r/_lp:.*?:(\d+):(\d+)/, content) 15 | do 16 | "Group: #{gid}, User: #{uid}" 17 | end 18 | 19 | IO.puts lp #=> Group: 26, User: 26 20 | IO.puts content #=> Now is the time 21 | -------------------------------------------------------------------------------- /examples/control/case.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | case File.open("case.ex") do 10 | { :ok, file } -> 11 | IO.puts "First line: #{IO.read(file, :line)}" 12 | { :error, reason } -> 13 | IO.puts "Failed to open file: #{reason}" 14 | end 15 | 16 | 17 | -------------------------------------------------------------------------------- /examples/control/case1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Users do 10 | dave = %{ name: "Dave", state: "TX", likes: "programming" } 11 | 12 | case dave do 13 | %{state: some_state} = person -> 14 | IO.puts "#{person.name} lives in #{some_state}" 15 | _ -> 16 | IO.puts "No matches" 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /examples/control/case2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Bouncer do 10 | dave = %{name: "Dave", age: 27} 11 | 12 | case dave do 13 | person = %{age: age} when is_number(age) and age >= 21 -> 14 | IO.puts "You are cleared to enter the Foo Bar, #{person.name}" 15 | _ -> 16 | IO.puts "Sorry, no admission" 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /examples/control/case3.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | case 1 do 10 | x when x > 1 -> IO.puts "1" 11 | x when x > 0 -> IO.puts "2" 12 | end -------------------------------------------------------------------------------- /examples/control/fizzbuzz.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule FizzBuzz do 10 | 11 | def upto(n) when n > 0, do: _upto(1, n, []) 12 | 13 | defp _upto(_current, 0, result), do: Enum.reverse result 14 | 15 | defp _upto(current, left, result) do 16 | next_answer = 17 | cond do 18 | rem(current, 3) == 0 and rem(current, 5) == 0 -> 19 | "FizzBuzz" 20 | rem(current, 3) == 0 -> 21 | "Fizz" 22 | rem(current, 5) == 0 -> 23 | "Buzz" 24 | true -> 25 | current 26 | end 27 | _upto(current+1, left-1, [ next_answer | result ]) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /examples/control/fizzbuzz1.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule FizzBuzz do 10 | 11 | def upto(n) when n > 0, do: _downto(n, []) 12 | 13 | defp _downto(0, result), do: result 14 | defp _downto(current, result) do 15 | next_answer = 16 | cond do 17 | rem(current, 3) == 0 and rem(current, 5) == 0 -> 18 | "FizzBuzz" 19 | rem(current, 3) == 0 -> 20 | "Fizz" 21 | rem(current, 5) == 0 -> 22 | "Buzz" 23 | true -> 24 | current 25 | end 26 | _downto(current-1, [ next_answer | result ]) 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /examples/control/fizzbuzz2.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule FizzBuzz do 10 | def upto(n) when n > 0 do 11 | 1..n |> Enum.map(&fizzbuzz/1) 12 | end 13 | 14 | defp fizzbuzz(n) do 15 | cond do 16 | rem(n, 3) == 0 and rem(n, 5) == 0 -> 17 | "FizzBuzz" 18 | rem(n, 3) == 0 -> 19 | "Fizz" 20 | rem(n, 5) == 0 -> 21 | "Buzz" 22 | true -> 23 | n 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /examples/control/fizzbuzz3.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule FizzBuzz do 10 | def upto(n) when n > 0, do: 1..n |> Enum.map(&fizzbuzz/1) 11 | 12 | defp fizzbuzz(n), do: _fizzword(n, rem(n, 3), rem(n, 5)) 13 | 14 | defp _fizzword(_n, 0, 0), do: "FizzBuzz" 15 | defp _fizzword(_n, 0, _), do: "Fizz" 16 | defp _fizzword(_n, _, 0), do: "Buzz" 17 | defp _fizzword( n, _, _), do: n 18 | end 19 | 20 | -------------------------------------------------------------------------------- /examples/enum/longest_line.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | IO.puts File.read!("/usr/share/dict/words") 10 | |> String.split 11 | |> Enum.max_by(&String.length/1) -------------------------------------------------------------------------------- /examples/enum/pipeline.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | [ 1, 2, 3, 4, 5 ] 10 | |> Enum.map(&(&1*&1)) 11 | |> Enum.with_index 12 | |> Enum.map(fn {value, index} -> value - index end) 13 | |> IO.inspect #=> [1,3,7,13,21] -------------------------------------------------------------------------------- /examples/enum/stream1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | [1,2,3,4] 10 | |> Stream.map(&(&1*&1)) 11 | |> Stream.map(&(&1+1)) 12 | |> Stream.filter(fn x -> rem(x,2) == 1 end) 13 | |> Enum.to_list 14 | -------------------------------------------------------------------------------- /examples/enum/stream2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | IO.puts File.open!("/usr/share/dict/words") 10 | |> IO.stream(:line) 11 | |> Enum.max_by(&String.length/1) -------------------------------------------------------------------------------- /examples/enum/stream3.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | IO.puts File.stream!("/usr/share/dict/words") |> Enum.max_by(&String.length/1) -------------------------------------------------------------------------------- /examples/exceptions/catch.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Catch do 10 | 11 | def start(n) do 12 | try do 13 | incite(n) 14 | catch 15 | :exit, code -> "Exited with code #{inspect code}" 16 | :throw, value -> "throw called with #{inspect value}" 17 | what, value -> "Caught #{inspect what} with #{inspect value}" 18 | end 19 | end 20 | 21 | 22 | defp incite(1) do 23 | exit(:something_bad_happened) 24 | end 25 | 26 | defp incite(2) do 27 | throw {:animal, "wombat"} 28 | end 29 | 30 | defp incite(3) do 31 | :erlang.error "Oh no!" 32 | end 33 | end -------------------------------------------------------------------------------- /examples/first_steps/fizzbuzz.exs: -------------------------------------------------------------------------------- 1 | #--- "Fizz Buzz" reader exercise on p.41 of Programming Elixir 1.3 2 | 3 | fizzbuzz = fn 4 | (0, 0, _) -> "FizzBuzz" 5 | (0, _, _) -> "Fizz" 6 | (_, 0, _) -> "Buzz" 7 | (_, _, 1) -> "Have A Banana!" # Cabin Pressure is Pure Genius! ;-) 8 | (_, _, a) -> a 9 | end # see: https://www.youtube.com/results?search_query=Fizz+Buzz+Have+a+banana 10 | 11 | IO.puts fizzbuzz.(0,1,1) 12 | IO.puts fizzbuzz.(1,0,3) 13 | IO.puts fizzbuzz.(0,0,1) 14 | IO.puts fizzbuzz.(1,1,1) 15 | IO.puts fizzbuzz.(1,1,4) 16 | IO.puts "\n" 17 | 18 | # Exercise Functions-3 19 | fb = fn n -> fizzbuzz.(rem(n, 3), rem(n, 5), n) end 20 | 21 | IO.puts fb.(1) 22 | IO.puts fb.(2) 23 | IO.puts fb.(3) 24 | IO.puts fb.(4) 25 | IO.puts fb.(5) 26 | IO.puts fb.(6) 27 | IO.puts fb.(7) 28 | IO.puts fb.(8) 29 | IO.puts fb.(9) 30 | IO.puts fb.(10) 31 | IO.puts fb.(11) 32 | IO.puts fb.(12) 33 | IO.puts fb.(13) 34 | IO.puts fb.(14) 35 | IO.puts fb.(15) 36 | IO.puts fb.(16) 37 | IO.puts "\n" 38 | -------------------------------------------------------------------------------- /examples/first_steps/handle_open.exs: -------------------------------------------------------------------------------- 1 | #--- handle_open.exs 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | handle_open = fn 10 | {:ok, file} -> "First line: #{IO.read(file, :line)}" 11 | {_, error} -> "Error: #{:file.format_error(error)}" 12 | end 13 | IO.puts handle_open.(File.open("handle_open.exs")) # call with a file that exists 14 | IO.puts handle_open.(File.open("nonexistent")) # and then with one that doesn't 15 | -------------------------------------------------------------------------------- /examples/first_steps/prefix.exs: -------------------------------------------------------------------------------- 1 | #--- "prefix" reader exercise on p.43 of Programming Elixir 1.3 2 | 3 | prefix = fn str1 -> 4 | fn str2 -> 5 | "#{str1} #{str2}" 6 | end 7 | end 8 | 9 | IO.puts prefix.("Hello").("World!") 10 | mrs = prefix.("Mrs") 11 | IO.puts mrs.("Smith") 12 | IO.puts prefix.("Elixir").("Rocks") 13 | -------------------------------------------------------------------------------- /examples/functions/pin.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Greeter do 10 | def for(name, greeting) do 11 | fn 12 | (^name) -> "Hello #{name}!, #{greeting}" 13 | (_) -> "I don't know you." 14 | end 15 | end 16 | end 17 | 18 | mr_valim = Greeter.for("josé", "Oi!") 19 | 20 | IO.puts mr_valim.("josé") 21 | IO.puts mr_valim.("dave") 22 | -------------------------------------------------------------------------------- /examples/functions/pin.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Greeter do 10 | def for(name, greeting) do 11 | fn 12 | (^name) -> "#{greeting} #{name}" 13 | (_) -> "I don't know you" 14 | end 15 | end 16 | end 17 | 18 | mr_valim = Greeter.for("José", "Oi!") 19 | 20 | IO.puts mr_valim.("José") # => Oi! José 21 | IO.puts mr_valim.("dave") # => I don't know you 22 | -------------------------------------------------------------------------------- /examples/intro/hello.exs: -------------------------------------------------------------------------------- 1 | IO.puts "Hello, World!" 2 | -------------------------------------------------------------------------------- /examples/lists/mylist.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule MyList do 10 | def len([]), do: 0 11 | def len([head|tail]), do: 1 + len(tail) 12 | end -------------------------------------------------------------------------------- /examples/lists/mylist1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule MyList do 10 | def len([]), do: 0 11 | def len([_head | tail]), do: 1 + len(tail) 12 | 13 | def square([]), do: [] 14 | def square([ head | tail ]), do: [ head*head | square(tail) ] 15 | 16 | def add_1([]), do: [] 17 | def add_1([ head | tail ]), do: [ head+1 | add_1(tail) ] 18 | 19 | def map([], _func), do: [] 20 | def map([ head | tail ], func), do: [ func.(head) | map(tail, func) ] 21 | 22 | end 23 | -------------------------------------------------------------------------------- /examples/lists/played.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Fib do 10 | def f(0), do: 0 11 | def f(1), do: 1 12 | def f(n), do: f(n-1) + f(n-2) 13 | end 14 | 15 | IO.puts Fib.f(10) -------------------------------------------------------------------------------- /examples/lists/reduce.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule MyList do 10 | def reduce([], value, _) do 11 | value 12 | end 13 | def reduce([head | tail], value, func) do 14 | reduce(tail, func.(head, value), func) 15 | end 16 | end 17 | 18 | -------------------------------------------------------------------------------- /examples/lists/sum.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule MyList do 10 | def sum([], total), do: total 11 | def sum([ head | tail ], total), do: sum(tail, head+total) 12 | end 13 | 14 | -------------------------------------------------------------------------------- /examples/lists/sum1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule MyList do 10 | def sum([], total // 0) do total end 11 | def sum([ head | tail ], total // 0) do sum(tail, head+total) end 12 | end 13 | 14 | -------------------------------------------------------------------------------- /examples/lists/sum2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule MyList do 10 | 11 | def sum(list), do: _sum(list, 0) 12 | 13 | # private methods 14 | defp _sum([], total), do: total 15 | defp _sum([ head | tail ], total), do: _sum(tail, head+total) 16 | end 17 | 18 | -------------------------------------------------------------------------------- /examples/lists/swap.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Swapper do 10 | def swap([]), do: [] 11 | def swap([ a, b | tail ]), do: [ b, a | swap(tail) ] 12 | def swap([_]), do: raise "Can't swap a list with an odd number of elements" 13 | end 14 | -------------------------------------------------------------------------------- /examples/lists/weather.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule WeatherHistory do 10 | 11 | def test_data do 12 | [ 13 | [1366225622, 26, 15, 0.125], 14 | [1366225622, 27, 15, 0.45], 15 | [1366225622, 28, 21, 0.25], 16 | [1366229222, 26, 19, 0.081], 17 | [1366229222, 27, 17, 0.468], 18 | [1366229222, 28, 15, 0.60], 19 | [1366232822, 26, 22, 0.095], 20 | [1366232822, 27, 21, 0.05], 21 | [1366232822, 28, 24, 0.03], 22 | [1366236422, 26, 17, 0.025] 23 | ] 24 | end 25 | 26 | def for_location_27([]), do: [] 27 | def for_location_27([ [time, 27, temp, rain ] | tail]) do 28 | [ [time, 27, temp, rain] | for_location_27(tail) ] 29 | end 30 | def for_location_27([ _ | tail]), do: for_location_27(tail) 31 | 32 | end 33 | -------------------------------------------------------------------------------- /examples/macros/eg.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule My do 10 | defmacro macro(code) do 11 | IO.inspect code 12 | code 13 | end 14 | end 15 | defmodule Test do 16 | require My 17 | My.macro(IO.puts("hello")) 18 | end 19 | -------------------------------------------------------------------------------- /examples/macros/eg1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule My do 10 | defmacro macro(code) do 11 | IO.inspect code 12 | quote do: IO.puts "Different code" 13 | end 14 | end 15 | defmodule Test do 16 | require My 17 | My.macro(IO.puts("hello")) 18 | end 19 | -------------------------------------------------------------------------------- /examples/macros/eg2.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule My do 10 | defmacro macro(code) do 11 | IO.inspect code 12 | quote do 13 | IO.puts(unquote(code)) 14 | end 15 | end 16 | end 17 | 18 | defmodule Test do 19 | require My 20 | 21 | My.macro(1 + 2) 22 | end 23 | -------------------------------------------------------------------------------- /examples/macros/expr.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule T do 10 | 11 | def scan(quoted), do: _scan(quoted, []) 12 | 13 | def _scan({name, meta, params}, result) do 14 | [ _scan(name, result), _scan(meta, result), _scan(params, result) | result ] 15 | end 16 | 17 | def _scan(x, result) when is_atom(x), do: x 18 | def _scan({a,b}, result), do: [ _scan(a, result), _scan(b, result) | result ] 19 | def _scan([], result), do: [] 20 | def _scan([h|t], result), do: [ _scan(h, result), _scan(t, result) | result ] 21 | 22 | end 23 | 24 | quoted = quote do a = b + c end 25 | IO.inspect quoted 26 | IO.inspect T.scan(quoted) -------------------------------------------------------------------------------- /examples/macros/hygiene.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Scope do 10 | defmacro update_local(val) do 11 | local = "some value" 12 | result = quote do 13 | local = unquote(val) 14 | IO.puts "End of macro body, local = #{local}" 15 | end 16 | IO.puts "In macro definition, local = #{local}" 17 | result 18 | end 19 | end 20 | 21 | defmodule Test do 22 | require Scope 23 | 24 | local = 123 25 | Scope.update_local("cat") 26 | IO.puts "On return, local = #{local}" 27 | end 28 | -------------------------------------------------------------------------------- /examples/macros/macro_binding.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule My do 10 | defmacro mydef(name) do 11 | quote bind_quoted: [name: name] do 12 | def unquote(name)(), do: unquote(name) 13 | end 14 | end 15 | end 16 | 17 | defmodule Test do 18 | require My 19 | [ :fred, :bert ] |> Enum.each(&My.mydef(&1)) 20 | end 21 | 22 | IO.puts Test.fred #=> fred 23 | -------------------------------------------------------------------------------- /examples/macros/macro_no_binding.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule My do 10 | defmacro mydef(name) do 11 | quote do 12 | def unquote(name)(), do: unquote(name) 13 | end 14 | end 15 | end 16 | 17 | defmodule Test do 18 | require My 19 | [ :fred, :bert ] |> Enum.each(&My.mydef(&1)) 20 | end 21 | 22 | IO.puts Test.fred 23 | -------------------------------------------------------------------------------- /examples/macros/myif.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule My do 10 | defmacro if(condition, clauses) do 11 | do_clause = Keyword.get(clauses, :do, nil) 12 | else_clause = Keyword.get(clauses, :else, nil) 13 | quote do 14 | case unquote(condition) do 15 | val when val in [false, nil] -> unquote(else_clause) 16 | _ -> unquote(do_clause) 17 | end 18 | end 19 | end 20 | end 21 | 22 | 23 | 24 | defmodule Test do 25 | require My 26 | My.if 1==2 do 27 | IO.puts "1 == 2" 28 | else 29 | IO.puts "1 != 2" 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /examples/macros/operators.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Operators do 10 | defmacro a + b do 11 | quote do 12 | to_string(unquote(a)) <> to_string(unquote(b)) 13 | end 14 | end 15 | end 16 | 17 | defmodule Test do 18 | IO.puts(123 + 456) #=> "579" 19 | import Kernel, except: [+: 2] 20 | import Operators 21 | IO.puts(123 + 456) #=> "123456" 22 | end 23 | 24 | IO.puts(123 + 456) #=> "579" 25 | -------------------------------------------------------------------------------- /examples/macros/quote.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | -------------------------------------------------------------------------------- /examples/macros/quote_binding.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule My do 10 | defmacro macro(code) do 11 | quote bind_quoted: [local_code: code] do 12 | IO.puts(local_code) 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/macros/times.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Times do 10 | defmacro times_n(n) do 11 | name = :"times_#{n}" 12 | quote do 13 | def unquote(name)(val), do: val*unquote(n) 14 | end 15 | end 16 | end 17 | 18 | defmodule Test do 19 | require Times 20 | Times.times_n(3) 21 | Times.times_n(4) 22 | end 23 | 24 | IO.puts Test.times_3(4) #=> 12 25 | IO.puts Test.times_4(5) #=> 20 -------------------------------------------------------------------------------- /examples/maps/access3.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | cast = %{ 10 | buttercup: %{ 11 | actor: {"Robin", "Wright"}, 12 | role: "princess" 13 | }, 14 | westley: %{ 15 | actor: {"Carey", "Elwes"}, 16 | role: "farm boy" 17 | } 18 | } 19 | 20 | IO.inspect get_in(cast, [Access.key(:westley), :actor, Access.elem(1)]) 21 | #=> "Elwes" 22 | 23 | IO.inspect get_and_update_in(cast, [Access.key(:buttercup), :role], 24 | fn (val) -> {val, "Queen"} end) 25 | #=> {"princess", 26 | # %{buttercup: %{actor: {"Robin", "Wright"}, role: "Queen"}, 27 | # westley: %{actor: {"Carey", "Elwes"}, role: "farm boy"}}} 28 | 29 | 30 | -------------------------------------------------------------------------------- /examples/maps/defstruct.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Subscriber do 10 | defstruct name: "", paid: false, over_18: true 11 | end 12 | -------------------------------------------------------------------------------- /examples/maps/defstruct1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Attendee do 10 | defstruct name: "", paid: false, over_18: true 11 | 12 | def may_attend_after_party(attendee = %Attendee{}) do 13 | attendee.paid && attendee.over_18 14 | end 15 | 16 | def print_vip_badge(%Attendee{name: name}) when name != "" do 17 | IO.puts "Very cheap badge for #{name}" 18 | end 19 | 20 | def print_vip_badge(%Attendee{}) do 21 | raise "missing name for badge" 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /examples/maps/derive.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Attendee do 10 | @derive Access 11 | defstruct name: "", over_18: false 12 | end 13 | 14 | -------------------------------------------------------------------------------- /examples/maps/get_in_func.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | authors = [ 10 | %{ name: "José", language: "Elixir" }, 11 | %{ name: "Matz", language: "Ruby" }, 12 | %{ name: "Larry", language: "Perl" } 13 | ] 14 | 15 | languages_with_an_r = fn (:get, collection, next_fn) -> 16 | for row <- collection do 17 | if String.contains?(row.language, "r") do 18 | next_fn.(row) 19 | end 20 | end 21 | end 22 | 23 | IO.inspect get_in(authors, [languages_with_an_r, :name]) 24 | #=> [ "José", nil, "Larry" ] 25 | -------------------------------------------------------------------------------- /examples/maps/nested.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Customer do 10 | defstruct name: "", company: "" 11 | end 12 | 13 | defmodule BugReport do 14 | defstruct owner: %Customer{}, details: "", severity: 1 15 | end 16 | 17 | defmodule User do 18 | report = %BugReport{owner: %Customer{name: "Dave", company: "Pragmatic"}, 19 | details: "broken"} 20 | 21 | IO.inspect report 22 | 23 | report = %BugReport{ report | owner: %Customer{ report.owner | company: "PragProg" }} 24 | 25 | IO.inspect report 26 | 27 | IO.inspect update_in(report.owner.name, &("Mr. " <> &1)) 28 | end 29 | -------------------------------------------------------------------------------- /examples/maps/query.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | people = [ 10 | %{ name: "Grumpy", height: 1.24 }, 11 | %{ name: "Dave", height: 1.88 }, 12 | %{ name: "Dopey", height: 1.32 }, 13 | %{ name: "Shaquille", height: 2.16 }, 14 | %{ name: "Sneezy", height: 1.28 } 15 | ] 16 | 17 | IO.inspect(for person = %{ height: height } <- people, height > 1.5, do: person) 18 | -------------------------------------------------------------------------------- /examples/maps/use_dict.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sum do 10 | def values(dict) do 11 | dict |> Dict.values |> Enum.sum 12 | end 13 | end 14 | 15 | # Sum a HashDict 16 | hd = [ one: 1, two: 2, three: 3 ] |> Enum.into HashDict.new 17 | IO.puts Sum.values(hd) # => 6 18 | 19 | # Sum a Map 20 | map = %{ four: 4, five: 5, six: 6 } 21 | IO.puts Sum.values(map) # => 15 22 | -------------------------------------------------------------------------------- /examples/mm/attributes.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Example do 10 | @author "Dave Thomas" 11 | def get_author do 12 | @author 13 | end 14 | end 15 | IO.puts "Example was written by #{Example.get_author}" 16 | -------------------------------------------------------------------------------- /examples/mm/attributes1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Example do 10 | @attr "one" 11 | def first, do: @attr 12 | @attr "two" 13 | def second, do: @attr 14 | end 15 | IO.puts "#{Example.second} #{Example.first}" # => two one 16 | -------------------------------------------------------------------------------- /examples/mm/debug.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Debug do 10 | def value(val) when is_integer(val), do: "integer: #{val}" 11 | def value(val) when is_atom(val), do: "atom: #{val}" 12 | def value(val) when is_list(val), do: "list: #{inspect val}" 13 | def value(val), do: "#{val}" 14 | end -------------------------------------------------------------------------------- /examples/mm/default_params.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Example do 10 | def func(p1, p2 \\ 2, p3 \\ 3, p4) do 11 | IO.inspect [p1, p2, p3, p4] 12 | end 13 | end 14 | 15 | Example.func("a", "b") # => ["a",2,3,"b"] 16 | Example.func("a", "b", "c") # => ["a","b",3,"c"] 17 | Example.func("a", "b", "c", "d") # => ["a","b","c","d"] 18 | 19 | -------------------------------------------------------------------------------- /examples/mm/default_params1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule DefaultParams1 do 10 | 11 | def func(p1, p2 \\ 123) do 12 | IO.inspect [p1, p2] 13 | end 14 | 15 | def func(p1, 99) do 16 | IO.puts "you said 99" 17 | end 18 | 19 | end 20 | 21 | IO -------------------------------------------------------------------------------- /examples/mm/default_params1.exs.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule DefaultParams1 do 10 | 11 | def func(p1, p2 \\ 123) do 12 | IO.puts "You used the default" 13 | end 14 | 15 | def func(p1, 99) do 16 | IO.puts "you said 99" 17 | end 18 | end 19 | 20 | IO -------------------------------------------------------------------------------- /examples/mm/default_params2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Params do 10 | 11 | def func(p1, p2 \\ 123) 12 | 13 | def func(p1, p2) when is_list(p1) do 14 | "You said #{p2} with a list" 15 | end 16 | 17 | def func(p1, p2) do 18 | "You passed in #{p1} and #{p2}" 19 | end 20 | 21 | end 22 | 23 | IO.puts Params.func(99) # You passed in 99 and 123 24 | IO.puts Params.func(99, "cat") # You passed in 99 and cat 25 | IO.puts Params.func([99]) # You said 123 with a list 26 | IO.puts Params.func([99], "dog") # You said dog with a list 27 | -------------------------------------------------------------------------------- /examples/mm/factorial1-bad.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule BadFactorial do 10 | def of(n), do: n * of(n-1) 11 | def of(0), do: 1 12 | end -------------------------------------------------------------------------------- /examples/mm/factorial1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Factorial do 10 | def of(0), do: 1 11 | def of(n), do: n * of(n-1) 12 | end -------------------------------------------------------------------------------- /examples/mm/factorial2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Factorial do 10 | def of(0), do: 1 11 | def of(n) when n > 0 do 12 | n * of(n-1) 13 | end 14 | end -------------------------------------------------------------------------------- /examples/mm/guard.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Guard do 10 | def what_is(x) when is_number(x) do 11 | IO.puts "#{x} is a number" 12 | end 13 | def what_is(x) when is_list(x) do 14 | IO.puts "#{inspect(x)} is a list" 15 | end 16 | def what_is(x) when is_atom(x) do 17 | IO.puts "#{x} is an atom" 18 | end 19 | end 20 | 21 | Guard.what_is(99) # => 99 is a number 22 | Guard.what_is(:cat) # => cat is an atom 23 | Guard.what_is([1,2,3]) # => [1,2,3] is a list 24 | -------------------------------------------------------------------------------- /examples/mm/guess_number_in_range.exs: -------------------------------------------------------------------------------- 1 | # Exercise: ModulesAndFunctions-5 implement Chop.guess(n, low..range) 2 | # run the file to see output 3 | defmodule Chop do 4 | def guess(n, low..high) do 5 | number = div(high + low, 2) 6 | IO.puts "Is it #{number}? (#{n}, #{low}..#{high})" 7 | make_guess(number, n, low..high) 8 | end 9 | 10 | def make_guess(number, n, _) when number == n do 11 | IO.puts "Found it: #{n}" 12 | end 13 | 14 | def make_guess(number, n, low.._) when number > n do 15 | guess(n, low..number) 16 | end 17 | 18 | def make_guess(number, n, _..high) when number < n do 19 | guess(n, number..high) 20 | end 21 | end 22 | 23 | Chop.guess(273, 1..1000) 24 | 25 | Chop.guess(911, 1..1000) 26 | -------------------------------------------------------------------------------- /examples/mm/import.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Example do 10 | def func1 do 11 | List.flatten [1,[2,3],4] 12 | end 13 | def func2 do 14 | import List, only: [flatten: 1] 15 | flatten [5,[6,7],8] 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /examples/mm/sum_n.exs: -------------------------------------------------------------------------------- 1 | # Exercise: ModulesAndFunctions-4 implement sum(n) with recursion 2 | # return the result of adding all number up to n 3 | # e.g: Sum.sum(5) is 15 (1+2+3+4+5) 4 | defmodule Sum do 5 | def sum(0), do: 0 6 | def sum(n) when n > 0, do: n + sum(n - 1) 7 | end 8 | -------------------------------------------------------------------------------- /examples/mm/times.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Times do 10 | def double(n) do 11 | n * 2 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /examples/mm/times1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Times do 10 | def double(n), do: n * 2 11 | end -------------------------------------------------------------------------------- /examples/odds/eval/apps/evaluator/.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | erl_crash.dump 5 | *.ez 6 | -------------------------------------------------------------------------------- /examples/odds/eval/apps/evaluator/lib/evaluator.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Evaluator do 10 | 11 | def eval(list_of_expressions) do 12 | { result, _final_binding } = 13 | Enum.reduce(list_of_expressions, 14 | {_result = [], _binding = binding()}, 15 | &evaluate_with_binding/2) 16 | Enum.reverse result 17 | end 18 | 19 | defp evaluate_with_binding(expression, { result, binding }) do 20 | { next_result, new_binding } = Code.eval_string(expression, binding) 21 | { [ "value> #{next_result}", "code> #{expression}" | result ], new_binding } 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /examples/odds/eval/apps/evaluator/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Evaluator.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :evaluator, 15 | version: "0.0.1", 16 | build_path: "../../_build", 17 | config_path: "../../config/config.exs", 18 | deps_path: "../../deps", 19 | lockfile: "../../mix.lock", 20 | build_embedded: Mix.env == :prod, 21 | start_permanent: Mix.env == :prod, 22 | deps: deps() 23 | ] 24 | end 25 | 26 | def application do 27 | [ 28 | applications: [:logger] 29 | ] 30 | end 31 | 32 | defp deps do 33 | [] 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /examples/odds/eval/apps/evaluator/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start() 10 | -------------------------------------------------------------------------------- /examples/odds/eval/apps/line_sigil/.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | erl_crash.dump 5 | *.ez 6 | -------------------------------------------------------------------------------- /examples/odds/eval/apps/line_sigil/lib/line_sigil.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule LineSigil do 10 | 11 | @doc """ 12 | 13 | Implement the `~l` sigil, which takes a string containing 14 | multiple lines and returns a list of those lines. 15 | 16 | ## Example usage 17 | 18 | iex> import LineSigil 19 | nil 20 | iex> ~l\""" 21 | ...> one 22 | ...> two 23 | ...> three 24 | ...> \""" 25 | ["one","two","three"] 26 | """ 27 | 28 | def sigil_l(lines, _opts) do 29 | lines |> String.rstrip |> String.split("\n") 30 | end 31 | 32 | end 33 | -------------------------------------------------------------------------------- /examples/odds/eval/apps/line_sigil/test/line_sigil_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule LineSigilTest do 10 | use ExUnit.Case 11 | doctest LineSigil 12 | 13 | test "the truth" do 14 | assert 1 + 1 == 2 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /examples/odds/eval/apps/line_sigil/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start() 10 | -------------------------------------------------------------------------------- /examples/odds/line_sigil.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule LineSigil do 10 | @doc """ 11 | Implement the `~l` sigil, which takes a string containing 12 | multiple lines and returns a list of those lines. 13 | 14 | ## Example usage 15 | 16 | iex> import LineSigil 17 | nil 18 | iex> ~l\""" 19 | ...> one 20 | ...> two 21 | ...> three 22 | ...> \""" 23 | ["one","two","three"] 24 | """ 25 | def sigil_l(lines, _opts) do 26 | lines |> String.rstrip |> String.split("\n") 27 | end 28 | end 29 | 30 | defmodule Example do 31 | import LineSigil 32 | 33 | def lines do 34 | ~l""" 35 | line 1 36 | line 2 37 | and another line in #{__MODULE__} 38 | """ 39 | end 40 | end 41 | 42 | IO.inspect Example.lines 43 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/evaluator/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/evaluator/lib/evaluator.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Evaluator do 10 | 11 | def eval(list_of_expressions) do 12 | { result, _final_binding } = 13 | Enum.reduce(list_of_expressions, 14 | {_result = [], _binding = binding()}, 15 | &evaluate_with_binding/2) 16 | Enum.reverse result 17 | end 18 | 19 | defp evaluate_with_binding(expression, { result, binding }) do 20 | { next_result, new_binding } = Code.eval_string(expression, binding) 21 | { [ "value> #{next_result}", "code> #{expression}" | result ], new_binding } 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/evaluator/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Evaluator.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :evaluator, 15 | version: "0.0.1", 16 | deps: deps(Mix.env) 17 | ] 18 | end 19 | 20 | # Configuration for the OTP application 21 | def application do 22 | [ ] 23 | end 24 | 25 | defp deps(:test) do 26 | [ { :line_sigil, path: "../line_sigil" } ] ++ deps(:default) 27 | end 28 | 29 | defp deps(_) do 30 | [] 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/evaluator/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/line_sigil/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/line_sigil/lib/line_sigil.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule LineSigil do 10 | 11 | @doc """ 12 | 13 | Implement the `~l` sigil, which takes a string containing 14 | multiple lines and returns a list of those lines. 15 | 16 | ## Example usage 17 | 18 | iex> import LineSigil 19 | nil 20 | iex> ~l""" 21 | ...> one 22 | ...> two 23 | ...> three 24 | ...> """ 25 | ["one","two","three"] 26 | """ 27 | 28 | def sigil_l(lines, _opts) do 29 | lines |> String.rstrip |> String.split("\n") 30 | end 31 | 32 | end 33 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/line_sigil/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule LineSigil.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :line_sigil, 15 | version: "0.0.1", 16 | deps: deps() 17 | ] 18 | end 19 | 20 | # Configuration for the OTP application 21 | def application do 22 | [] 23 | end 24 | 25 | # Returns the list of dependencies in the format: 26 | # { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" } 27 | defp deps do 28 | [] 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/line_sigil/test/line_sigil_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule LineSigilTest do 10 | use ExUnit.Case 11 | 12 | test "the truth" do 13 | assert(true) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/odds/old_eval/apps/line_sigil/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/odds/old_eval/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Eval.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | apps_path: "apps", 15 | deps: deps() 16 | ] 17 | end 18 | 19 | # Returns the list of dependencies in the format: 20 | # { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" } 21 | # These dependencies are not accessible from child applications 22 | defp deps do 23 | [] 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /examples/odds/simple/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/odds/simple/lib/client.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Client do 10 | 11 | @spec other_function() :: non_neg_integer 12 | def other_function do 13 | Simple.count_atoms [[:a, :b], :c] 14 | end 15 | 16 | end -------------------------------------------------------------------------------- /examples/odds/simple/lib/no_specs.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule NoSpecs do 10 | 11 | def length_plus_n(list, n) do 12 | length(list) + n 13 | end 14 | 15 | def call_it do 16 | length_plus_n([:a, :b], :c) 17 | end 18 | end 19 | 20 | -------------------------------------------------------------------------------- /examples/odds/simple/lib/simple.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Simple do 10 | 11 | @type atom_list :: list(atom) 12 | 13 | @spec count_atoms(atom_list) :: non_neg_integer 14 | 15 | def count_atoms(list) do 16 | length list 17 | end 18 | 19 | end 20 | -------------------------------------------------------------------------------- /examples/odds/simple/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Simple.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :simple, 15 | version: "0.0.1", 16 | deps: deps() 17 | ] 18 | end 19 | 20 | # Configuration for the OTP application 21 | def application do 22 | [] 23 | end 24 | 25 | # Returns the list of dependencies in the format: 26 | # { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" } 27 | defp deps do 28 | [] 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /examples/odds/simple/test/simple_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | Code.require_file "test_helper.exs", __DIR__ 10 | 11 | defmodule SimpleTest do 12 | use ExUnit.Case 13 | 14 | test "the truth" do 15 | assert(true) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /examples/odds/simple/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/odds/simple_csv.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SimpleCsv do 10 | 11 | def sigil_v(lines, options) do 12 | separator = cond do 13 | ?c in options -> "," 14 | ?t in options -> "\t" 15 | true -> "," 16 | end 17 | 18 | lines 19 | |> String.rstrip 20 | |> String.split("\n") 21 | |> Enum.map(&String.split(&1, separator)) 22 | end 23 | 24 | end 25 | 26 | defmodule Example do 27 | import SimpleCsv 28 | 29 | def comma do 30 | ~v""" 31 | 1,2,cat 32 | 4,5,6,7 33 | linus,lucy 34 | """ 35 | end 36 | 37 | def tab do 38 | ~v""" 39 | 1\t2\tcat 40 | 4\t5\t6\t7 41 | linus\tlucy 42 | """t 43 | end 44 | 45 | end 46 | 47 | IO.inspect Example.comma 48 | IO.inspect Example.tab 49 | -------------------------------------------------------------------------------- /examples/otp-app/sequence/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | def start(_type, initial_number) do 13 | Sequence.Supervisor.start_link(initial_number) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-app/sequence/lib/sequence/stash.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Stash do 10 | use GenServer 11 | 12 | ##### 13 | # External API 14 | 15 | def start_link(current_number) do 16 | GenServer.start_link( __MODULE__, current_number) 17 | end 18 | 19 | def save_value(pid, value) do 20 | GenServer.cast pid, {:save_value, value} 21 | end 22 | 23 | def get_value(pid) do 24 | GenServer.call pid, :get_value 25 | end 26 | 27 | ##### 28 | # GenServer implementation 29 | 30 | def handle_call(:get_value, _from, current_value) do 31 | { :reply, current_value, current_value } 32 | end 33 | 34 | def handle_cast({:save_value, value}, _current_value) do 35 | { :noreply, value} 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /examples/otp-app/sequence/lib/sequence/subsupervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.SubSupervisor do 10 | use Supervisor 11 | 12 | def start_link(stash_pid) do 13 | Supervisor.start_link(__MODULE__, stash_pid) 14 | end 15 | 16 | def init(stash_pid) do 17 | child_processes = [ worker(Sequence.Server, [stash_pid]) ] 18 | supervise child_processes, strategy: :one_for_one 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /examples/otp-app/sequence/lib/sequence/supervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Supervisor do 10 | use Supervisor 11 | 12 | def start_link(initial_number) do 13 | result = {:ok, sup } = Supervisor.start_link(__MODULE__, [initial_number]) 14 | start_workers(sup, initial_number) 15 | result 16 | end 17 | 18 | def start_workers(sup, initial_number) do 19 | # Start the stash worker 20 | {:ok, stash} = 21 | Supervisor.start_child(sup, worker(Sequence.Stash, [initial_number])) 22 | 23 | # and then the subsupervisor for the actual sequence server 24 | Supervisor.start_child(sup, supervisor(Sequence.SubSupervisor, [stash])) 25 | end 26 | 27 | def init(_) do 28 | supervise [], strategy: :one_for_one 29 | end 30 | 31 | end -------------------------------------------------------------------------------- /examples/otp-app/sequence/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Mixfile do 10 | use Mix.Project 11 | 12 | # ... 13 | 14 | def project do 15 | [ 16 | app: :sequence, 17 | version: "0.0.1", 18 | deps: deps() 19 | ] 20 | end 21 | 22 | # Configuration for the OTP application 23 | def application do 24 | [ 25 | mod: { Sequence, 456 }, 26 | registered: [ Sequence.Server ] 27 | ] 28 | end 29 | 30 | defp deps do 31 | [] 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /examples/otp-app/sequence/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | end 13 | -------------------------------------------------------------------------------- /examples/otp-app/sequence/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v0/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | def start(_type, initial_number) do 13 | Sequence.Supervisor.start_link(initial_number) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v0/lib/sequence/stash.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Stash do 10 | use GenServer 11 | 12 | ##### 13 | # External API 14 | 15 | def start_link(current_number) do 16 | GenServer.start_link( __MODULE__, current_number) 17 | end 18 | 19 | def save_value(pid, value) do 20 | GenServer.cast pid, {:save_value, value} 21 | end 22 | 23 | def get_value(pid) do 24 | GenServer.call pid, :get_value 25 | end 26 | 27 | ##### 28 | # GenServer implementation 29 | 30 | def handle_call(:get_value, _from, current_value) do 31 | { :reply, current_value, current_value } 32 | end 33 | 34 | def handle_cast({:save_value, value}, _current_value) do 35 | { :noreply, value} 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v0/lib/sequence/subsupervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.SubSupervisor do 10 | use Supervisor 11 | 12 | def start_link(stash_pid) do 13 | Supervisor.start_link(__MODULE__, stash_pid) 14 | end 15 | 16 | def init(stash_pid) do 17 | child_processes = [ worker(Sequence.Server, [stash_pid]) ] 18 | supervise child_processes, strategy: :one_for_one 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /examples/otp-app/sequence_v0/lib/sequence/supervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Supervisor do 10 | use Supervisor 11 | 12 | def start_link(initial_number) do 13 | result = {:ok, sup } = Supervisor.start_link(__MODULE__, [initial_number]) 14 | start_workers(sup, initial_number) 15 | result 16 | end 17 | 18 | def start_workers(sup, initial_number) do 19 | # Start the stash worker 20 | {:ok, stash} = 21 | Supervisor.start_child(sup, worker(Sequence.Stash, [initial_number])) 22 | 23 | # and then the subsupervisor for the actual sequence server 24 | Supervisor.start_child(sup, supervisor(Sequence.SubSupervisor, [stash])) 25 | end 26 | 27 | def init(_) do 28 | supervise [], strategy: :one_for_one 29 | end 30 | 31 | end -------------------------------------------------------------------------------- /examples/otp-app/sequence_v0/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Mixfile do 10 | use Mix.Project 11 | 12 | # ... 13 | 14 | def project do 15 | [ 16 | app: :sequence, 17 | version: "0.0.1", 18 | deps: deps() 19 | ] 20 | end 21 | 22 | # Configuration for the OTP application 23 | def application do 24 | [ 25 | mod: { Sequence, 456 }, 26 | registered: [ Sequence.Server ] 27 | ] 28 | end 29 | 30 | defp deps do 31 | [ 32 | {:exrm, "~> 1.0.6"} 33 | ] 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v0/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | end 13 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v0/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/Sequence.Server.next_numberlocalhost: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dwyl/learn-elixir/e7c3df9a12c31052821577a9b2caac03555e760e/examples/otp-app/sequence_v1/Sequence.Server.next_numberlocalhost -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | def start(_type, initial_number) do 13 | Sequence.Supervisor.start_link(initial_number) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/lib/sequence/stash.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Stash do 10 | use GenServer 11 | 12 | ##### 13 | # External API 14 | 15 | def start_link(current_number) do 16 | GenServer.start_link( __MODULE__, current_number) 17 | end 18 | 19 | def save_value(pid, value) do 20 | GenServer.cast pid, {:save_value, value} 21 | end 22 | 23 | def get_value(pid) do 24 | GenServer.call pid, :get_value 25 | end 26 | 27 | ##### 28 | # GenServer implementation 29 | 30 | def handle_call(:get_value, _from, current_value) do 31 | { :reply, current_value, current_value } 32 | end 33 | 34 | def handle_cast({:save_value, value}, _current_value) do 35 | { :noreply, value} 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/lib/sequence/subsupervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.SubSupervisor do 10 | use Supervisor 11 | 12 | def start_link(stash_pid) do 13 | Supervisor.start_link(__MODULE__, stash_pid) 14 | end 15 | 16 | def init(stash_pid) do 17 | child_processes = [ worker(Sequence.Server, [stash_pid]) ] 18 | supervise child_processes, strategy: :one_for_one 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/lib/sequence/supervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Supervisor do 10 | use Supervisor 11 | 12 | def start_link(initial_number) do 13 | result = {:ok, sup } = Supervisor.start_link(__MODULE__, [initial_number]) 14 | start_workers(sup, initial_number) 15 | result 16 | end 17 | 18 | def start_workers(sup, initial_number) do 19 | # Start the stash worker 20 | {:ok, stash} = 21 | Supervisor.start_child(sup, worker(Sequence.Stash, [initial_number])) 22 | 23 | # and then the subsupervisor for the actual sequence server 24 | Supervisor.start_child(sup, supervisor(Sequence.SubSupervisor, [stash])) 25 | end 26 | 27 | def init(_) do 28 | supervise [], strategy: :one_for_one 29 | end 30 | 31 | end -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Mixfile do 10 | use Mix.Project 11 | 12 | # ... 13 | def project do 14 | [ 15 | app: :sequence, 16 | version: "0.0.2", 17 | deps: deps() 18 | ] 19 | end 20 | 21 | # Configuration for the OTP application 22 | def application do 23 | [ 24 | mod: { Sequence, 456 }, 25 | registered: [ Sequence.Server ] 26 | ] 27 | end 28 | 29 | defp deps do 30 | [ exrm: "~> 1.0.6" ] 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | end 13 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v1/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/Sequence.Server.next_numberlocalhost: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dwyl/learn-elixir/e7c3df9a12c31052821577a9b2caac03555e760e/examples/otp-app/sequence_v2/Sequence.Server.next_numberlocalhost -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | def start(_type, initial_number) do 13 | Sequence.Supervisor.start_link(initial_number) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/lib/sequence/stash.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Stash do 10 | use GenServer 11 | 12 | ##### 13 | # External API 14 | 15 | def start_link(current_number) do 16 | GenServer.start_link( __MODULE__, current_number) 17 | end 18 | 19 | def save_value(pid, value) do 20 | GenServer.cast pid, {:save_value, value} 21 | end 22 | 23 | def get_value(pid) do 24 | GenServer.call pid, :get_value 25 | end 26 | 27 | ##### 28 | # GenServer implementation 29 | 30 | def handle_call(:get_value, _from, current_value) do 31 | { :reply, current_value, current_value } 32 | end 33 | 34 | def handle_cast({:save_value, value}, _current_value) do 35 | { :noreply, value} 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/lib/sequence/subsupervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.SubSupervisor do 10 | use Supervisor 11 | 12 | def start_link(stash_pid) do 13 | Supervisor.start_link(__MODULE__, stash_pid) 14 | end 15 | 16 | def init(stash_pid) do 17 | child_processes = [ worker(Sequence.Server, [stash_pid]) ] 18 | supervise child_processes, strategy: :one_for_one 19 | end 20 | 21 | end -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :sequence, 15 | version: "0.0.3", 16 | deps: deps() 17 | ] 18 | end 19 | def application do 20 | [ 21 | mod: { Sequence, 456 }, 22 | registered: [ Sequence.Server ], 23 | applications: [ :logger ] 24 | ] 25 | end 26 | 27 | defp deps do 28 | [ exrm: "~> 1.0.6" ] 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/mix.lock: -------------------------------------------------------------------------------- 1 | %{"bbmustache": {:hex, :bbmustache, "1.0.3"}, 2 | "erlware_commons": {:hex, :erlware_commons, "0.15.0"}, 3 | "exrm": {:hex, :exrm, "1.0.0-rc7"}, 4 | "getopt": {:hex, :getopt, "0.8.2"}, 5 | "providers": {:hex, :providers, "1.4.1"}, 6 | "relx": {:hex, :relx, "3.5.0"}} 7 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | end 13 | -------------------------------------------------------------------------------- /examples/otp-app/sequence_v2/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/otp-fsm/1/listen/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | *.ez 5 | -------------------------------------------------------------------------------- /examples/otp-fsm/1/listen/lib/listen.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Listen do 10 | end 11 | -------------------------------------------------------------------------------- /examples/otp-fsm/1/listen/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Listen.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :listen, 15 | version: "0.0.1", 16 | deps: deps() 17 | ] 18 | end 19 | 20 | # Configuration for the OTP application 21 | def application do 22 | [] 23 | end 24 | 25 | # Returns the list of dependencies in the format: 26 | # { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" } 27 | defp deps do 28 | [] 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /examples/otp-fsm/1/listen/test/listen_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | Code.require_file "test_helper.exs", __DIR__ 10 | 11 | defmodule ListenTest do 12 | use ExUnit.Case 13 | 14 | test "the truth" do 15 | assert(true) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /examples/otp-fsm/1/listen/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/otp-server/1/sequence/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/otp-server/1/sequence/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | # See http://elixir-lang.org/docs/stable/Application.Behaviour.html 13 | # for more information on OTP Applications 14 | def start(_type, _args) do 15 | Sequence.Supervisor.start_link 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /examples/otp-server/1/sequence/lib/sequence/server.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Server do 10 | use GenServer 11 | 12 | def handle_call(:next_number, _from, current_number) do 13 | { :reply, current_number, current_number+1 } 14 | end 15 | 16 | def handle_cast({:increment_number, delta}, current_number) do 17 | { :noreply, current_number + delta} 18 | end 19 | 20 | def format_status(_reason, [ _pdict, state ]) do 21 | [data: [{'State', "My current state is '#{inspect state}', and I'm happy"}]] 22 | end 23 | 24 | end 25 | -------------------------------------------------------------------------------- /examples/otp-server/1/sequence/lib/sequence/supervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Supervisor do 10 | use Supervisor 11 | 12 | def start_link do 13 | :supervisor.start_link(__MODULE__, []) 14 | end 15 | 16 | def init([]) do 17 | children = [ 18 | # Define workers and child supervisors to be supervised 19 | # worker(Sequence.Worker, []) 20 | ] 21 | 22 | # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html 23 | # for other strategies and supported options 24 | supervise(children, strategy: :one_for_one) 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /examples/otp-server/1/sequence/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :sequence, 15 | version: "0.0.1", 16 | elixir: "~> 1.3", 17 | deps: deps() 18 | ] 19 | end 20 | 21 | # Configuration for the OTP application 22 | def application do 23 | [mod: { Sequence, [] }] 24 | end 25 | 26 | # Returns the list of dependencies in the format: 27 | # { :foobar, git: "https://github.com/elixir-lang/foobar.git", tag: "0.1" } 28 | # 29 | # To specify particular versions, regardless of the tag, do: 30 | # { :barbat, "~> 0.1", github: "elixir-lang/barbat.git" } 31 | defp deps do 32 | [] 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /examples/otp-server/1/sequence/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | test "the truth" do 13 | 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-server/1/sequence/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/otp-server/2/sequence/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | # See http://elixir-lang.org/docs/stable/elixir/Application.html 13 | # for more information on OTP Applications 14 | def start(_type, _args) do 15 | import Supervisor.Spec, warn: false 16 | 17 | children = [ ] 18 | 19 | opts = [strategy: :one_for_one, name: Sequence.Supervisor] 20 | Supervisor.start_link(children, opts) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /examples/otp-server/2/sequence/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :sequence, 15 | version: "0.0.1", 16 | deps: deps() 17 | ] 18 | end 19 | 20 | # Configuration for the OTP application 21 | def application do 22 | [mod: { Sequence, [] }] 23 | end 24 | 25 | # Returns the list of dependencies in the format: 26 | # { :foobar, git: "https://github.com/elixir-lang/foobar.git", tag: "0.1" } 27 | # 28 | # To specify particular versions, regardless of the tag, do: 29 | # { :barbat, "~> 0.1", github: "elixir-lang/barbat.git" } 30 | defp deps do 31 | [] 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /examples/otp-server/2/sequence/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | test "the truth" do 13 | 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-server/2/sequence/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/otp-supervisor/1/sequence/.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /deps 3 | erl_crash.dump 4 | *.ez 5 | -------------------------------------------------------------------------------- /examples/otp-supervisor/1/sequence/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | def start(_type, _args) do 13 | import Supervisor.Spec, warn: false 14 | 15 | children = [ 16 | worker(Sequence.Server, [123]) 17 | ] 18 | 19 | opts = [strategy: :one_for_one, name: Sequence.Supervisor] 20 | {:ok, _pid} = Supervisor.start_link(children, opts) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /examples/otp-supervisor/1/sequence/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | test "the truth" do 13 | assert 1 + 1 == 2 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-supervisor/1/sequence/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start() 10 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/lib/sequence.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence do 10 | use Application 11 | 12 | def start(_type, _args) do 13 | {:ok, _pid} = Sequence.Supervisor.start_link(123) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/lib/sequence/stash.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Stash do 10 | use GenServer 11 | 12 | ##### 13 | # External API 14 | 15 | def start_link(current_number) do 16 | {:ok,_pid} = GenServer.start_link( __MODULE__, current_number) 17 | end 18 | 19 | def save_value(pid, value) do 20 | GenServer.cast pid, {:save_value, value} 21 | end 22 | 23 | def get_value(pid) do 24 | GenServer.call pid, :get_value 25 | end 26 | 27 | ##### 28 | # GenServer implementation 29 | 30 | def handle_call(:get_value, _from, current_value) do 31 | { :reply, current_value, current_value } 32 | end 33 | 34 | def handle_cast({:save_value, value}, _current_value) do 35 | { :noreply, value} 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/lib/sequence/sub_supervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.SubSupervisor do 10 | use Supervisor 11 | 12 | def start_link(stash_pid) do 13 | {:ok, _pid} = Supervisor.start_link(__MODULE__, stash_pid) 14 | end 15 | def init(stash_pid) do 16 | child_processes = [ worker(Sequence.Server, [stash_pid]) ] 17 | supervise child_processes, strategy: :one_for_one 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/lib/sequence/subsupervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/lib/sequence/supervisor.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Supervisor do 10 | use Supervisor 11 | def start_link(initial_number) do 12 | result = {:ok, sup } = Supervisor.start_link(__MODULE__, [initial_number]) 13 | start_workers(sup, initial_number) 14 | result 15 | end 16 | def start_workers(sup, initial_number) do 17 | # Start the stash worker 18 | {:ok, stash} = 19 | Supervisor.start_child(sup, worker(Sequence.Stash, [initial_number])) 20 | # and then the subsupervisor for the actual sequence server 21 | Supervisor.start_child(sup, supervisor(Sequence.SubSupervisor, [stash])) 22 | end 23 | def init(_) do 24 | supervise [], strategy: :one_for_one 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Sequence.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :sequence, 15 | version: "0.0.1", 16 | deps: deps() 17 | ] 18 | end 19 | 20 | # Configuration for the OTP application 21 | def application do 22 | [mod: { Sequence, [] }] 23 | end 24 | 25 | # Returns the list of dependencies in the format: 26 | # { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" } 27 | defp deps do 28 | [] 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/test/sequence_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SequenceTest do 10 | use ExUnit.Case 11 | 12 | test "the truth" do 13 | 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/otp-supervisor/2/sequence/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/project/0/issues/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/project/0/issues/lib/issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues do 10 | use Application 11 | 12 | def start(_type, _args) do 13 | 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/project/0/issues/mix.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dwyl/learn-elixir/e7c3df9a12c31052821577a9b2caac03555e760e/examples/project/0/issues/mix.lock -------------------------------------------------------------------------------- /examples/project/0/issues/test/cli_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule CliTest do 10 | use ExUnit.Case 11 | 12 | test "nil returned by option parsing with -h and --help options" do 13 | assert Issues.CLI.parse_args(["-h", "anything"]) == :help 14 | assert Issues.CLI.parse_args(["--help", "anything"]) == :help 15 | end 16 | 17 | test "three values returned if three given" do 18 | assert Issues.CLI.parse_args(["user", "project", "99"]) == { "user", "project", 99 } 19 | end 20 | 21 | test "count is defaulted if two values given" do 22 | assert Issues.CLI.parse_args(["user", "project"]) == { "user", "project", 4 } 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /examples/project/0/issues/test/issues_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule IssuesTest do 10 | use ExUnit.Case 11 | doctest Issues 12 | 13 | test "the truth" do 14 | assert 1 + 1 == 2 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /examples/project/0/issues/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/project/1/issues/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/project/1/issues/lib/issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues do 10 | end 11 | -------------------------------------------------------------------------------- /examples/project/1/issues/lib/issues/github_issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues.GithubIssues do 10 | @user_agent [ {"User-agent", "Elixir dave@pragprog.com"} ] 11 | 12 | def fetch(user, project) do 13 | issues_url(user, project) 14 | |> HTTPoison.get(@user_agent) 15 | |> handle_response 16 | end 17 | 18 | def issues_url(user, project) do 19 | "https://api.github.com/repos/#{user}/#{project}/issues" 20 | end 21 | 22 | def handle_response({ :ok, %{status_code: 200, body: body}}) do 23 | { :ok, body } 24 | end 25 | 26 | def handle_response({ _, %{status_code: _, body: body}}) do 27 | { :error, body } 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /examples/project/1/issues/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :issues, 15 | version: "0.0.1", 16 | build_embedded: Mix.env == :prod, 17 | start_permanent: Mix.env == :prod, 18 | deps: deps() 19 | ] 20 | end 21 | 22 | # Configuration for the OTP application 23 | def application do 24 | [ applications: [ :logger, :httpoison ] ] 25 | end 26 | 27 | defp deps do 28 | [ 29 | { :httpoison, "~> 0.9" } 30 | ] 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /examples/project/1/issues/test/cli_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule CliTest do 10 | use ExUnit.Case 11 | doctest Issues 12 | 13 | import Issues.CLI, only: [ parse_args: 1 ] 14 | 15 | test ":help returned by option parsing with -h and --help options" do 16 | assert parse_args(["-h", "anything"]) == :help 17 | assert parse_args(["--help", "anything"]) == :help 18 | end 19 | 20 | test "three values returned if three given" do 21 | assert parse_args(["user", "project", "99"]) == { "user", "project", 99 } 22 | end 23 | 24 | test "count is defaulted if two values given" do 25 | assert parse_args(["user", "project"]) == { "user", "project", 4 } 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /examples/project/1/issues/test/issues_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule IssuesTest do 10 | use ExUnit.Case 11 | doctest Issues 12 | 13 | test "the truth" do 14 | assert 1 + 1 == 2 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /examples/project/1/issues/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/project/2/issues/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/project/2/issues/lib/issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues do 10 | end 11 | -------------------------------------------------------------------------------- /examples/project/2/issues/lib/issues/github_issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues.GithubIssues do 10 | 11 | @user_agent [ {"User-agent", "Elixir dave@pragprog.com"} ] 12 | 13 | def fetch(user, project) do 14 | issues_url(user, project) 15 | |> HTTPoison.get(@user_agent) 16 | |> handle_response 17 | end 18 | 19 | def issues_url(user, project) do 20 | "https://api.github.com/repos/#{user}/#{project}/issues" 21 | end 22 | 23 | def handle_response({ :ok, %{status_code: 200, body: body}}), do: { :ok, body } 24 | def handle_response({ _, %{status_code: _, body: body}}), do: { :error, body } 25 | 26 | end 27 | -------------------------------------------------------------------------------- /examples/project/2/issues/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :issues, 15 | version: "0.0.1", 16 | build_embedded: Mix.env == :prod, 17 | start_permanent: Mix.env == :prod, 18 | deps: deps() 19 | ] 20 | end 21 | 22 | # Configuration for the OTP application 23 | def application do 24 | [ 25 | applications: [ :logger, :httpoison ] 26 | ] 27 | end 28 | 29 | defp deps do 30 | [ 31 | httpoison: "~> 0.9", 32 | poison: "~> 2.2" 33 | ] 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /examples/project/2/issues/test/cli_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule CliTest do 10 | use ExUnit.Case 11 | 12 | import Issues.CLI, only: [ parse_args: 1 ] 13 | 14 | test ":help returned by option parsing with -h and --help options" do 15 | assert parse_args(["-h", "anything"]) == :help 16 | assert parse_args(["--help", "anything"]) == :help 17 | end 18 | 19 | test "three values returned if three given" do 20 | assert parse_args(["user", "project", "99"]) == { "user", "project", 99 } 21 | end 22 | 23 | test "count is defaulted if two values given" do 24 | assert parse_args(["user", "project"]) == { "user", "project", 4 } 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /examples/project/2/issues/test/issues_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule IssuesTest do 10 | use ExUnit.Case 11 | 12 | end 13 | -------------------------------------------------------------------------------- /examples/project/2/issues/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/project/3/issues/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/project/3/issues/config/config.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | use Mix.Config 10 | config :issues, github_url: "https://api.github.com" 11 | -------------------------------------------------------------------------------- /examples/project/3/issues/lib/issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues do 10 | def start(_type, _args) do 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /examples/project/3/issues/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :issues, 15 | version: "0.0.1", 16 | build_embedded: Mix.env == :prod, 17 | start_permanent: Mix.env == :prod, 18 | deps: deps() 19 | ] 20 | end 21 | 22 | # Configuration for the OTP application 23 | def application do 24 | [ 25 | applications: [ :logger, :httpoison ] 26 | ] 27 | end 28 | 29 | defp deps do 30 | [ 31 | httpoison: "~> 0.9", 32 | poison: "~> 2.2" 33 | ] 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /examples/project/3/issues/test/issues_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule IssuesTest do 10 | use ExUnit.Case 11 | 12 | end 13 | -------------------------------------------------------------------------------- /examples/project/3/issues/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/project/4/issues/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/project/4/issues/config/config.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | use Mix.Config 10 | 11 | config :issues, github_url: "https://api.github.com" 12 | -------------------------------------------------------------------------------- /examples/project/4/issues/lib/issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues do 10 | def start(_type, _args) do 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /examples/project/4/issues/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ 14 | app: :issues, 15 | escript: escript_config, 16 | version: "0.0.1", 17 | build_embedded: Mix.env == :prod, 18 | start_permanent: Mix.env == :prod, 19 | deps: deps() 20 | ] 21 | end 22 | 23 | # Configuration for the OTP application 24 | def application do 25 | [ 26 | applications: [ :logger, :httpoison ] 27 | ] 28 | end 29 | 30 | defp deps do 31 | [ 32 | httpoison: "~> 0.9", 33 | poison: "~> 2.2" 34 | ] 35 | end 36 | 37 | defp escript_config do 38 | [ main_module: Issues.CLI ] 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /examples/project/4/issues/test/issues_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule IssuesTest do 10 | use ExUnit.Case 11 | 12 | end 13 | -------------------------------------------------------------------------------- /examples/project/4/issues/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/project/5/issues/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/project/5/issues/config/config.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | use Mix.Config 10 | config :issues, github_url: "https://api.github.com" 11 | config :logger, compile_time_purge_level: :info 12 | -------------------------------------------------------------------------------- /examples/project/5/issues/lib/issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues do 10 | def start(_type, _args) do 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /examples/project/5/issues/test/doc_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule DocTest do 10 | use ExUnit.Case 11 | doctest Issues.TableFormatter 12 | end 13 | -------------------------------------------------------------------------------- /examples/project/5/issues/test/issues_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule IssuesTest do 10 | use ExUnit.Case 11 | 12 | 13 | end 14 | -------------------------------------------------------------------------------- /examples/project/5/issues/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/protocols/basic.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Blob do 10 | defstruct content: nil 11 | end -------------------------------------------------------------------------------- /examples/protocols/bitmap.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Bitmap do 10 | defstruct value: 0 11 | 12 | @doc """ 13 | A simple accessor for the 2^bit value in an integer 14 | 15 | iex> b = %Bitmap{value: 5} 16 | %Bitmap{value: 5} 17 | iex> Bitmap.fetch_bit(b,2) 18 | 1 19 | iex> Bitmap.fetch_bit(b,1) 20 | 0 21 | iex> Bitmap.fetch_bit(b,0) 22 | 1 23 | """ 24 | def fetch_bit(%Bitmap{value: value}, bit) do 25 | use Bitwise 26 | 27 | (value >>> bit) &&& 1 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /examples/protocols/bitmap_access.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Bitmap do 10 | defstruct value: 0 11 | 12 | defimpl Access do 13 | use Bitwise 14 | def get(%Bitmap{value: value}, bit) do 15 | if (value &&& (1 <<< bit)) == 0, do: 0, else: 1 16 | end 17 | def get_and_update(bitmap = %Bitmap{value: value}, bit, accessor_fn) do 18 | old_value = get(bitmap, bit) 19 | new_value = accessor_fn.(old_value) 20 | value = (value &&& bnot( 1 <<< bit )) ||| (new_value <<< bit) 21 | %Bitmap{value: value} 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /examples/protocols/bitmap_collectable.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defimpl Collectable, for: Bitmap do 10 | use Bitwise 11 | 12 | def into(%Bitmap{value: target}) do 13 | {target, fn 14 | acc, {:cont, next_bit} -> (acc <<< 1) ||| next_bit 15 | acc, :done -> %Bitmap{value: acc} 16 | _, :halt -> :ok 17 | end} 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /examples/protocols/bitmap_inspect.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Bitmap do 10 | defstruct value: 0 11 | 12 | defimpl Inspect do 13 | def inspect(%Bitmap{value: value}, _opts) do 14 | "%Bitmap{#{value}=#{as_binary(value)}}" 15 | end 16 | defp as_binary(value) do 17 | to_string(:io_lib.format("~.2B", [value])) 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /examples/protocols/bitmap_string.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defimpl String.Chars, for: Bitmap do 10 | def to_string(bitmap) do 11 | import Enum 12 | bitmap 13 | |> reverse 14 | |> chunk(3) 15 | |> map(fn three_bits -> three_bits |> reverse |> join end) 16 | |> reverse 17 | |> join("_") 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /examples/protocols/collect.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defimpl Collectable, for: Integer do 10 | def into(original) do 11 | {[], fn 12 | list, {:cont, x} -> [x|list] 13 | list, :done -> original ++ :lists.reverse(list) 14 | _, :halt -> :ok 15 | end} 16 | end 17 | end 18 | 19 | IO.inspect Enum.into(3, []) 20 | -------------------------------------------------------------------------------- /examples/protocols/enumerable.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defrecord MyList, list: [] 10 | 11 | defimpl Enumerable, for: MyList do 12 | 13 | def reduce(MyList[list: list], acc, fun), do: _reduce(list, acc, fun) 14 | 15 | def _reduce([h|t], acc, fun) do 16 | _reduce(t, fun.(h, acc), fun) 17 | end 18 | 19 | def _reduce([], acc, _fun) do 20 | acc 21 | end 22 | 23 | 24 | def member?(collection, value) do 25 | Enum.member?(collection.list, value) 26 | end 27 | 28 | def count(collection) do 29 | length(collection.list) 30 | end 31 | 32 | end 33 | 34 | my_list = MyList.new list: [1, 2, 3] 35 | 36 | IO.puts(Enum.count my_list) 37 | IO.puts(Enum.member? my_list, 3) 38 | Enum.each my_list, IO.puts(&1) 39 | -------------------------------------------------------------------------------- /examples/protocols/is_collection.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defprotocol Collection do 10 | @fallback_to_any true 11 | def is_collection?(value) 12 | end 13 | 14 | defimpl Collection, for: [List, Tuple, BitString, Map] do 15 | def is_collection?(_), do: true 16 | end 17 | 18 | defimpl Collection, for: Any do 19 | def is_collection?(_), do: false 20 | end 21 | 22 | Enum.each [ 1, 1.0, [1,2], {1,2}, %{}, "cat" ], fn value -> 23 | IO.puts "#{inspect value}: #{Collection.is_collection?(value)}" 24 | end 25 | -------------------------------------------------------------------------------- /examples/protocols/pid.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defimpl Inspect, for: PID do 10 | def inspect(pid, _opts) do 11 | "#Process" <> String.from_char_list!(pid_to_list(pid)) <> "!!" 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /examples/protocols/range.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | @doc 10 | @only 11 | @except 12 | @record_fields 13 | @spec 14 | @record_optimizable 15 | @type 16 | @opaque 17 | @callback 18 | -------------------------------------------------------------------------------- /examples/records/person.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defrecord Person, name: "", age: 0 do 10 | def say_hello(greeting, record) do 11 | IO.puts "#{greeting}, #{record.name}" 12 | end 13 | end 14 | 15 | m = Person.new name: "Methuselah", age: 969 16 | 17 | m.say_hello "You look good for your age" 18 | -------------------------------------------------------------------------------- /examples/records/person1.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defrecord Person, name: "", age: 0 do 10 | def say_hello(record, greeting) do 11 | IO.puts "#{greeting}, #{record.name}" 12 | end 13 | end 14 | 15 | m = Person.new name: "Methuselah", age: 969 16 | 17 | Person.say_hello m, "You look good for your age" 18 | -------------------------------------------------------------------------------- /examples/records/person2.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defrecord Person, name: "", age: 0 10 | 11 | defmodule Friendly do 12 | def say_hello(record, greeting) do 13 | IO.puts "#{greeting}, #{record.name}" 14 | end 15 | end 16 | 17 | m = Person.new name: "Methuselah", age: 969 18 | 19 | Friendly.say_hello m, "You look good for your age" 20 | -------------------------------------------------------------------------------- /examples/records/play.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defrecord Play, name: "", age: 0 do 10 | def say_name(a,b,r) do 11 | IO.puts a 12 | IO.puts b 13 | IO.puts r.name 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/spawn/fact_tr.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule TailRecursive do 10 | def factorial(n), do: _fact(n, 1) 11 | defp _fact(0, acc), do: acc 12 | defp _fact(n, acc), do: _fact(n-1, acc*n) 13 | end 14 | -------------------------------------------------------------------------------- /examples/spawn/fib_agent.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule FibAgent do 10 | def start_link do 11 | Agent.start_link(fn -> %{ 0 => 0, 1 => 1 } end) 12 | end 13 | 14 | def fib(pid, n) when n >= 0 do 15 | Agent.get_and_update(pid, &do_fib(&1, n)) 16 | end 17 | 18 | defp do_fib(cache, n) do 19 | case cache[n] do 20 | nil -> 21 | { n_1, cache } = do_fib(cache, n-1) 22 | result = n_1 + cache[n-2] 23 | { result, Map.put(cache, n, result) } 24 | 25 | cached_value -> 26 | { cached_value , cache } 27 | end 28 | end 29 | 30 | end 31 | 32 | {:ok, agent} = FibAgent.start_link() 33 | IO.puts FibAgent.fib(agent, 2000) 34 | -------------------------------------------------------------------------------- /examples/spawn/fib_tr.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Fibonacci do 10 | 11 | def of(n), do: _fib(n, 0, 1) 12 | 13 | def _fib(0, result, _next), do: result 14 | def _fib(n, result, next), do: _fib(n-1, next, result+next) 15 | 16 | end -------------------------------------------------------------------------------- /examples/spawn/link1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Link1 do 10 | import :timer, only: [ sleep: 1 ] 11 | 12 | def sad_function do 13 | sleep 500 14 | exit(:boom) 15 | end 16 | def run do 17 | spawn(Link1, :sad_function, []) 18 | receive do 19 | msg -> 20 | IO.puts "MESSAGE RECEIVED: #{inspect msg}" 21 | after 1000 -> 22 | IO.puts "Nothing happened as far as I am concerned" 23 | end 24 | end 25 | end 26 | 27 | Link1.run 28 | 29 | -------------------------------------------------------------------------------- /examples/spawn/link2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Link2 do 10 | import :timer, only: [ sleep: 1 ] 11 | 12 | def sad_function do 13 | sleep 500 14 | exit(:boom) 15 | end 16 | def run do 17 | spawn_link(Link2, :sad_function, []) 18 | receive do 19 | msg -> 20 | IO.puts "MESSAGE RECEIVED: #{inspect msg}" 21 | after 1000 -> 22 | IO.puts "Nothing happened as far as I am concerned" 23 | end 24 | end 25 | end 26 | 27 | Link2.run 28 | 29 | -------------------------------------------------------------------------------- /examples/spawn/link3.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Link3 do 10 | import :timer, only: [ sleep: 1 ] 11 | 12 | def sad_function do 13 | sleep 500 14 | exit(:boom) 15 | end 16 | def run do 17 | Process.flag(:trap_exit, true) 18 | spawn_link(Link3, :sad_function, []) 19 | receive do 20 | msg -> 21 | IO.puts "MESSAGE RECEIVED: #{inspect msg}" 22 | after 1000 -> 23 | IO.puts "Nothing happened as far as I am concerned" 24 | end 25 | end 26 | end 27 | 28 | Link3.run 29 | 30 | -------------------------------------------------------------------------------- /examples/spawn/monitor1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Monitor1 do 10 | import :timer, only: [ sleep: 1 ] 11 | 12 | def sad_function do 13 | sleep 500 14 | exit(:boom) 15 | end 16 | def run do 17 | res = spawn_monitor(Monitor1, :sad_function, []) 18 | IO.puts inspect res 19 | receive do 20 | msg -> 21 | IO.puts "MESSAGE RECEIVED: #{inspect msg}" 22 | after 1000 -> 23 | IO.puts "Nothing happened as far as I am concerned" 24 | end 25 | end 26 | end 27 | 28 | Monitor1.run 29 | -------------------------------------------------------------------------------- /examples/spawn/pmap.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Parallel do 10 | def pmap(collection, fun) do 11 | me = self 12 | collection 13 | |> Enum.map(fn (elem) -> 14 | spawn_link fn -> (send me, { self, fun.(elem) }) end 15 | end) 16 | |> Enum.map(fn (pid) -> 17 | receive do { ^pid, result } -> result end 18 | end) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /examples/spawn/pmap1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Parallel do 10 | def pmap(collection, func) do 11 | collection 12 | |> Enum.map(&(Task.async(fn -> func.(&1) end))) 13 | |> Enum.map(&Task.await/1) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/spawn/spawn-basic.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule SpawnBasic do 10 | def greet do 11 | IO.puts "Hello" 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /examples/spawn/spawn1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Spawn1 do 10 | def greet do 11 | receive do 12 | {sender, msg} -> 13 | send sender, { :ok, "Hello, #{msg}" } 14 | end 15 | end 16 | end 17 | 18 | # here's a client 19 | pid = spawn(Spawn1, :greet, []) 20 | send pid, {self, "World!"} 21 | 22 | receive do 23 | {:ok, message} -> 24 | IO.puts message 25 | end 26 | 27 | -------------------------------------------------------------------------------- /examples/spawn/spawn2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Spawn2 do 10 | def greet do 11 | receive do 12 | {sender, msg} -> 13 | send sender, { :ok, "Hello, #{msg}" } 14 | end 15 | end 16 | end 17 | 18 | # here's a client 19 | pid = spawn(Spawn2, :greet, []) 20 | 21 | send pid, {self, "World!"} 22 | 23 | receive do 24 | {:ok, message} -> 25 | IO.puts message 26 | end 27 | 28 | send pid, {self, "Kermit!"} 29 | receive do 30 | {:ok, message} -> 31 | IO.puts message 32 | end 33 | 34 | -------------------------------------------------------------------------------- /examples/spawn/spawn3.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Spawn3 do 10 | def greet do 11 | receive do 12 | {sender, msg} -> 13 | send sender, { :ok, "Hello, #{msg}" } 14 | end 15 | end 16 | end 17 | 18 | # here's a client 19 | pid = spawn(Spawn3, :greet, []) 20 | 21 | send pid, {self, "World!"} 22 | receive do 23 | {:ok, message} -> 24 | IO.puts message 25 | end 26 | 27 | send pid, {self, "Kermit!"} 28 | receive do 29 | {:ok, message} -> 30 | IO.puts message 31 | after 500 -> 32 | IO.puts "The greeter has gone away" 33 | end 34 | 35 | -------------------------------------------------------------------------------- /examples/spawn/spawn4.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Spawn4 do 10 | def greet do 11 | receive do 12 | {sender, msg} -> 13 | send sender, { :ok, "Hello, #{msg}" } 14 | greet 15 | end 16 | end 17 | end 18 | 19 | # here's a client 20 | pid = spawn(Spawn4, :greet, []) 21 | send pid, {self, "World!"} 22 | receive do 23 | {:ok, message} -> 24 | IO.puts message 25 | end 26 | 27 | send pid, {self, "Kermit!"} 28 | receive do 29 | {:ok, message} -> 30 | IO.puts message 31 | after 500 -> 32 | IO.puts "The greeter has gone away" 33 | end 34 | 35 | -------------------------------------------------------------------------------- /examples/strings/nextcodepoint.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule MyString do 10 | def each(str, func), do: _each(String.next_codepoint(str), func) 11 | 12 | defp _each({codepoint, rest}, func) do 13 | func.(codepoint) 14 | _each(String.next_codepoint(rest), func) 15 | end 16 | 17 | defp _each(nil, _), do: [] 18 | end 19 | 20 | MyString.each "∂og", fn c -> IO.puts c end 21 | 22 | -------------------------------------------------------------------------------- /examples/strings/parse.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Parse do 10 | 11 | def number([ ?- | tail ]), do: _number_digits(tail, 0) * -1 12 | def number([ ?+ | tail ]), do: _number_digits(tail, 0) 13 | def number(str), do: _number_digits(str, 0) 14 | 15 | defp _number_digits([], value), do: value 16 | defp _number_digits([ digit | tail ], value) 17 | when digit in '0123456789' do 18 | _number_digits(tail, value*10 + digit - ?0) 19 | end 20 | defp _number_digits([ non_digit | _ ], _) do 21 | raise "Invalid digit '#{[non_digit]}'" 22 | end 23 | end -------------------------------------------------------------------------------- /examples/strings/utf-iterate.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Utf8 do 10 | def each(str, func) when is_binary(str), do: _each(str, func) 11 | 12 | defp _each(<< head :: utf8, tail :: binary >>, func) do 13 | func.(head) 14 | _each(tail, func) 15 | end 16 | 17 | defp _each(<<>>, _func), do: [] 18 | end 19 | 20 | Utf8.each "∂og", fn char -> IO.puts char end -------------------------------------------------------------------------------- /examples/tasks/agent_dict.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Frequency do 10 | 11 | def start_link do 12 | Agent.start_link(fn -> %{} end, name: __MODULE__) 13 | end 14 | 15 | def add_word(word) do 16 | Agent.update(__MODULE__, 17 | fn map -> 18 | Map.update(map, word, 1, &(&1+1)) 19 | end) 20 | end 21 | 22 | def count_for(word) do 23 | Agent.get(__MODULE__, fn map -> map[word] end) 24 | end 25 | 26 | def words do 27 | Agent.get(__MODULE__, fn map -> Map.keys(map) end) 28 | end 29 | 30 | end 31 | -------------------------------------------------------------------------------- /examples/tasks/fib.rb: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | @count = 0 10 | def fib(n) 11 | @count += 1 12 | puts "fib(#{n})" 13 | if n < 2 14 | n 15 | else 16 | fib(n-1) + fib(n-2) 17 | end 18 | end 19 | 20 | (5..5).each do |i| 21 | @count = 0 22 | result = fib(i) 23 | puts @count 24 | # puts sprintf("%2d %-10d %s", i, @count, result) 25 | end 26 | -------------------------------------------------------------------------------- /examples/tasks/tasks1.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Fib do 10 | def of(0), do: 0 11 | def of(1), do: 1 12 | def of(n), do: Fib.of(n-1) + Fib.of(n-2) 13 | end 14 | 15 | IO.puts "Start the task" 16 | worker = Task.async(fn -> Fib.of(20) end) 17 | IO.puts "Do something else" 18 | # ... 19 | IO.puts "Wait for the task" 20 | result = Task.await(worker) 21 | 22 | IO.puts "The result is #{result}" -------------------------------------------------------------------------------- /examples/tasks/tasks2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Fib do 10 | def of(0), do: 0 11 | def of(1), do: 1 12 | def of(n), do: Fib.of(n-1) + Fib.of(n-1) 13 | end 14 | 15 | #HSTART:body 16 | worker = Task.async(Fib, :of, [20]) 17 | result = Task.await(worker) 18 | IO.puts "The result is #{result}" 19 | -------------------------------------------------------------------------------- /examples/tasks/words/list1: -------------------------------------------------------------------------------- 1 | angor 2 | argon 3 | caret 4 | carte 5 | cater 6 | crate 7 | creat 8 | creta 9 | 10 | -------------------------------------------------------------------------------- /examples/tasks/words/list2: -------------------------------------------------------------------------------- 1 | ester 2 | estre 3 | goran 4 | grano 5 | groan 6 | leapt 7 | nagor 8 | orang 9 | 10 | 11 | -------------------------------------------------------------------------------- /examples/tasks/words/list3: -------------------------------------------------------------------------------- 1 | palet 2 | patel 3 | pelta 4 | petal 5 | pleat 6 | react 7 | recta 8 | reest 9 | -------------------------------------------------------------------------------- /examples/tasks/words/list4: -------------------------------------------------------------------------------- 1 | rogan 2 | ronga 3 | steer 4 | stere 5 | stree 6 | terse 7 | tsere 8 | tepal 9 | -------------------------------------------------------------------------------- /examples/tooling/issues/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | -------------------------------------------------------------------------------- /examples/tooling/issues/config/config.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | use Mix.Config 10 | config :issues, github_url: "https://api.github.com" 11 | config :logger, compile_time_purge_level: :info 12 | -------------------------------------------------------------------------------- /examples/tooling/issues/issues: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dwyl/learn-elixir/e7c3df9a12c31052821577a9b2caac03555e760e/examples/tooling/issues/issues -------------------------------------------------------------------------------- /examples/tooling/issues/lib/issues.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Issues do 10 | def start(_type, _args) do 11 | end 12 | 13 | end 14 | -------------------------------------------------------------------------------- /examples/tooling/issues/test/doc_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule DocTest do 10 | use ExUnit.Case 11 | doctest Issues.TableFormatter 12 | end 13 | -------------------------------------------------------------------------------- /examples/tooling/issues/test/issues_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule IssuesTest do 10 | use ExUnit.Case 11 | 12 | 13 | end 14 | -------------------------------------------------------------------------------- /examples/tooling/issues/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | -------------------------------------------------------------------------------- /examples/tooling/pbt/.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc 12 | 13 | # If the VM crashes, it generates a dump, let's ignore it too. 14 | erl_crash.dump 15 | 16 | # Also ignore archive artifacts (built via "mix archive.build"). 17 | *.ez 18 | -------------------------------------------------------------------------------- /examples/tooling/pbt/lib/pbt.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Pbt do 10 | end 11 | -------------------------------------------------------------------------------- /examples/tooling/pbt/lib/stats.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Stats do 10 | def sum(vals), do: vals |> Enum.reduce(0, &+/2) 11 | def count(vals), do: vals |> length 12 | def average(vals), do: sum(vals) / count(vals) 13 | end 14 | 15 | -------------------------------------------------------------------------------- /examples/tooling/pbt/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Pbt.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [app: :pbt, 14 | version: "0.1.0", 15 | elixir: "~> 1.3", 16 | build_embedded: Mix.env == :prod, 17 | start_permanent: Mix.env == :prod, 18 | deps: deps()] 19 | end 20 | 21 | def application do 22 | [applications: [:logger]] 23 | end 24 | 25 | defp deps do 26 | [ 27 | { :triq, github: "krestenkrab/triq", only: :test}, 28 | { :excheck, "~> 0.4.0", only: :test }, 29 | ] 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /examples/tooling/pbt/test/pbt_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule PbtTest do 10 | use ExUnit.Case 11 | use ExCheck 12 | doctest Pbt 13 | 14 | def average(list) do 15 | IO.inspect list 16 | with sum = Enum.reduce(list, 0, &+/2), 17 | count = length(list) 18 | do 19 | IO.inspect { count, sum } 20 | sum/count 21 | end 22 | end 23 | 24 | 25 | property :a do 26 | for_all a in [int,int], do: average(a) >= -7 27 | end 28 | 29 | # property :average do 30 | # for_all l in such_that(l in list(int) when length(l) > 0), do: average(l) > 0 31 | # end 32 | end 33 | 34 | IO.inspect PbtTest.average([1,2,3]) 35 | -------------------------------------------------------------------------------- /examples/tooling/pbt/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start() 10 | ExCheck.start() 11 | -------------------------------------------------------------------------------- /examples/tooling/webapp/.gitignore: -------------------------------------------------------------------------------- 1 | # App artifacts 2 | /_build 3 | /db 4 | /deps 5 | /*.ez 6 | 7 | # Generated on crash by the VM 8 | erl_crash.dump 9 | 10 | # Static artifacts 11 | /node_modules 12 | 13 | # Since we are building assets from web/static, 14 | # we ignore priv/static. You may want to comment 15 | # this depending on your deployment strategy. 16 | /priv/static/ 17 | 18 | # The config/prod.secret.exs file by default contains sensitive 19 | # data and you should not commit it into version control. 20 | # 21 | # Alternatively, you may comment the line below and commit the 22 | # secrets file as long as you replace its contents by environment 23 | # variables. 24 | /config/prod.secret.exs 25 | -------------------------------------------------------------------------------- /examples/tooling/webapp/config/test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | use Mix.Config 10 | 11 | # We don't run a server during test. If one is required, 12 | # you can enable the server option below. 13 | config :webapp, Webapp.Endpoint, 14 | http: [port: 4001], 15 | server: false 16 | 17 | # Print only warnings and errors during test 18 | config :logger, level: :warn 19 | 20 | # Configure your database 21 | config :webapp, Webapp.Repo, 22 | adapter: Ecto.Adapters.Postgres, 23 | username: "postgres", 24 | password: "postgres", 25 | database: "webapp_test", 26 | hostname: "localhost", 27 | pool: Ecto.Adapters.SQL.Sandbox 28 | -------------------------------------------------------------------------------- /examples/tooling/webapp/lib/webapp/repo.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Webapp.Repo do 10 | use Ecto.Repo, otp_app: :webapp 11 | end 12 | -------------------------------------------------------------------------------- /examples/tooling/webapp/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "repository": {}, 3 | "license": "MIT", 4 | "scripts": { 5 | "deploy": "brunch build --production", 6 | "watch": "brunch watch --stdin" 7 | }, 8 | "dependencies": { 9 | "phoenix": "file:deps/phoenix", 10 | "phoenix_html": "file:deps/phoenix_html" 11 | }, 12 | "devDependencies": { 13 | "babel-brunch": "~6.0.0", 14 | "brunch": "2.7.4", 15 | "clean-css-brunch": "~2.0.0", 16 | "css-brunch": "~2.0.0", 17 | "javascript-brunch": "~2.0.0", 18 | "uglify-js-brunch": "~2.0.1" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /examples/tooling/webapp/priv/repo/seeds.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | # Script for populating the database. You can run it as: 10 | # 11 | # mix run priv/repo/seeds.exs 12 | # 13 | # Inside the script, you can read and write to any of your 14 | # repositories directly: 15 | # 16 | # Webapp.Repo.insert!(%Webapp.SomeModel{}) 17 | # 18 | # We recommend using the bang functions (`insert!`, `update!` 19 | # and so on) as they will fail if something goes wrong. 20 | -------------------------------------------------------------------------------- /examples/tooling/webapp/test/controllers/page_controller_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Webapp.PageControllerTest do 10 | use Webapp.ConnCase 11 | 12 | test "GET /", %{conn: conn} do 13 | conn = get conn, "/" 14 | assert html_response(conn, 200) =~ "Welcome to Phoenix!" 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /examples/tooling/webapp/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | ExUnit.start 10 | 11 | Ecto.Adapters.SQL.Sandbox.mode(Webapp.Repo, :manual) 12 | 13 | -------------------------------------------------------------------------------- /examples/tooling/webapp/test/views/error_view_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Webapp.ErrorViewTest do 10 | use Webapp.ConnCase, async: true 11 | 12 | # Bring render/3 and render_to_string/3 for testing custom views 13 | import Phoenix.View 14 | 15 | test "renders 404.html" do 16 | assert render_to_string(Webapp.ErrorView, "404.html", []) == 17 | "Page not found" 18 | end 19 | 20 | test "render 500.html" do 21 | assert render_to_string(Webapp.ErrorView, "500.html", []) == 22 | "Internal server error" 23 | end 24 | 25 | test "render any other" do 26 | assert render_to_string(Webapp.ErrorView, "505.html", []) == 27 | "Internal server error" 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /examples/tooling/webapp/test/views/layout_view_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Webapp.LayoutViewTest do 10 | use Webapp.ConnCase, async: true 11 | end 12 | -------------------------------------------------------------------------------- /examples/tooling/webapp/test/views/page_view_test.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Webapp.PageViewTest do 10 | use Webapp.ConnCase, async: true 11 | end 12 | -------------------------------------------------------------------------------- /examples/tooling/webapp/web/controllers/page_controller.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Webapp.PageController do 10 | use Webapp.Web, :controller 11 | 12 | def index(conn, _params) do 13 | render conn, "index.html" 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/tooling/webapp/web/router.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Programming Elixir 1.3", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/elixir13 for more book information. 8 | #--- 9 | defmodule Webapp.Router do 10 | use Webapp.Web, :router 11 | 12 | pipeline :browser do 13 | plug :accepts, ["html"] 14 | plug :fetch_session 15 | plug :fetch_flash 16 | plug :protect_from_forgery 17 | plug :put_secure_browser_headers 18 | end 19 | 20 | pipeline :api do 21 | plug :accepts, ["json"] 22 | end 23 | 24 | scope "/", Webapp do 25 | pipe_through :browser # Use the default browser stack 26 | 27 | get "/", PageController, :index 28 | end 29 | 30 | # Other scopes may use custom stacks. 31 | # scope "/api", Webapp do 32 | # pipe_through :api 33 | # end 34 | end 35 | -------------------------------------------------------------------------------- /examples/tooling/webapp/web/static/assets/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dwyl/learn-elixir/e7c3df9a12c31052821577a9b2caac03555e760e/examples/tooling/webapp/web/static/assets/favicon.ico -------------------------------------------------------------------------------- /examples/tooling/webapp/web/static/assets/images/phoenix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dwyl/learn-elixir/e7c3df9a12c31052821577a9b2caac03555e760e/examples/tooling/webapp/web/static/assets/images/phoenix.png -------------------------------------------------------------------------------- /examples/tooling/webapp/web/static/assets/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /examples/tooling/webapp/web/static/css/app.css: -------------------------------------------------------------------------------- 1 | /* This file is for your main application css. */ -------------------------------------------------------------------------------- /examples/tooling/webapp/web/templates/page/index.html.eex: -------------------------------------------------------------------------------- 1 |
A productive web framework that
does not compromise speed and maintainability.