Skip to content

Commit 55d29c4

Browse files
Fix track 2/procedures.jl
1 parent 81ee62e commit 55d29c4

File tree

2 files changed

+94
-25
lines changed

2 files changed

+94
-25
lines changed

index.jl

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ using Markdown
1010
using InteractiveUtils
1111

1212
# ╔═╡ efc045a0-41b1-471d-9c51-fa828eb411b0
13-
# ╠═╡ disabled = true
14-
#=╠═╡
1513
# Automatically create a local environment and install needed packages
1614
using PyCall, BenchmarkTools
17-
╠═╡ =#
1815

1916
# ╔═╡ 8d964530-9e3d-11ed-301f-43882004bcd5
2017
md"""
2118
# A tour of Julia
2219
2320
Julia is a general purpose programming language designed with technical and scientific computing in mind.
2421
25-
It is a dynamic, high-level language, and its syntax is similar to other interpreted languages, like MATLAB or Python. The performance of Julia programs can match programs written using statically compiled languages, like C or Fortran. This is possible thanks to a clever design and Just In Time (JIT) compilation.
22+
It is a dynamic, high-level language, and its syntax is similar to other interpreted languages, like MATLAB or Python. The performance of Julia programs can match those of other implementations written in statically compiled languages, like C or Fortran. This is possible thanks to a clever design and Just In Time (JIT) compilation.
2623
2724
In the following I will introduce the language, its syntax and main features.
2825
"""
@@ -131,50 +128,32 @@ Another scenario might be **data-model integration using machine learning**: cha
131128
"""
132129

133130
# ╔═╡ b0caa168-c18e-4f71-a4af-6dd3d6110b91
134-
# ╠═╡ disabled = true
135-
#=╠═╡
136131
md"""
137132
## Brief features/performance show-off
138133
139134
Consider the following oversimplified illustration of the capabilities of Julia.
140135
"""
141-
╠═╡ =#
142136

143137
# ╔═╡ 28a20b93-b534-4c7d-a4aa-d105537c22f8
144-
# ╠═╡ disabled = true
145-
#=╠═╡
146138
# Import a Python library as Julia object
147139
np = pyimport("numpy")
148-
╠═╡ =#
149140

150141
# ╔═╡ e02dba57-3e24-404e-84a9-5244793943cb
151-
# ╠═╡ disabled = true
152-
#=╠═╡
153142
# Create a random Julia array of ten thousands elements
154143
xs = rand(10_000_000)
155-
╠═╡ =#
156144

157145
# ╔═╡ 8fbbf443-3220-4069-8fa4-b13936740c13
158-
# ╠═╡ disabled = true
159-
#=╠═╡
160146
# Pass a Julia array to the `numpy.sort` Python function, evaluate several times the function, print the minimum of the evaluation times to terminal, and convert the resulting `numpy.array` to a Julia array and assign it to a local variable.
161147
# ... In one line of code
162148
xs_sorted_numpy = @btime np.sort(xs)
163-
╠═╡ =#
164149

165150
# ╔═╡ 05ff3195-a071-4d0b-80e5-82395652ceae
166-
# ╠═╡ disabled = true
167-
#=╠═╡
168151
# Perform the same operations in Julia (the Julia implementation is as fast as, if not faster than, the low-level language implementation used by NumPy).
169152
xs_sorted_julia = @btime sort(xs)
170-
╠═╡ =#
171153

172154
# ╔═╡ 1e03fa5f-966a-443e-a488-987457887776
173-
# ╠═╡ disabled = true
174-
#=╠═╡
175155
# The compare the results
176156
xs_sorted_numpy == xs_sorted_julia
177-
╠═╡ =#
178157

179158
# ╔═╡ 00000000-0000-0000-0000-000000000001
180159
PLUTO_PROJECT_TOML_CONTENTS = """
@@ -393,7 +372,7 @@ version = "1.52.0+1"
393372
# ╟─a5c40f9a-0109-4baa-8225-a15e59010e35
394373
# ╟─98a176d7-5140-4492-8a18-b42ca3c3e87d
395374
# ╟─fff6699a-ae06-4ec5-8cd6-bdea86d84371
396-
# ╠═b0caa168-c18e-4f71-a4af-6dd3d6110b91
375+
# ╟─b0caa168-c18e-4f71-a4af-6dd3d6110b91
397376
# ╠═efc045a0-41b1-471d-9c51-fa828eb411b0
398377
# ╠═28a20b93-b534-4c7d-a4aa-d105537c22f8
399378
# ╠═e02dba57-3e24-404e-84a9-5244793943cb

