mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
finished function section
This commit is contained in:
parent
b642fcb309
commit
b1e305626e
@ -362,30 +362,44 @@ all_the_args(1, 3, keyword_arg=4)
|
|||||||
# optional arg: 3
|
# optional arg: 3
|
||||||
# keyword arg: 4
|
# keyword arg: 4
|
||||||
|
|
||||||
|
# Julia has first class functions
|
||||||
|
function create_adder(x)
|
||||||
|
adder = function (y)
|
||||||
|
return x + y
|
||||||
|
end
|
||||||
|
return adder
|
||||||
|
end
|
||||||
|
|
||||||
####
|
# or equivalently
|
||||||
#### In progress point
|
function create_adder(x)
|
||||||
####
|
y -> x + y
|
||||||
|
end
|
||||||
|
|
||||||
# Python has first class functions
|
# you can also name the internal function, if you want
|
||||||
def create_adder(x):
|
function create_adder(x)
|
||||||
def adder(y):
|
function adder(y)
|
||||||
return x + y
|
x + y
|
||||||
return adder
|
end
|
||||||
|
adder
|
||||||
|
end
|
||||||
|
|
||||||
add_10 = create_adder(10)
|
add_10 = create_adder(10)
|
||||||
add_10(3) #=> 13
|
add_10(3) #=> 13
|
||||||
|
|
||||||
# There are also anonymous functions
|
# The first two inner functions above are anonymous functions
|
||||||
(lambda x: x > 2)(3) #=> True
|
(x -> x > 2)(3) #=> true
|
||||||
|
|
||||||
# There are built-in higher order functions
|
# There are built-in higher order functions
|
||||||
map(add_10, [1,2,3]) #=> [11, 12, 13]
|
map(add_10, [1,2,3]) #=> [11, 12, 13]
|
||||||
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]
|
filter(x -> x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]
|
||||||
|
|
||||||
# We can use list comprehensions for nice maps and filters
|
# We can use list comprehensions for nice maps and filters
|
||||||
|
[add_10(i) for i=[1, 2, 3]] #=> [11, 12, 13]
|
||||||
[add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13]
|
[add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13]
|
||||||
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]
|
|
||||||
|
####
|
||||||
|
#### In progress point
|
||||||
|
####
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
## 5. Classes
|
## 5. Classes
|
||||||
|
Loading…
Reference in New Issue
Block a user