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
| # Elixir has lazily evaluated enumerable objects that allow you | |
| # to work with enumerable objects like lists either only as needed | |
| # or infinitely. | |
| # Start up iex to play around | |
| $ iex | |
| # Typical enumeration is done eagerly where the result is computed ASAP | |
| iex> Enum.map(1..10, fn i -> i * 2 end) | |
| [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] |
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
| # The Elixir language is very extensible to allow for future additions or | |
| # third party developers to take the language in directions that the original | |
| # authors could not predict. | |
| # | |
| # Lets start with understanding what an Elixir macro is | |
| iex> quote do | |
| ...> 1 + 1 | |
| ...> end | |
| {:+, [context: Elixir, import: Kernel], [1, 1]} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The golden trinity of Erlang is the secret sauce behind what makes | |
| # Elixir a strong choice for any backend application and/or system. | |
| # https://tkowal.wordpress.com/2015/10/20/failing-fast-and-slow-in-erlang-and-elixir/ | |
| # The supervision and worker system, commonly referred to as OTP (Open | |
| # Telecom Platform, which has nothing to do with telephony), is what | |
| # gives Erlang/Elixir applications them a robust "self-healing" type | |
| # property that makes them extremely fault tolerant. |
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
| # To show hot code uploading, we first need to build a simple phoenix project so we can see it happen in real time. | |
| # Start by making a new phoenix project | |
| $ mix phoenix.new hotcode | |
| # Go into the directory | |
| $ cd hotcode | |
| # Add exrm dependency to mix.exs file | |
| {:exrm, "~> 1.0.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
| defmodule Ecto.Polymorphic do | |
| defmacro __using__(_) do | |
| quote do | |
| require Ecto.Schema | |
| import Ecto.Schema, except: [belongs_to: 2, belongs_to: 3] | |
| import unquote(__MODULE__) | |
| 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
| $ mix new policy_enforcement_playground | |
| $ cd policy_enforcement_playground | |
| # Make a new file lib/authorization.ex | |
| defmodule Authorization do | |
| def authorize_first_thing(_user, resource) do | |
| # Do some authorization here, modifying what gets returned based on the | |
| # authorization level of the user | |
| resource | |
| 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
| package main | |
| import "fmt" | |
| var allMasses []int = []int{ | |
| 147308, 51605, 71317, 110882, 92545, 126856, 104937, 92433, 107850, 119538, 82733, 52216, 105704, 123682, 105919, 136265, 100540, 84245, 72006, 111652, 85116, 85841, 71374, 144196, 125493, 113529, 64637, 87489, 138161, 120897, 53384, 83310, | |
| 120672, 126144, 107681, 101369, 77469, 141056, 140426, 114920, 124454, 130867, 64611, 104813, 138808, 114234, 148654, 59031, 91367, 83316, 106192, 127495, 139980, 119024, 149567, 57007, 61075, 65637, 75293, 61670, 104044, 77230, 80201, | |
| 137094, 99733, 50801, 68922, 148404, 79980, 62716, 67666, 72694, 81951, 108427, 111721, 55532, 94442, 88562, 101088, 111656, 111649, 92085, 91730, 114744, 59355, 55842, 100926, 146370, 147829, 62160, 91447, 115745, 141815, 106837, 68151, | |
| 89077, 60357, 89856, 75040, 139131, | |
| } |
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
| # Take all numbers from 0 to 50 million and sum every number from 0 to that number and sum those numbers | |
| :timer.tc fn -> | |
| 1..50_000_000 | |
| |> Enum.map(& Enum.sum(0..&1)) | |
| |> Enum.sum() | |
| end | |
| # Takes almost 2 minutes to run and returns the value 20,833,345,833,335,000,000 | |
| # => {108521694, 20833334583333350000000} | |
| # Using task async stream, this can be done concurrently easily by just |
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
| # machine 1 | |
| # start up with `iex --name m1@127.0.0.1 --cookie greeter` | |
| # This isn't really necessary but it won't hurt anything | |
| Node.connect :"m1@127.0.0.1" | |
| # All `Node.connect/1` calls can go to the same machine and every machine | |
| # in the cluster will automatically be connected. | |
| # machine 2 | |
| # start up with `iex --name m2@127.0.0.1 --cookie greeter` |
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
| {} + "" == "()" // true | |
| pow(2, 31).intValue + 1 > 0 // false | |
| Set(1,2,3).sameElements(Set(3,2,1)) // false | |
| Some(null).get // No exception :(, Some(null) should result in a None IMO | |
| // Option objects might be null!!! | |
| def safeOption(shouldBeSafe : Option[String]) : String = { |
NewerOlder