This file contains 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
def make_account(balance): | |
def withdraw(amount): | |
nonlocal balance | |
if balance > amount: | |
balance -= amount | |
return balance | |
def deposit(amount): | |
nonlocal balance | |
balance += amount |
This file contains 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
def make_account(balance): | |
def can_withdraw(amount): | |
return balance > amount | |
return can_withdraw | |
can_i_withdraw = make_account(100) | |
can_i_withdraw(150) |
This file contains 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
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) |
This file contains 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
my_monies = 100 | |
def make_account(balance): | |
return "You have " + str(balance) + " dollars in your bank account." | |
make_account(my_monies) |
This file contains 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
# 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. |
This file contains 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
# 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; | |
}; |
This file contains 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 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!!" |
This file contains 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
# 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 |
This file contains 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
// 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); | |
}; |
This file contains 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
# 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 |
NewerOlder