Last active
June 9, 2016 02:05
-
-
Save paulosuzart/b749a3c4f825eebccaaefa70a80393dd to your computer and use it in GitHub Desktop.
Simple module to randomly generate bible verses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| gn: [31, 25, 24, 26, 32, 22, 24, 22, 29, 32, 32, 20, 18, 24, 21, 16, 27, 33, 38, 18, 34, 24, 20, 67, 34, 35, 46, 22, 35, 43, 54, 33, 20, 31, 29, 43, 36, 30, 23, 23, 57, 38, 34, 34, 28, 34, 31, 22, 33, 26] | |
| ex: [22, 25, 22, 31, 23, 30, 29, 28, 35, 29, 10, 51, 22, 31, 27, 36, 16, 27, 25, 26, 37, 30, 33, 18, 40, 37, 21, 43, 46, 38, 18, 35, 23, 35, 35, 38, 29, 31, 43, 38] | |
| lv: [17, 16, 17, 35, 26, 23, 38, 36, 24, 20, 47, 8, 59, 57, 33, 34, 16, 30, 37, 27, 24, 33, 44, 23, 55, 46, 34] | |
| nm: [54, 34, 51, 49, 31, 27, 89, 26, 23, 36, 35, 16, 33, 45, 41, 35, 28, 32, 22, 29, 35, 41, 30, 25, 19, 65, 23, 31, 39, 17, 54, 42, 56, 29, 34, 13] | |
| dt: [46, 37, 29, 49, 33, 25, 26, 20, 29, 22, 32, 31, 19, 29, 23, 22, 20, 22, 21, 20, 23, 29, 26, 22, 19, 19, 26, 69, 28, 20, 30, 52, 29, 12] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Versicle.Mixfile do | |
| use Mix.Project | |
| def project do | |
| [app: :versicle, | |
| version: "0.0.1", | |
| elixir: "~> 1.2", | |
| build_embedded: Mix.env == :prod, | |
| start_permanent: Mix.env == :prod, | |
| deps: deps] | |
| end | |
| # Configuration for the OTP application | |
| # | |
| # Type "mix help compile.app" for more information | |
| def application do | |
| [applications: [:logger, :yaml_elixir]] | |
| end | |
| # Dependencies can be Hex packages: | |
| # | |
| # {:mydep, "~> 0.3.0"} | |
| # | |
| # Or git/path repositories: | |
| # | |
| # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} | |
| # | |
| # Type "mix help deps" for more examples and options | |
| defp deps do | |
| [{:csv, "~> 1.4"}, | |
| {:yaml_elixir, "~> 1.2"}, | |
| {:yamerl, github: "yakaz/yamerl"},] | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Versicle do | |
| require YamlElixir | |
| use GenServer | |
| def start(src \\ "./bible.yaml" |> Path.expand(__DIR__)) do | |
| {:ok, pid} = GenServer.start_link(Versicle, [src]) | |
| {:ok, csv_file} = GenServer.call(pid, :load) | |
| IO.puts(csv_file) | |
| {:ok, pid} | |
| end | |
| def suggest(pid) do | |
| GenServer.call(pid, :suggest) | |
| end | |
| def handle_call(:load, _from, [src]) do | |
| table = src |> YamlElixir.read_from_file | |
| keys = Map.keys(table) | |
| len = length(keys) | |
| {:reply, {:ok, src}, [src, len, keys, table]} | |
| end | |
| def handle_call(:suggest, _from, [src| [len, keys, table]]) do | |
| book_idx = :rand.uniform(len) - 1 | |
| book = Enum.at(keys, book_idx) | |
| chapters = Map.get(table, book) | |
| chapter_idx = :rand.uniform(length(chapters)) - 1 | |
| verse = :rand.uniform(chapters |> Enum.at(chapter_idx)) - 1 | |
| {:reply, "#{book}#{chapter_idx}-#{verse}", [src, len, keys, table]} | |
| end | |
| end | |
| # Example: | |
| # {:ok, pid} = Versicle.start() | |
| # Versicle.suggest(pid) | |
| # > ex1-18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment