diff options
author | Ryan Plant <ryan@ryanplant.net> | 2016-07-12 18:07:38 +1000 |
---|---|---|
committer | ven <vendethiel@hotmail.fr> | 2016-07-12 10:07:38 +0200 |
commit | e249d12ee6fdc9366e6d13b869f2ca2ccff6ee56 (patch) | |
tree | 4de15cbdae3aecdcdcff675817f6cbdf8e4250e2 /elixir.html.markdown | |
parent | b16c7ee2d8b41b5db4f713360280c284ca9b1a80 (diff) |
Elixir agents/maps, Ruby conventions/docs (#2116)
* Add name to contributors list
* Fix typo
* Note convention on curly-braced blocks
* Replace hardcoded link to 2.1.1 documentation with generic current link
* Add notes on Elixir agents
* Add explanation of maps
* Add name to contributors list
* Fix code fence that was obscuring markdown
* Fix syntax error
* Remove disputed comment
* Remove from contributors list
Diffstat (limited to 'elixir.html.markdown')
-rw-r--r-- | elixir.html.markdown | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/elixir.html.markdown b/elixir.html.markdown index eb708576..26a547c3 100644 --- a/elixir.html.markdown +++ b/elixir.html.markdown @@ -3,6 +3,7 @@ language: elixir contributors: - ["Joao Marques", "http://github.com/mrshankly"] - ["Dzianis Dashkevich", "https://github.com/dskecse"] + - ["Ryan Plant", "https://github.com/ryanplant-au"] filename: learnelixir.ex --- @@ -96,6 +97,14 @@ string. lower..upper = 1..10 # Can use pattern matching on ranges as well [lower, upper] #=> [1, 10] +# Maps are key-value pairs +genders = %{"david" => "male", "gillian" => "female"} +genders["david"] #=> "male" + +# Maps with atom keys can be used like this +genders = %{david: "male", gillian: "female"} +genders.gillian #=> "female" + ## --------------------------- ## -- Operators ## --------------------------- @@ -407,6 +416,23 @@ send pid, {:circle, 2} # The shell is also a process, you can use `self` to get the current pid self() #=> #PID<0.27.0> + +## --------------------------- +## -- Agents +## --------------------------- + +# An agent is a process that keeps track of some changing value + +# Create an agent with `Agent.start_link`, passing in a function +# The initial state of the agent will be whatever that function returns +{ok, my_agent} = Agent.start_link(fn -> ["red, green"] end) + +# `Agent.get` takes an agent name and a `fn` that gets passed the current state +# Whatever that `fn` returns is what you'll get back +Agent.get(my_agent, fn colors -> colors end) #=> ["red, "green"] + +# Update the agent's state the same way +Agent.update(my_agent, fn colors -> ["blue" | colors] end) ``` ## References |