Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brainstorming a curriculum for beginners #119

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions 0X_Learn_to_Code_with_Elixir/01-building-blocks-data.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
* 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
* Think: Banking Applications (balances), Games (Points, Scoreboards), Thermostats (Temp)
* Basic operations with Numbers
* 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
88 changes: 88 additions & 0 deletions 0X_Learn_to_Code_with_Elixir/02-building-blocks-functions.markdown
Original file line number Diff line number Diff line change
@@ -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, "[email protected]")

* 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()