Skip to content

Instantly share code, notes, and snippets.

View fay-jai's full-sized avatar
✌️

Willson Mock fay-jai

✌️
View GitHub Profile
# 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
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]
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?
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
@fay-jai
fay-jai / programming-elixir-ch2.ex
Created November 9, 2018 06:09
Programming Elixir Chapter 2 - Pattern Matching
# 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
// 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:
const team1Hoop = createStore( /* ignore the arguments here for now */ );
const twoPointer = {
type: TWO_POINT_SHOT,
payload: {
points: 2
}
};
/*
// 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
/*
* 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
@fay-jai
fay-jai / custom_form.jsx
Created April 19, 2016 06:15
Component Backing Instances in React
// 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);