track 2/procedures.jl

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,101 @@ bar(; kwargs...) = kwargs
343343
# ╔═╡ cf8cdafd-4814-4a18-8b96-29db5a86872f
344344
bar(x=1, y=2)
345345

346+
# ╔═╡ ee5d1867-7ca3-4bc3-a916-b3567be207ae
347+
md"""
348+
## Is Julia a FP language?
349+
350+
Some (if not most) concepts in computer science don't have rigorous definitions, as in math, at least when it comes to real systems. Functional Programming (FP) and Object-Oriented Programming (OOP) are examples of them.
351+
352+
Functional programming is a paradigm that has its roots in lambda calculus, and express computations as function application and composition, where the arguments and the return values of functions might be other functions (functions are _first-class citizens_).
353+
354+
In purely functional programming languages, programs are modeled as mathematical functions, that maps arguments to return values without changing the state of the world (side-effects). Evaluating the same function with the same arguments will return the same value every time, which means that functions could be substituted by a look-up tables, or _memoized_. This property provides certain guarantees about programs written in these languages, for example that they always terminate and that they can be executed concurrently without worrying about, for example, data races or deadlocks.
355+
356+
However, in most of real applications, a programming language has to deal with the state of the system, by reading it or changing it. For example, Haskell leverage its type system to clearly separate pure functions from impure functions.
357+
358+
Julia is heavily inspired by Lisp, and Scheme in particular. These are considered functional programming languages, and hence we might include Julia in this programming paradigm. As Lisp, Julia follows a more practical approach than Haskell, and allows to mix freely pure and impure functions.
359+
360+
In particular, Julia has full support for higher-order functions, such as [closures](https://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html).
361+
"""
362+
363+
# ╔═╡ 49d5b966-8e00-4a56-b3b1-c3a9a6fbe6e9
364+
let
365+
# You can return both functions defined locally and lambdas from functions
366+
367+
function greater_than(y) # equilvalent to `greater_than(y) = x -> x > y`
368+
function greater_than_value(x)
369+
x > y
370+
end
371+
return greater_than_value # pleonastic
372+
end
373+
374+
f = greater_than(0.5)
375+
376+
f(rand())
377+
end
378+
379+
# ╔═╡ c0693f3e-dadf-4755-9d49-468b2703ec09
380+
let
381+
# Indeed, comparison operators applied to only one arguments are functions
382+
f = >(0.5)
383+
384+
f(rand())
385+
end
386+
387+
# ╔═╡ 6c0579df-9fc3-4835-8623-54595e29bb6d
388+
# Hence they can be used in maps, as you would do with an anonymous function
389+
map(>(0.5), rand(10)) # == map(x -> x > 0.5, rand(10))
390+
391+
# ╔═╡ 045f2618-63eb-4bdb-8f22-cae4a7b1e649
392+
md"""
393+
Furthermore, there is a special syntax for defining lambdas and passing them to functions that accept a function as their first argument.
394+
395+
The following block of code
396+
```julia
397+
f(args...) do a, b, ...
398+
# function body
399+
end
400+
```
401+
is equivalent to the following
402+
```julia
403+
f((a, b, ...) -> begin #= function body =# end, args...)
404+
```
405+
406+
This makes working with files, reminiscent of what you would do in Python, using context managers:
407+
```julia
408+
open("outfile", "w") do io
409+
write(io, data)
410+
end
411+
```
412+
where `open` is defined the following way
413+
```julia
414+
function open(f::Function, args...)
415+
io = open(args...)
416+
try
417+
f(io)
418+
finally
419+
close(io)
420+
end
421+
end
422+
```
423+
"""
424+
425+
# ╔═╡ 349d01e3-1a55-4a9d-a80f-464830606ec6
426+
map(rand(10)) do x
427+
x > 0.5
428+
end
429+
346430
# ╔═╡ f070f6c1-6a42-46e6-bb42-616204534074
347431
md"""
348432
!!! exercise
349-
Define an higher-order function `nonmutating` that takes a function `f!` that mutates its first argument and returns a non-matating function `f` that takes the same arguments. The `nonmutating` function should have a keyword argument for choosing if to use `copy` or `deepcopy` in its implementation. Check your results.
433+
Define an higher-order function `nonmutating` that takes a function `f!` that mutates its first argument and returns a non-mutating function `f` that takes the same arguments. The `nonmutating` function should have a keyword argument for choosing if to use `copy` or `deepcopy` in its implementation. Check your results.
350434
"""
351435

