Skip to content

Instantly share code, notes, and snippets.

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

Willson Mock fay-jai

✌️
View GitHub Profile
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
// Variadic sum function in JavaScript - note that we'll be using some ES6 features here
const sum = (...args) => { // args get collected into an array
return args.reduce((acc, current) => acc + current, 0);
};
# Anonymous function definitions in Elixir
sum = fn (a, b) -> a + b end
# Anonymous function definitions in JavaScript
var sum = (a, b) => a + b;
# Anonymous function definitions in Ruby