Skip to content

Instantly share code, notes, and snippets.

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

Willson Mock fay-jai

✌️
View GitHub Profile
import React from "react";
import { render } from "react-dom";
const ParentComponent = React.createClass({
getDefaultProps: function() {
console.log("ParentComponent - getDefaultProps");
},
getInitialState: function() {
console.log("ParentComponent - getInitialState");
return { text: "" };
@fay-jai
fay-jai / hello_world.jsx
Created April 17, 2016 23:40
React Elements - Hello World
// This uses JSX syntax.
var helloWorld = <div>Hello World!</div>;
// And this is what the JSX syntax compiles into in JavaScript:
var helloWorld = React.createElement(
"div",
null,
"Hello World!"
);
def make_account(balance):
def withdraw(amount):
nonlocal balance
if balance > amount:
balance -= amount
return balance
def deposit(amount):
nonlocal balance
balance += amount
def make_account(balance):
def can_withdraw(amount):
return balance > amount
return can_withdraw
can_i_withdraw = make_account(100)
can_i_withdraw(150)
my_name = "Willson"
my_monies = 100
def make_account(balance):
return str(my_name) + " has " + str(balance) + " dollars in his / her bank account."
make_account(my_monies)
my_monies = 100
def make_account(balance):
return "You have " + str(balance) + " dollars in your bank account."
make_account(my_monies)
# Shorthand notation for anonymous functions in Elixir
sum = fn (a, b) -> a + b end # This is the normal notation for anonymous functions in Elixir
sum = &(&1 + &2) # This is the shorthand notation.
# The initial & is equivalent to the `fn` and `end` keywords.
# Within the parentheses, &1 refers to the first parameter, and &2 refers to the second parameter.
# If your anonymous function has more parameters, you can continue naming them in the same way.
# Just for comparison, here's the normal and shorthand notation for anonymous functions in JavaScript.
# FizzBuzz - Write a program that prints the numbers from 1 to 100. But for multiples of 3, print "Fizz" instead of the number and for the multiples of 5, print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz".
# First, the JavaScript implementation
const fizzBuzz = (num) => {
if (num % 3 === 0 && num % 5 === 0) return "FizzBuzz";
if (num % 3 === 0) return "Fizz";
if (num % 5 === 0) return "Buzz";
return num;
};
# Pattern Matching and Multiple Function Clauses in Elixir
greet = fn # The `fn` keyword starts the function definition
("Willson") -> "Wassup Willson!!" # A function clause consists of a parameter list and a body. In this case, we have defined 2 function clauses
(name) -> "How are you doing, #{name}?" # Notice that both function clauses have the same number of arguments (1, in this case).
end # The `end` keyword ends the function definition
greet.("Tiffany") # This returns "How are you doing, Tiffany?"
greet.("Willson") # This returns "Wassup Willson!!"
# How to invoke an anonymous function in Elixir
greet = fn (name) -> "Hello, #{name}" end
greet.("Willson") # Notice the dot right after variable name but before the opening parentheses
# How to invoke a named function in Elixir
defmodule Greeter do # This is an Elixir module, and for the purposes of this example, it's simply a container for named functions
def greet(name), do: "Hello #{name}" # Here we're defining a new named function `greet`
end