diff options
author | Leah Hanson <astrieanna@gmail.com> | 2013-07-02 17:23:10 -0400 |
---|---|---|
committer | Leah Hanson <astrieanna@gmail.com> | 2013-07-02 17:23:10 -0400 |
commit | b1e305626efe710e366003206ae53018f8e76df8 (patch) | |
tree | df8b3da1c95ce88499ffcb25700baabe48b44af3 /julia.html.markdown | |
parent | b642fcb3097bdaa4117bccab23088654f0ce4c76 (diff) |
finished function section
Diffstat (limited to 'julia.html.markdown')
-rw-r--r-- | julia.html.markdown | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/julia.html.markdown b/julia.html.markdown index e1c6731c..d54e7d0e 100644 --- a/julia.html.markdown +++ b/julia.html.markdown @@ -362,30 +362,44 @@ all_the_args(1, 3, keyword_arg=4) # optional arg: 3 # keyword arg: 4 +# Julia has first class functions +function create_adder(x) + adder = function (y) + return x + y + end + return adder +end -#### -#### In progress point -#### +# or equivalently +function create_adder(x) + y -> x + y +end -# Python has first class functions -def create_adder(x): - def adder(y): - return x + y - return adder +# you can also name the internal function, if you want +function create_adder(x) + function adder(y) + x + y + end + adder +end add_10 = create_adder(10) add_10(3) #=> 13 -# There are also anonymous functions -(lambda x: x > 2)(3) #=> True +# The first two inner functions above are anonymous functions +(x -> x > 2)(3) #=> true # There are built-in higher order functions 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 +[add_10(i) for i=[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 |