352436
# ╔═╡ 84d5e04e-f828-42e6-a45e-cebdcd13cd1a
353437
md"""
354438
## Is Julia an OOP language?
355439
356-
Some (if not most) concepts in computer science don't have rigorous definitions, at least when it comes to real systems, and in contrast with mathematics or physics. Object-Oriented Programming (OOP) is one of them. I'll quote verbatim the first few paragraph of "What Is Object-Oriented Programming?" from the chapter 18 of Types and Programming Languages, by Benjamin Pierce.
440+
I'll quote verbatim the first few paragraph of "What Is Object-Oriented Programming?" from the chapter 18 of Types and Programming Languages, by Benjamin Pierce.
357441
358442
> Most arguments about "What is the essence of...?" do more to reveal the prejudices of the partecipants than to uncover any objective truth about the topic of discussion. Attempts to define the term "object-oriented" precisely are no exception. Nonetheless, we can identify a few fundamental features that are found in most object-oriented languages and that, in concert, support a distinctive programming style with well-understood advantages and disadvantages.
359443
> 1. **Multiple representations.** Perhaps the most basic characteristic of the object-oriented style is that, when an operation is invoked on an object, the object itself determines what code gets executed. [...] These implementations are called the object's _methods_. Invoking an operation on a object--called _method invocation_ or, more colorfully, sending it a _message_--involves looking up the operation's name at run time in a method table associated with the object, a process called _dynamic dispatch_. [...]
@@ -1050,6 +1134,12 @@ version = "17.4.0+2"
10501134
# ╠═bc41ad7b-883f-45de-bf5b-9759471fe380
10511135
# ╠═1c7c4966-428f-483c-acea-25b4e7c677fb
10521136
# ╠═cf8cdafd-4814-4a18-8b96-29db5a86872f
1137+
# ╟─ee5d1867-7ca3-4bc3-a916-b3567be207ae
1138+
# ╠═49d5b966-8e00-4a56-b3b1-c3a9a6fbe6e9
1139+
# ╠═c0693f3e-dadf-4755-9d49-468b2703ec09
1140+
# ╠═6c0579df-9fc3-4835-8623-54595e29bb6d
1141+
# ╟─045f2618-63eb-4bdb-8f22-cae4a7b1e649
1142+
# ╠═349d01e3-1a55-4a9d-a80f-464830606ec6
10531143
# ╟─f070f6c1-6a42-46e6-bb42-616204534074
10541144
# ╟─84d5e04e-f828-42e6-a45e-cebdcd13cd1a
10551145
# ╟─80768236-9675-11ed-3cfb-c31606f7223e

0 commit comments

Comments
 (0)