{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/more-basics-of-elixir-and-phoenix","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"More Basics of Elixir and Phoenix\",\n  \"order\": 11,\n  \"free\": true,\n  \"active\": true,\n  \"tech\": [\"phoenix\", \"elixir\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"More Basics of Elixir and Phoenix\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Functions and modules\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Notes for Ruby developers\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Basics of Phoenix and MVC\")), mdx(\"h2\", null, \"Control Structures\"), mdx(\"p\", null, \"Elixir supports \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"if\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"unless\"), \" like other modern languages such as Ruby and CoffeeScript. If you aren't familiar with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"unless\"), \", don't worry it's just syntactic sugar for: \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"if !value\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> if true do\\n...>   \\\"truth\\\"\\n...> else\\n...>   \\\"false\\\"\\n...> end\\n\\\"truth\\\"\\n\")), mdx(\"p\", null, \"If we replace \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"if\"), \" with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"unless\"), \", we should get the opposite.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> unless true do\\n...>   \\\"true\\\"\\n...> else\\n...>   \\\"false\\\"\\n...> end\\n\\\"false\\\"\\n\")), mdx(\"p\", null, \"Elixir doesn't stop with just \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"if\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"unless\"), \", it includes two addition control structures: \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"case\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"cond\"), \". The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"case\"), \" structure is similar to a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"switch\"), \" statement in other languages but it relies heavily on pattern matching. The best way to understand the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"case\"), \" statement is to try it in IEx.\"), mdx(\"div\", null), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> case {:ok, \\\"Success\\\"} do\\n...>   {:ok, value} -> value\\n...>   _ -> \\\"No match\\\"\\n...> end\\n\\\"Success\\\"\\n\")), mdx(\"p\", null, \"You may have noticed an underscore (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"_\"), \") above. An underscore is a placeholder that accepts any value. So in the example above, if the case does not match \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"{:ok, value}\"), \", then it will pass to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"_\"), \" which will match with anything else and return \\\"No match\\\".\"), mdx(\"p\", null, \"Last but not least is \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"cond\"), \". We can use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"cond\"), \" when we want to match conditions and not values or patterns. The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"cond\"), \" structure is similar to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"else if\"), \" in other languages. An example will help us better understand it's usage.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> val = 4\\n4\\niex> cond do\\n...>   val + 2 < 4 -> \\\"val < 2\\\"\\n...>   val + 2 >= 4 -> \\\"val >= 2\\\"\\n...> end\\n\\\"val >= 2\\\"\\n\")), mdx(\"h2\", null, \"Pipes\"), mdx(\"p\", null, \"One of the nicest syntactic features of Elixir is the pipe operator, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"|>\"), \", which allows you to \\\"pipe\\\" a value from one function to another function. This is very similar to the \", mdx(\"a\", {\n    \"href\": \"https://en.wikipedia.org/wiki/Pipeline_(Unix)\",\n    \"parentName\": \"p\"\n  }, \"unix pipeline\"), \" \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"|\"), \" if you've ever used that. In its simplest form, the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"|>\"), \" takes the left value and passes it to the right side as the first argument.\"), mdx(\"p\", null, \"This is particularly useful because Elixir is just collections of functions, piping them together makes the code expressive and easy to understand at a glance. For example, if were were to make a cake using functions, we could write.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"ingredients\\n|> measure\\n|> mix\\n|> bake\\n|> decorate\\n\")), mdx(\"p\", null, \"Each step takes the result of the previous function and applies a transformation to it. This is a surprisingly powerful abstraction that makes it very easy to understand the flow of your app and makes for highly-maintainable code.\"), mdx(\"h2\", null, \"Functions\"), mdx(\"p\", null, \"Functions in Elixir, and other functional programming languages, are considered first class citizens. That means that all operations available to other types are available to them, they can be passed as arguments to other functions, assigned to variables, and returned from other functions.\"), mdx(\"p\", null, \"In Elixir there are a couple types of functions, but for this section we'll focus on anonymous functions. As the name suggests, anonymous functions have no name. If not captured in a variable, an anonymous function would otherwise not exist. To define an anonymous function we'll use the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"fn (arg) -> body end\"), \" syntax. To call an anonymous function we use the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".()\"), \" syntax on our variable, which is similar to what you find in other languages.\"), mdx(\"p\", null, \"Let's start with an example function to add two numbers together and then try using it.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> add = fn (a, b) -> a + b end\\n#Function<12.54118792/2 in :erl_eval.expr/5>\\niex> add.(1, 2)\\n3\\n\")), mdx(\"p\", null, \"Elixir also includes a helpful shorthand syntax for anonymous functions. This shorthand makes use of the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"&\"), \" operator. With the shorthand syntax our arguments are referred to as \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"&1\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"&2\"), \", and so on. We can try our previous example using the shorthand syntax.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> add = &(&1 + &2)\\n&:erlang.+/2\\niex> add.(1, 2)\\n3\\n\")), mdx(\"p\", null, \"We learned about pattern matching using the match operator (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"=\"), \") but we can also use it when specifying function signatures. Let's expand on our prior example. Let's update the function to return the string \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"\\\"zero\\\"\"), \" if someone tries to add \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"0\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"0\"), \" together:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> add = fn\\n...>         (0, 0) -> \\\"zero\\\"\\n...>         (a, b) -> a + b\\n...>       end\\n#Function<12.54118792/2 in :erl_eval.expr/5>\\niex> add.(1, 2)\\n2\\niex> add.(0, 0)\\n\\\"zero\\\"\\n\")), mdx(\"p\", null, \"That's just the start of functions in Elixir. In the next section, and later lessons, we'll cover other function types and how they all fit together.\"), mdx(\"h2\", null, \"Modules\"), mdx(\"p\", null, \"At their most basic, modules are collections of functions. With \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"defmodule\"), \" we can create a new module, add functions to it, and then re-use it elsewhere in our application. We'll learn more about modules in later lessons, but for now we'll focus on the basics.\"), mdx(\"p\", null, \"We've already covered anonymous functions so let's take a look at functions with regards to modules. In our anonymous functions we used the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"fn -> ... end\"), \" syntax, but named functions (or functions in modules) use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"def\"), \" and a lowercase name.\"), mdx(\"p\", null, \"Let's create a simple example module that can add two numbers together. We can start by using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"defmodule Example\"), \" to define our module. Then we'll need to use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"def add(a, b)\"), \" to define our function, implementing the functionality should be easy. Once we've defined everything we can use it by calling \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Example.add(1, 2)\"), \". If we put it all together we should get something like this:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> defmodule Example do\\n...>   def add(a, b) do\\n...>     a + b\\n...>   end\\n...> end\\n{:module, Example,\\n <<70, 79, 82, 49, 0, 0, 4, 248, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 157, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,\\n {:add, 2}}\\niex> Example.add(1, 2)\\n3\\n\")), mdx(\"p\", null, \"You might be confused by all the numbers that Elixir has outputted for us. For now, don't worry about it because we won't be using that information any time soon. But if you insist on jumping ahead, you can check out \", mdx(\"a\", {\n    \"href\": \"http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#binaries-and-bitstrings\",\n    \"parentName\": \"p\"\n  }, \"Elixir binaries\"), \".\"), mdx(\"p\", null, \"That's the tip of the iceberg for modules. In the next lessons we'll learn more about composing our modules and connecting the pieces.\"), mdx(\"h2\", null, \"Structs\"), mdx(\"p\", null, \"When we need a named key-value store with known keys, we turn to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"structs\"), \". Structs share the name of the module they're defined in and use a syntax similar to our maps. We define them with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"defstruct\"), \" and a keyword list of fields and default values. For our example, let's create a simple module and struct to present a person:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> defmodule Person do\\n...>   defstruct name: nil, age: 0, location: nil\\n...> end\\n{:module, Person,\\n <<70, 79, 82, 49, 0, 0, 5, 32, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 133, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,\\n %Person{age: 0, location: nil, name: nil}}\\n\")), mdx(\"p\", null, \"Now that we have our struct created, it's time to use it. As we already learned, structs and maps share a similar syntax. In fact, structs are just maps with a few extra features. Create a new struct containing your information and try to access the fields:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"iex> sean = %Person{name: \\\"Sean\\\", age: 31, location: \\\"US\\\"}\\n%Person{age: 31, location: \\\"US\\\", name: \\\"Sean\\\"}\\niex> sean.age\\n31\\n\")), mdx(\"h2\", null, \"Notes for Ruby Developers\"), mdx(\"p\", null, \"It's no coincidence that Elixir and Ruby share many similarities; Jose, the creator of Elixir, was a contributor to Ruby and Rails.\"), mdx(\"div\", null), mdx(\"div\", null), mdx(\"h2\", null, \"Model-View-Controller\"), mdx(\"p\", null, \"Phoenix is the framework we'll be using with Elixir. If you've used an MVC framework such as Rails or SailsJS, a lot these topics will be familiar to you.\"), mdx(\"p\", null, \"Phoenix uses the standard Model-View-Controller (MVC) architecture that we have come to expect while building web apps. In general, anything related to the web application can be found in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"web\"), \" directory. In there we will find out \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"models\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"views\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"templates\"), \", and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"controllers\"), \". For those unfamiliar with MVC, we'll do high level overview.\"), mdx(\"p\", null, \"The appeal of the MVC pattern is the separation of concerns. As you probably guessed there are three components to the MVC pattern: models, views, and controllers. Another way of saying that is: data, user interface, decisions. In a MVC application the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"model\"), \" is responsible for managing data and business rules. Our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"view\"), \" handles the user interface be it HTML, XML, or JSON. Finally, our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"controller\"), \" handles most of the work orchestrating inputs to models and finally views.\"), mdx(\"p\", null, \"Most of the logic of how the user interacts with the app will be handled by the controller. You will see this in practice in short order, so there is no reason to go into detail here.\"), mdx(\"div\", null), mdx(\"h2\", null, \"Additional\"), mdx(\"p\", null, \"We will cover Phoenix and Elixir in more detail as the concepts become relevant in the rest of the tutorial.\"), mdx(\"p\", null, \"In Phoenix, most files are named with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"_\"), \" underscores. For example, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"user_controller.ex\"), \".\"), mdx(\"p\", null, \"When debugging, most people use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"IO.puts\"), \" to log some particular output to the terminal. For example, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"IO.puts \\\"Running!\\\"\"), \" will simply return a string if it is called.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"More Basics of Elixir and Phoenix Functions and modules Notes for Ruby developers Basics of Phoenix and MVC Control Structures Elixir…","frontmatter":{"title":"More Basics of Elixir and Phoenix","order":11,"active":true,"free":true,"tech":["phoenix","elixir"]}}},"pageContext":{"slug":"more-basics-of-elixir-and-phoenix","prevNode":{"frontmatter":{"title":"Basics of Elixir and Functional Programming","active":true,"free":true,"order":10,"tech":["phoenix","elixir"]},"fields":{"slug":"basics-of-elixir-and-functional-programming"}},"nextNode":{"frontmatter":{"title":"User Accounts and Signup: Part 1","active":true,"free":true,"order":12,"tech":["phoenix"]},"fields":{"slug":"user-accounts-and-signup-part-1"}}}},"staticQueryHashes":["1723017916"]}