From 593f248b4913cdecd94e14f2e11c1e4fc63b4352 Mon Sep 17 00:00:00 2001 From: Luke Morris Date: Sun, 24 Jun 2018 18:27:06 -0700 Subject: [PATCH 1/5] sketch(beginner): 01 - intro to data types --- .../01-building-blocks-data.markdown | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown diff --git a/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown new file mode 100644 index 0000000..0bf8b83 --- /dev/null +++ b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown @@ -0,0 +1,123 @@ +* What are data types and why do we care? + * What's the purpose of programming? + * "Problem Solving", "Computer Science" + * ultimately a way to get a computer to make a decision for us, so that we don't have to spend brain power doing it + * It's a way to automate a decision + * It's a way to group those types of decisions together in the same place? + * In order to get the computer to make decisions on our behalf, we have to help it understand our world. So that it can make decisions about our world. + * (Computers know nothing a priori) + * So we have to construct a representation of our world. Then give that representation to the computer, which bases its decisions off that representation + * Let's talk about some of the tools we have to represent our world + +* Integers and Floats + * Probably the most fundamental building block + * Think: Banking Applications (balances), Games (Points, Scoreboards), Thermostats (Temp) + * Basic operations with Integers and Floats + * Addition, Subtraction, Division, Multiplication + * Depositing money to a bank account + * Withdrawing money from a bank account + * Calculating how high a character jumps based on gravity + * Calculating critical hit damage vs a zombie + +* Strings + * Another fundamental building block + * Think: Twitter (tweets), WhatsApp (SMS) + * Basic operations with Strings + * Concatenation + * Adding a greeting to a provided name on login + +* Compound Data Types + * Fundamental building blocks alone aren't sufficient for representing our world + * We don't just want to describe a bank balance ($125), we want to describe an ACCOUNT + * Map + * An account has many attributes + * owned by "Laura" + * currently has $125 + * has an account number + * has a routing number + * So we can translate to something like + %{ + "owner" => "Laura", + "amount" => 125, + "account_number" => 00005671, + "routing" => 1894593882 + } + + * Maps allow you to describe a concept or an object with attributes + * When describing a vehicle, you might talk about how many wheels it has, what type of propulsion it has, what color it is, what material it's made from, what its occupancy is, its name + * When describing a building, you might talk about how many windows it has, what kind of insulation it has, what kind of roof it has, what color its paint is, what its last appraisal was, what type of foundation it has, where it's located (lat/long, neighborhood, city, state), who owns the title, when it was last purchased, how much property taxes are, how many bedrooms / bathrooms there are, how many sqft it has, etc. + + * Class exercise: how might we represent a transaction + * First step, how do we describe a bank transaction + * Transaction transfers money from one account to another account on a particular date + * from account, to account, amount transferred, when transferred, transaction number + * so might look like + * %{ + "from" => 00005671, + "to" => 00005672, + "amount" => 20, + "when" => "May 20, 2018", + "transaction_number" => 12 + } + * and then you might have another transaction like + * %{ + "from" => 00005671, + "to" => 00005673, + "amount" => 25, + "when" => "May 21, 2018", + "transaction_number" => 13 + } + + * Lists + * Older bank accounts don't have just one transaction, typically they have a whole history of transactions + * You can represent a transaction history as a LIST of transactions + * here's what a list might look like [ transaction, transaction, transaction ] + * and this transaction list might exist on the account object: + * %{ + "owner" => "Laura", + "amount" => 125, + "account_number" => 00005671, + "routing" => 1894593882, + "transactions" => [ transaction, transaction, transaction ] + } + + * lists are great because you can store anything in them + * maybe you want a list of ElixirBridge attendees: ["Amy", "Bob", "Charlie", "Dana", ...] + * maybe you want a list of Lotto numbers: [3, 6, 12, 15, 35, 51] + * maybe you're doing some planning and want a list of Saturdays in 2018: [..., "June 23", "June 30", "July 7", ...] + * an account has a list of transactions + * a neighborhood has a list of houses + * a state has a list of cities + * a car has a list of wheels + * a business has a list of employees, a list of products, a list of executives, a list of investors, a list of customers, a list of testimonials + * an ikea furniture kit has a list of parts + +* Variables + * Point out that referring to large structs or large lists is cumbersome and painful + * When constructing a list of transactions, you don't want to type out each transaction over and over again + * You want to type out the first transaction once, and save it away for reference later + * Then construct the next transaction and save it away for reference + * `transaction_one = %{"from" => 000123, ...}` + * `transaction_two = %{"from" => 000123, ...}` + * Then when you build a list, you can refer to them by name, rather than by value + * [ transaction_one, transaction_two, transaction_three ] + * Maybe you want to name ^ this list your `transactions_list` + * `transactions_list = [transaction_one, transaction_two, transaction_three]` + * Then when you build your account, you can + * `my_account = %{ "owner" => "Laura", ..., "transactions" => transaction_list }` + + * Variables are great because you can give names to anything + * number_of_cars = 100 + * wheels_per_car = 4 + * my_city = "San Francisco" + * my_state = "California" + * my_account = %{ ... } + + * Then, just like we operated on numbers above, we can operate on variables: + * number_of_cars * wheels_per_car + * then save that off in a variable! + * total_wheels = number_of_cars * wheels_per_car + * And also with strings + * my_city <> ", " <> my_state + * then save that off in a variable! + * my_location = my_city <> ", " <> my_state \ No newline at end of file From 807ea9402b6d7d8a98e3c66fea0c4112b0f19f48 Mon Sep 17 00:00:00 2001 From: Luke Morris Date: Sun, 24 Jun 2018 18:29:11 -0700 Subject: [PATCH 2/5] sketch(beginner): 02 - intro to functions --- .../02-building-blocks-functions.markdown | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 0X_Learn_to_Code_with_Elixir/02-building-blocks-functions.markdown diff --git a/0X_Learn_to_Code_with_Elixir/02-building-blocks-functions.markdown b/0X_Learn_to_Code_with_Elixir/02-building-blocks-functions.markdown new file mode 100644 index 0000000..0228910 --- /dev/null +++ b/0X_Learn_to_Code_with_Elixir/02-building-blocks-functions.markdown @@ -0,0 +1,88 @@ +* With our DATA building blocks, we can construct a new world for the computer to operate in +* We can teach it about accounts, we can teach it about transactions +* We can teach it about cars, buildings, businesses, any other concrete object in the world, and even more abstract concepts + +* But this world isn't interesting if nothing moves +* Twitter wouldn't be interesting unless you could CREATE tweets, or LIKE tweets, or RETWEET tweets +* Youtube wouldn't be interesting unless you could DOWNLOAD videos and WATCH them +* Google Calendar wouldn't be interesting unless you could SCHEDULE events, MOVE events, ADD locations, INVITE participants +* Gmail wouldn't be interesting unless you could DRAFT a new email, ADD recipients, CHANGE the email body, SEND the email, RECEIVE email +* Note that all the interesting things above involve a data building block (tweet, video, events, locations, participants, email, email addresses) +* They become interesting when they are given life + +* We need a way to represent __action__ in our world + + +* Functions do 3 things + * Accept some input + * Do something + * Return some output + + +* Elixir has already written some functions for you. Let's play around with them + * String.capitalize("california") + (takes "california" as input + transforms it by capitalizing the first letter + returns the transformed string) + * String.length("how many letters?") + (takes "how many letters?" as input + counts the number of letters in the string, including spaces and question marks + returns that number) + * List.first(["Nancy", "Charles", "Wilfred"]) + (takes a list as input + extracts the first item from the list + returns that item) + * List.delete(["my account", "their account", "your account", "my friend's account"], "your account") + (takes a list as input + removes the specified item from the list + returns a shorter list without the specified item) + * Map.get(account, "owner") + (takes a map as input + finds the "owner" key and extracts the corresponding value + returns the value) + * Map.put(account, "owner", "Jake") + (takes a map as input + transforms it by setting the "owner" to "Jake" + returns the map) + * Map.keys(account) + (takes a map as input + extracts a list of all the keys it can find in the map + returns the list) + +* Let's briefly point out the structure of the above functions: + * Module.function_name(inputs, go, here) + * What's a module? Just like files have folders, functions have modules. They're a way for you to organize your functions + * Look at the functions above: + * All the functions that operate on strings (capitalize, length) are in the String module + * All the functions that operate on lists (first, delete) are in the List module + * All the functions that operate on maps (get, put, keys) are in the Map module + * As a side note, this lets use reuse function names: + * Map.delete transforms a map by deleting a key + * List.delete transforms a list by removing an item + + +* You might write a function to reschedule a calendar event + * Inputs: the event to reschedule, when it should be rescheduled to + * Do something: transform the event by saving the new time + * Return the new event + * CalendarEvent.move(event, "Mar 21, 2018") + +* You might write a function to like a tweet + * Inputs: tweet to be liked + * Do something: add 1 to the # of likes the tweet has + * Return the changed tweet + * Tweet.like(tweet) + +* You might write a function to add a recipient to your email draft + * Inputs: unsent email, new recipient address + * Do something: add the recipient address to the recipient list + * Return the updated email + * Email.add_recipient(email, "jose@elixirbridge.com") + +* You might write a function to draft a new email + * Inputs: nothing! + * Do something: construct a bare bones email map with empty values + * Return the new email draft + * Email.new() + + From 32df48817ce90a381bc7b6d709eabe7025ee1e8e Mon Sep 17 00:00:00 2001 From: Luke Morris Date: Sun, 24 Jun 2018 18:35:44 -0700 Subject: [PATCH 3/5] sketch(beginner): 01 - refine just a bit --- .../01-building-blocks-data.markdown | 5 ----- 1 file changed, 5 deletions(-) diff --git a/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown index 0bf8b83..20704df 100644 --- a/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown +++ b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown @@ -1,12 +1,7 @@ * What are data types and why do we care? * What's the purpose of programming? - * "Problem Solving", "Computer Science" * ultimately a way to get a computer to make a decision for us, so that we don't have to spend brain power doing it - * It's a way to automate a decision - * It's a way to group those types of decisions together in the same place? * In order to get the computer to make decisions on our behalf, we have to help it understand our world. So that it can make decisions about our world. - * (Computers know nothing a priori) - * So we have to construct a representation of our world. Then give that representation to the computer, which bases its decisions off that representation * Let's talk about some of the tools we have to represent our world * Integers and Floats From fd40266453d1cbfa35e04bc826ea72f69f4ce5c0 Mon Sep 17 00:00:00 2001 From: Luke Morris Date: Sun, 24 Jun 2018 18:42:17 -0700 Subject: [PATCH 4/5] sketch(beginner): 01 - integers and floats are just numbers --- 0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown index 20704df..00a5e60 100644 --- a/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown +++ b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown @@ -4,10 +4,10 @@ * In order to get the computer to make decisions on our behalf, we have to help it understand our world. So that it can make decisions about our world. * Let's talk about some of the tools we have to represent our world -* Integers and Floats +* Numbers * Probably the most fundamental building block * Think: Banking Applications (balances), Games (Points, Scoreboards), Thermostats (Temp) - * Basic operations with Integers and Floats + * Basic operations with Numbers * Addition, Subtraction, Division, Multiplication * Depositing money to a bank account * Withdrawing money from a bank account From cbd87e7978cb46ff6ba2740551a4c48f8dbf4dcd Mon Sep 17 00:00:00 2001 From: Luke Morris Date: Sun, 24 Jun 2018 18:43:03 -0700 Subject: [PATCH 5/5] sketch(beginner): 01 - no need for a header --- .../01-building-blocks-data.markdown | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown index 00a5e60..3a822d8 100644 --- a/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown +++ b/0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown @@ -1,8 +1,7 @@ -* What are data types and why do we care? - * What's the purpose of programming? - * ultimately a way to get a computer to make a decision for us, so that we don't have to spend brain power doing it - * In order to get the computer to make decisions on our behalf, we have to help it understand our world. So that it can make decisions about our world. - * Let's talk about some of the tools we have to represent our world +* What's the purpose of programming? +* ultimately a way to get a computer to make a decision for us, so that we don't have to spend brain power doing it +* In order to get the computer to make decisions on our behalf, we have to help it understand our world. So that it can make decisions about our world. +* Let's talk about some of the tools we have to represent our world * Numbers * Probably the most fundamental building block