{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Packages\n", "\n", "Julia has over 2000 registered packages, making packages a huge part of the Julia ecosystem.\n", "\n", "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`.\n", "\n", "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!\n", "\n", "To see all available packages, check out\n", "\n", "https://pkg.julialang.org/\n", "or\n", "https://juliaobserver.com/\n", "\n", "For now, let's learn how to use a package." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first time you use a package on a given Julia installation, you need to use the package manager to explicitly add it:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `C:\\Users\\USti\\.julia\\registries\\General`\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[?25l" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[2K\u001b[?25h" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\USti\\.julia\\environments\\v1.4\\Project.toml`\n", "\u001b[90m [no changes]\u001b[39m\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\USti\\.julia\\environments\\v1.4\\Manifest.toml`\n", "\u001b[90m [no changes]\u001b[39m\n" ] } ], "source": [ "using Pkg\n", "Pkg.add(\"Example\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "using Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the source code of `Example.jl` at\n", "https://github.com/JuliaLang/Example.jl/blob/master/src/Example.jl\n", "we see the following function declared\n", "\n", "```\n", "hello(who::String) = \"Hello, $who\"\n", "```\n", "\n", "Having loaded `Example`, we should now be able to call `hello`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Hello, it's me. I was wondering if after all these years you'd like to meet.\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hello(\"it's me. I was wondering if after all these years you'd like to meet.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's play with the Colors package" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\USti\\.julia\\environments\\v1.4\\Project.toml`\n", "\u001b[90m [no changes]\u001b[39m\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\USti\\.julia\\environments\\v1.4\\Manifest.toml`\n", "\u001b[90m [no changes]\u001b[39m\n" ] } ], "source": [ "Pkg.add(\"Colors\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "using Colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a palette of 100 different colors" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "256-element Array{RGB{N0f8},1} with eltype RGB{FixedPointNumbers.Normed{UInt8,8}}:\n", " RGB{N0f8}(0.0,0.0,0.0)\n", " RGB{N0f8}(1.0,1.0,0.455)\n", " RGB{N0f8}(1.0,0.608,1.0)\n", " RGB{N0f8}(0.0,0.827,1.0)\n", " RGB{N0f8}(0.886,0.388,0.051)\n", " RGB{N0f8}(0.0,0.494,0.0)\n", " RGB{N0f8}(0.0,0.314,0.902)\n", " RGB{N0f8}(0.675,0.0,0.278)\n", " RGB{N0f8}(0.0,1.0,0.784)\n", " RGB{N0f8}(0.0,0.392,0.408)\n", " RGB{N0f8}(1.0,0.835,0.769)\n", " RGB{N0f8}(0.424,0.322,0.0)\n", " RGB{N0f8}(0.478,0.459,0.506)\n", " ⋮\n", " RGB{N0f8}(0.733,0.675,0.647)\n", " RGB{N0f8}(0.824,0.0,0.345)\n", " RGB{N0f8}(0.643,0.392,0.471)\n", " RGB{N0f8}(0.957,0.894,1.0)\n", " RGB{N0f8}(0.153,0.212,0.2)\n", " RGB{N0f8}(0.294,0.047,0.012)\n", " RGB{N0f8}(0.494,0.345,0.506)\n", " RGB{N0f8}(0.0,0.722,0.173)\n", " RGB{N0f8}(0.463,0.631,0.753)\n", " RGB{N0f8}(0.0,0.02,0.098)\n", " RGB{N0f8}(0.0,0.549,0.812)\n", " RGB{N0f8}(0.561,0.431,0.467)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "palette = distinguishable_colors(256)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "?distinguishable_colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and then we can create a randomly checkered matrix using the `rand` command" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "3×3 Array{RGB{N0f8},2} with eltype RGB{FixedPointNumbers.Normed{UInt8,8}}:\n", " RGB{N0f8}(0.349,0.314,0.4) … RGB{N0f8}(1.0,0.882,0.89)\n", " RGB{N0f8}(0.729,0.478,0.345) RGB{N0f8}(0.0,0.553,0.325)\n", " RGB{N0f8}(0.0,0.176,0.188) RGB{N0f8}(1.0,1.0,0.682)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(palette, 3, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the next notebook, we'll use a new package to plot datasets." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises\n", "\n", "#### 7.1\n", "Load the Primes package (source code at https://github.com/JuliaMath/Primes.jl)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", "\u001b[32m\u001b[1m Installed\u001b[22m\u001b[39m Primes ─ v0.4.0\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\USti\\.julia\\environments\\v1.4\\Project.toml`\n", " \u001b[90m [27ebfcd6]\u001b[39m\u001b[92m + Primes v0.4.0\u001b[39m\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\USti\\.julia\\environments\\v1.4\\Manifest.toml`\n", " \u001b[90m [27ebfcd6]\u001b[39m\u001b[92m + Primes v0.4.0\u001b[39m\n" ] } ], "source": [ "Pkg.add(\"Primes\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "using Primes" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "@assert @isdefined Primes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 7.2\n", "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`" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "78498-element Array{Int64,1}:\n", " 2\n", " 3\n", " 5\n", " 7\n", " 11\n", " 13\n", " 17\n", " 19\n", " 23\n", " 29\n", " 31\n", " 37\n", " 41\n", " ⋮\n", " 999809\n", " 999853\n", " 999863\n", " 999883\n", " 999907\n", " 999917\n", " 999931\n", " 999953\n", " 999959\n", " 999961\n", " 999979\n", " 999983" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes_list = primes(1000000)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "@assert primes_list == primes(1000000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.4", "language": "julia", "name": "julia-1.3" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.0" } }, "nbformat": 4, "nbformat_minor": 4 }