You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: index.jl
+2-23Lines changed: 2 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -10,19 +10,16 @@ using Markdown
10
10
using InteractiveUtils
11
11
12
12
# ╔═╡ efc045a0-41b1-471d-9c51-fa828eb411b0
13
-
# ╠═╡ disabled = true
14
-
#=╠═╡
15
13
# Automatically create a local environment and install needed packages
16
14
using PyCall, BenchmarkTools
17
-
╠═╡ =#
18
15
19
16
# ╔═╡ 8d964530-9e3d-11ed-301f-43882004bcd5
20
17
md"""
21
18
# A tour of Julia
22
19
23
20
Julia is a general purpose programming language designed with technical and scientific computing in mind.
24
21
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.
26
23
27
24
In the following I will introduce the language, its syntax and main features.
28
25
"""
@@ -131,50 +128,32 @@ Another scenario might be **data-model integration using machine learning**: cha
131
128
"""
132
129
133
130
# ╔═╡ b0caa168-c18e-4f71-a4af-6dd3d6110b91
134
-
# ╠═╡ disabled = true
135
-
#=╠═╡
136
131
md"""
137
132
## Brief features/performance show-off
138
133
139
134
Consider the following oversimplified illustration of the capabilities of Julia.
140
135
"""
141
-
╠═╡ =#
142
136
143
137
# ╔═╡ 28a20b93-b534-4c7d-a4aa-d105537c22f8
144
-
# ╠═╡ disabled = true
145
-
#=╠═╡
146
138
# Import a Python library as Julia object
147
139
np =pyimport("numpy")
148
-
╠═╡ =#
149
140
150
141
# ╔═╡ e02dba57-3e24-404e-84a9-5244793943cb
151
-
# ╠═╡ disabled = true
152
-
#=╠═╡
153
142
# Create a random Julia array of ten thousands elements
154
143
xs =rand(10_000_000)
155
-
╠═╡ =#
156
144
157
145
# ╔═╡ 8fbbf443-3220-4069-8fa4-b13936740c13
158
-
# ╠═╡ disabled = true
159
-
#=╠═╡
160
146
# 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.
161
147
# ... In one line of code
162
148
xs_sorted_numpy =@btime np.sort(xs)
163
-
╠═╡ =#
164
149
165
150
# ╔═╡ 05ff3195-a071-4d0b-80e5-82395652ceae
166
-
# ╠═╡ disabled = true
167
-
#=╠═╡
168
151
# 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).
Copy file name to clipboardExpand all lines: track 2/procedures.jl
+92-2Lines changed: 92 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -343,17 +343,101 @@ bar(; kwargs...) = kwargs
343
343
# ╔═╡ cf8cdafd-4814-4a18-8b96-29db5a86872f
344
344
bar(x=1, y=2)
345
345
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
+
functiongreater_than(y) # equilvalent to `greater_than(y) = x -> x > y`
368
+
functiongreater_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
+
346
430
# ╔═╡ f070f6c1-6a42-46e6-bb42-616204534074
347
431
md"""
348
432
!!! 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.
350
434
"""
351
435
352
436
# ╔═╡ 84d5e04e-f828-42e6-a45e-cebdcd13cd1a
353
437
md"""
354
438
## Is Julia an OOP language?
355
439
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.
357
441
358
442
> 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.
359
443
> 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_. [...]
0 commit comments