From e249d12ee6fdc9366e6d13b869f2ca2ccff6ee56 Mon Sep 17 00:00:00 2001 From: Ryan Plant Date: Tue, 12 Jul 2016 18:07:38 +1000 Subject: [PATCH] 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 --- elixir.html.markdown | 26 ++++++++++++++++++++++++++ ruby.html.markdown | 4 ++-- 2 files changed, 28 insertions(+), 2 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 diff --git a/ruby.html.markdown b/ruby.html.markdown index adf5ce81..a1532855 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -210,7 +210,7 @@ array.push(6) #=> [1, 2, 3, 4, 5, 6] # Check if an item exists in an array array.include?(1) #=> true -# Hashes are Ruby's primary dictionary with keys/value pairs. +# Hashes are Ruby's primary dictionary with key/value pairs. # Hashes are denoted with curly braces: hash = { 'color' => 'green', 'number' => 5 } @@ -612,7 +612,7 @@ Something.new.qux # => 'qux' - [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges. - [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Learn Ruby through a series of interactive tutorials. -- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/) +- [Official Documentation](http://ruby-doc.org/core) - [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/) - [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online. - [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.