Digging deeper into the standard library

The standard library is written in Julia and is comprised of a very broad range of functionalities: from regular expressions, working with dates and times, a package manager, internationalization and Unicode, linear algebra, complex numbers, specialized mathematical functions, statistics, I/O and networking, Fast Fourier Transformations (FFT), and parallel computing, to macros, and reflection. Julia provides a firm and broad foundation for numerical computing and data science (for example, much of what NumPy has to offer is provided). Despite being targeted at numerical computing and data science, Julia aims to be a general-purpose programming language.

The source code of the standard library can be found in the sharejuliaase and sharejuliastdlib subfolders of Julia's root installation folder. Coding in Julia leads almost naturally to this source code, for example, when viewing all the methods of a particular function with methods(), or when using the @which macro to find out more about a certain method (refer to the Generic functions and multiple dispatch section in Chapter 3, Functions).

Here, we see the output of the command methods(+), which lists 167+ methods available in Julia version 1.0, together with their source locations:

The same command in a Jupyter notebook even provides hyperlinks to the source code.

We covered some of the most important types and functions in the previous chapters, and you can refer to the manual for a more exhaustive overview at https://docs.julialang.org/en/latest/base/base/.

It is certainly important to know that Julia contains a wealth of functional constructs to work with collections, such as the reduce, fold, min, max, sum, any, all, map, and filter functions. Some examples are as follows:

  • filter(f, coll) applies the function f to all the elements of the collection coll:
# code in Chapter 10stdlib.jl: 
filter(x -> iseven(x), 1:10) 

This returns 5-element Array{Int64,1}, which consists of 2, 4, 6, 8, and 10.

  • mapreduce(f, op, coll) applies the function f to all the elements of coll and then reduces this to one resulting value by applying the operation op:
mapreduce(x -> sqrt(x), +, 1:10) #> 22.4682781862041 
# which is equivalent to: 
sum(map(x -> sqrt(x), 1:10))

When working in the REPL, it can be handy to store a variable in the operating system's clipboard if you want to clean the REPL's variables memory with workspace(). Consider the ensuing example:

a = 42 
using InteractiveUtils  
clipboard(a) 
# quit and restart REPL: 
a # returns ERROR: a not defined 
a = clipboard() # returns "42" 

This also works while copying information from another application, for example, a string from a website or from a text editor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset