This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic Data Types in Elixir | |
# Atoms | |
name = :willson # an atom's name is its value | |
full_name = :willson_mock # underscores and other symbols are also valid in atom names | |
# Tuple | |
result = { :ok, "Hello World" } | |
{ response, message } = result # Tuples are often used in pattern matching |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = [1, 2, 3] | |
# Build a list - using the cons operator on the right hand side of an expression | |
add_to_list = [ 4 | list ] # This results in a new list with value [4, 1, 2, 3] | |
# Destructuring a list - using the cons operator on the left hand side of an expression | |
[ head | tail ] = list | |
IO.puts head # head is bound to the value 1 | |
IO.puts tail # tail is bound to the value [2, 3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doSomethingWith(array) { | |
// do something with array here | |
} | |
let array = [1, 2, 3]; // In languages like JavaScript, arrays are mutable data structures where you can add and remove values without changing the reference to the underlying object. | |
doSomethingWith(array); // When doSomethingWith is passed the array as an argument, we pass it by reference. The implication here is that whatever happens inside of the function can mutate the underlying array. | |
console.log(array); // At this point, you don't actually know what to expect when you log the array - does it still have [1, 2, 3] or does it have other values? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const petName = "Fido"; // You bind the value "Fido" to the variable petName | |
const shoutPetName = petName.toUpperCase(); // The value "FIDO" is bound to shoutPetName | |
console.log(petName); // However, the original value "Fido" hasn't been changed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pattern Matching in Elixir | |
# Basic pattern matching examples | |
a = 1 # In order for this to match, 'a' must be bound to the value of 1 | |
1 = a # Since a is already bound to the value of 1, this looks more like 1 = 1. And in that case, the match will be successful. | |
2 = a # Since a is already bound to the value of 1, this looks more like 2 = 1. This match will fail since we can't make both sides match in value. | |
# Pattern matching examples with lists |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PSEUDO CODE BELOW | |
import { combineReducers, createStore } from "redux"; | |
// sub reducer function that manages just the points part of the overall | |
// application state | |
function pointsReducer(pointsState = 0, action) { | |
switch (action.type) { | |
case TWO_POINT_SHOT: | |
case FREE_THROW: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const team1Hoop = createStore( /* ignore the arguments here for now */ ); | |
const twoPointer = { | |
type: TWO_POINT_SHOT, | |
payload: { | |
points: 2 | |
} | |
}; | |
/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PSEUDO CODE BELOW | |
import { createStore } from "redux"; | |
const team1Hoop = createStore( /* ignore the arguments here for now */ ); | |
/* | |
* The store created above manages your entire application's state. | |
* You can think of the store as containing a giant JavaScript object | |
* where each key is a particular piece of the application state and |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Actions are plain old JavaScript objects. They adhere to particular | |
* interface in that they must have a "type" property, but aside from | |
* that, you can add any other property you want. | |
*/ | |
const FREE_THROW = "FREE_THROW"; | |
const TWO_POINT_SHOT = "TWO_POINT_SHOT"; | |
// an example Action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here's a React Component class | |
class CustomForm extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
inputText: "Willson" | |
}; | |
this.handleInputChange = this.handleInputChange.bind(this); |