28
loading...
This website collects cookies to deliver better user experience
If Javascript, an old language with so many flaws, could evolve like it did, Python could learn from it and step out of its ivory tower.
x = lambda a : a + 10
x = lambda a : a + 10 (lambda b : b * 2))
let x = (c => c + 2)(a => a + 10)(b => b * 2)(y);
let x = y |> (c => c + 2) |> (a => a + 10) |> (b => b * 2)
let nums = [1, 2, 3, 4].map(n => n * 2);
let nums = [1, 2, 3, 4].iter().map(|n| n * 2);
module:function
approach, which is similar to what Python does. However, their expression-based syntax and unambiguous scoped variable binding makes the code readable and easy to get right. Here is a sample of a map function in Ocaml:let nums = let double n = n * 2 in List.map double [1; 2; 3; 4]
double = lambda n : n * 2
nums = map(double, [1, 2, 3, 4])
let nums = [1, 2, 3, 4]
.map(n => n * 2)
.filter(n => n > 2);
.reduce((acc, current) => acc + current);
let nums = [1; 2; 3; 4]
|> map (fun n -> n * 2)
|> filter (fun n -> n > 2)
|> fold_left ( + ) 0
def hello(func):
def inner():
print("Hello ")
func()
return inner
def name():
print("Alice")
# `hello` is a decorator function
obj = hello(name)
obj() # prints "Hello Alice"
The short-handed version for this is:
@hello
def name():
print("Alice")
if __name__ == '__main__':
name() # prints "Hello Alice"