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
| # Copied from https://mudssrali.com/blog/slugify-a-string-in-elixir | |
| # and then modified to drop leading and trailing dashes. | |
| defmodule String do | |
| # 1. Convert a string to downcase | |
| # 2. Remove any leading and trailing spaces | |
| # 3. Convert graphemes to normal characters (e.g. á, é) | |
| # 4. Replace non-alphanumeric characters with an empty string | |
| # 5. Replace multiple spaces or dashes with a single dash | |
| # 6. Remove any leading and trailing dashes |
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
| # Trans 1.0 did not perform safety checks when building queries. | |
| # Adding conditions on a non-existing field would result un runtime errors. | |
| iex> Article | |
| ...> |> Article.with_translation(:es, :fake, "this field does not exist") | |
| ...> |> Repo.all | |
| ** (ArgumentError) The field `fake` is not declared as translatable |
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
| # Using tab to autocomplete shows the functions added by Trans | |
| # Those functions pollute the module interface and add database | |
| # and query concerns to the schema module | |
| iex(5)> Article. | |
| changeset/1 changeset/2 with_translation/4 | |
| with_translation/5 with_translations/2 with_translations/3 |
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
| # Fetch articles titled "La République" in French | |
| iex> Article | |
| ...> |> Trans.QueryBuilder.with_translation(:fr, :title, "La République") | |
| ...> |> Repo.all | |
| # SELECT a0."id", a0."title", a0."body", a0."translations" | |
| # FROM "articles" AS a0 | |
| # WHERE (a0."translations"->"fr"->>"title" = "La République") |
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
| # Fetch Articles titled "Trans" in French and having an English comment | |
| # containing the word "good" | |
| iex> Repo.all(from a in Article, | |
| ...> join: c in assoc(a, :comments), | |
| ...> where: translated(Article, a.title, :fr) == "Trans", | |
| ...> where: ilike(translated(Comment, c.comment, :en), "good")) | |
| # SELECT a0."id", a0."title", a0."body", a0."translations" | |
| # FROM "articles" AS a0 | |
| # INNER JOIN "comments" AS c1 ON a0."id" = c1."article_id" | |
| # WHERE ((a0."translations"->"fr"->>"title") = "Trans") |
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
| iex(1)> require Anaphora | |
| Anaphora | |
| iex(2)> Anaphora.if Universe.response_to_everything do | |
| ...(2)> # La variable 'it' se crea automaticamente durante la | |
| ...(2)> # macro expansión. | |
| ...(2)> Universe.proclaim it | |
| ...(2)> end | |
| "Whoa! The data is 42" |
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 Universe do | |
| def response_to_everything do | |
| :timer.sleep(5000) # Simulate complex calculations for 5 seconds | |
| 42 | |
| end | |
| def proclaim(data), do: "Whoa! The data is #{data}" | |
| end | |
| defmodule Anaphora do |
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 Universe do | |
| def response_to_everything do | |
| :timer.sleep(5000) # Simulate complex calculations for 5 seconds | |
| 42 | |
| end | |
| def proclaim(data), do: "Whoa! The data is #{data}" | |
| end | |
| with result <- Universe.response_to_everything do |
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 Universe do | |
| def response_to_everything do | |
| :timer.sleep(5000) # Simulate complex calculations for 5 seconds | |
| 42 | |
| end | |
| def proclaim(data), do: "Whoa! The data is #{data}" | |
| 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 Fibo do | |
| def recursive(n) when n <= 2, do: 1 | |
| def recursive(n), do: recursive(n-1) + recursive(n-2) | |
| def tail_rec(n, prev \\ 0, current \\ 1) | |
| def tail_rec(0, prev, _), do: prev | |
| def tail_rec(n, prev, current), do: tail_rec(n - 1, current, current + prev) | |
| end | |
| defmodule Benchmark do |