티스토리 뷰

7_Packages

Packages

Reference

https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/introductory-tutorials/intro-to-julia/)

Topics:

  1. Packages
  2. Exercises

Series

Packages

Julia has 1906 registered packages today, making packages a huge part of the Julia ecosystem.

Even so, the package ecosystem still has some growing to do. Notably, we have first class function calls to other languages, providing excellent foreign function interfaces. We can easily call into python or R, for example, with PyCall or Rcall.

This means that you don't have to wait until the Julia ecosystem is fully mature, and that moving to Julia doesn't mean you have to give up your favorite package/library from another language!

To see all available packages, check out

https://pkg.julialang.org/ or https://juliaobserver.com/

For now, let's learn how to use a package.

The first time you use a package on a given Julia installation, you need to use the package manager to explicitly add it:

In [1]:
using Pkg
Pkg.add("Example")
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `~/.julia/environments/v1.0/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v1.0/Manifest.toml`
 [no changes]

Every time you use Julia (start a new session at the REPL, or open a notebook for the first time, for example), you load the package with the using keyword

In [2]:
using Example

In the source code of Example.jl at https://github.com/JuliaLang/Example.jl/blob/master/src/Example.jl we see the following function declared

hello(who::String) = "Hello, $who"

Having loaded Example, we should now be able to call hello

In [3]:
hello("it's me. I was wondering if after all these years you'd like to meet.")
Out[3]:
"Hello, it's me. I was wondering if after all these years you'd like to meet."

Now let's play with the Colors package

In [4]:
Pkg.add("Colors")
 Resolving package versions...
  Updating `~/.julia/environments/v1.0/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v1.0/Manifest.toml`
 [no changes]
In [5]:
using Colors

Let's create a palette of 100 different colors

In [6]:
palette = distinguishable_colors(100)
Out[6]:

and then we can create a randomly checkered matrix using the rand command

In [7]:
rand(palette, 3, 3)
Out[7]:

In the next notebook, we'll use a new package to plot datasets.

Exercises

7.1

Load the Primes package (source code at https://github.com/JuliaMath/Primes.jl).

In [8]:
using Pkg
Pkg.add("Primes")
using Primes
 Resolving package versions...
  Updating `~/.julia/environments/v1.0/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v1.0/Manifest.toml`
 [no changes]
In [9]:
@assert @isdefined Primes

7.2

Verify that you can now use the function primes to grab all prime numbers under 1,000,000 and store it in variable primes_list

In [10]:
primes_list = primes(1000000)
Out[10]:
78498-element Array{Int64,1}:
      2
      3
      5
      7
     11
     13
     17
     19
     23
     29
     31
     37
     41
      ⋮
 999809
 999853
 999863
 999883
 999907
 999917
 999931
 999953
 999959
 999961
 999979
 999983
In [11]:
@assert primes_list == primes(1000000)

'Flux in Julia > Learning Julia (Intro_to_Julia)' 카테고리의 다른 글

08. Plotting  (0) 2018.09.30
07. Packages (한글)  (0) 2018.09.29
06. Functions (한글)  (0) 2018.09.27
06. Functions  (0) 2018.09.27
05. Conditionals (한글)  (0) 2018.09.18
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함