There are some similarities between computer languages. The two most popular computer languages in these days are probably JavaScript and Python. I have looked in a few useful features between languages. The first three functions I have compared here are basically serving the same purpose with very similar syntax between JavaScript and Python. The purpose of this blog is to show how similar between two languages beyond basic syntax.
First one is Arrow function from JavaScript and Lambda from Python.
Lambda in Python, Arrow function in JavaScript
First, in JavaScript,
We can change this simple function to arrow function in JavaScript. I won’t mention about “this” binding with arrow function here but just talk about simplicity of arrow function.
//JavaScript function
function funcName(param) {
return param + 10;
}
//JavaScript arrow function
const funcName = (param) => param + 2
Both JavaScript arrow function and Python lambda can be used as a function by assigning function name, or it can just be used as an anonymous function. An anonymous function means the function doesn’t have its name assigned which is typically for one time use with serving simple purpose inside another function call. Arrow function in JavaScript serves more than its simplicity such as lexical “this” binding.
#Python function def func_name(param):
return param + 10
#Python lambda func_name = lambda param: param + 10
Map function
Map function between JavaScript and Python are almost identical. In JavaScript, forEach function does similar job but it is not used in some cases. map function is used instead where we need to create a new copy of object in a different memory location after map function call. In Python, map function works the same way as not corrupting the original copy in memory but creating a new one to reconstruct new one.
Looking at the following example, “nums” is original copy, and “doubles” is new copy. We don’t corrupt “nums” after running map function.
Map vs. Map
In Python:
nums = [ 1, 2, 3, 4, 5 ]
def double(x):
return x * 2
doubles = map(double, nums)
You can use Python lambda here to simplify it. Just one line of code…. Awesome!
doubles = list(map(lambda x: x * 2, nums))
#Result: [ 2, 4, 6, 8, 10 ]
In JavaScript:
We can also apply arrow function here to simplify it. In JavaScript, Map function is widely used in React.js as for the same reason that “nums” in the following won’t be modified after running map function.
const nums = [1, 2, 3, 4, 5]
const doubles = nums.map( x => x * 2);
console.log(doubles)
//Result:
[2, 4, 6, 8, 10]
Filter function
Filer function between JavaScript and Python is similar as what we have seen from map function. The same rule applies here that we don’t corrupt the original copy but reconstruct a new one after running filter function.
In Python:
Let’s try to take even numbers out from given list.
nums = [1,2,3,4,5,6]
evens = list(filter(lambda x : x % 2 == 0, nums))
#Result: [2, 4, 6]
In JavaScript:
const nums = [1,2,3,4,5,6]
const evens = nums.filter( x => x % 2 == 0 )
console.log(evens)
//Result:
[2,4,6]
Conclusion
Learning Python after JavaScript, or learning JavaScript after Python would be a lot of fun. It is like learning another foreign language but the new one you try to learn is already very similar to the one you already can read and write. Both languages are fantastic and its popularity is undeniable in modern computer programming languages.
暂无评论内容