Skip to content

Instantly share code, notes, and snippets.

Avatar
✌️

Willson Mock fay-jai

✌️
View GitHub Profile
View nonlocal-closure.py
def make_account(balance):
def withdraw(amount):
nonlocal balance
if balance > amount:
balance -= amount
return balance
def deposit(amount):
nonlocal balance
balance += amount
View simple-closure.py
def make_account(balance):
def can_withdraw(amount):
return balance > amount
return can_withdraw
can_i_withdraw = make_account(100)
can_i_withdraw(150)
View accessing-the-global-frame.py
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)
View understanding-frames.py
my_monies = 100
def make_account(balance):
return "You have " + str(balance) + " dollars in your bank account."
make_account(my_monies)
View programming-elixir-ch5-shorthand-notation.ex
# 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.
View programming-elixir-ch5-fizz-buzz.ex
# 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;
};
View programming-elixir-ch5-multiple-function-clauses.ex
# 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!!"
View programming-elixir-ch5-invoking-functions.ex
# 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
View programming-elixir-ch5-sum-function.js
// 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);
};
View programming-elixir-ch5-function-definitions.ex
# 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