Skip to content

Instantly share code, notes, and snippets.

@chidimo
Last active May 15, 2019 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chidimo/430378eff15b5e1f0290bc3a8d1ce5a6 to your computer and use it in GitHub Desktop.
Save chidimo/430378eff15b5e1f0290bc3a8d1ce5a6 to your computer and use it in GitHub Desktop.
Comparison of Higher Order Functions (HOF) in Python and JavaScript

JavaScript Code

let salute = (age) => {
    let showAge = (name) => {
        console.log(`Welcome ${name}`)
        console.log(`Your age is ${age}`)
    }
    return showAge
}

s = salute(16) // returns showAge function
s("chidi")

console.log(s) // returns the showAge function
(name) => {
    console.log("Welcome " + name)
    console.log("Your age is " + age)
}

Output

Welcome chidi

Your age is 16

Python Code

def salute(age):
    def show_age(name):
        print("Welcome ", name)
        print("Your age is ", age)
    return show_age

# Call the function
s = salute(16) # returns the show_age function
s("chidi") # pass the my name argument to the show_age function

Output

Welcome chidi

Your age is 16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment