GOTOH Shunsuke
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 発表, 機械学習名古屋, Julia slideOptions: transition: slide theme: league --- Julia v1.0 の紹介 === <!-- .element: style="font-size:330%" --> 2018/09/15 機械学習 名古屋 第17回勉強会 antimon2(後藤 俊介) Note: スポンサー枠(?)LTスライドですっ ---- ## お品書き + 自己紹介 + Juliaの紹介 + Juliaのパッケージマネージャ + Juliaでプロジェクト開発 --- # 自己紹介 ---- ## 自己紹介 + 名前:後藤 俊介 + 所属:**[有限会社 来栖川電算](https://www.kurusugawa.jp)** + コミュニティ:**[機械学習名古屋](https://machine-learning.connpass.com/)**, [Python東海](https://connpass.com/series/292/), NGK2018B, Ruby東海, Rails Girls Nagoya(コーチ), … + 言語:**[Julia](https://julialang.org)**, Python, Scala(勉強中), Ruby, … + ![Twitter](https://i.imgur.com/HqouMIg.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [@antimon2](https://twitter.com/antimon2) / ![Facebook](https://i.imgur.com/01nPd37.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [antimon2](https://www.facebook.com/antimon2) + ![Github](https://i.imgur.com/yBKtii5.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [antimon2](https://github.com/antimon2/) / ![Qiita](https://i.imgur.com/FxHMi64.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [@antimon2](http://qiita.com/antimon2) Note: 今日も Julia の話っ ---- [![有限会社来栖川電算](https://i.imgur.com/8Kuhfel.png) https://www.kurusugawa.jp](https://www.kurusugawa.jp) Note: スポンサー枠っ ---- [![Julia Tokyo #8](https://i.imgur.com/iSfdNyK.png) https://juliatokyo.connpass.com/event/100780/](https://juliatokyo.connpass.com/event/100780/) Note: 『機械学習と名古屋とJulia(仮)』というテーマで発表予定っ ---- [![Python東海 第37回勉強会](https://i.imgur.com/9sy5QNY.png) https://connpass.com/event/98350/](https://connpass.com/event/98350/) Note: LT予定っ --- # Julia の紹介 Note: Julia 良いよ Julia っ ---- [![Julia](https://upload.wikimedia.org/wikipedia/commons/6/69/Julia_prog_language.svg)<!-- .element: style="background:white;max-width:80%" -->](https://julialang.org) ---- ## Julia とは?(1) + [The Julia Language](https://julialang.org) + 最新 v1.0.0(2018/08/08) + ついに出ました v1.0! + 科学技術計算に強い! + 動作が速い!(LLVM JIT コンパイル) Note: ググるときは [julialang](https://www.google.co.jp/search?q=julialang) で! ---- ## Julia とは?(2) > + Rのように中身がぐちゃぐちゃでなく、 > + Rubyのように遅くなく、 > + Lispのように原始的またはエレファントでなく、 > + Prologのように変態的なところはなく、 > + Javaのように硬すぎることはなく、 > + Haskellのように抽象的すぎない > > ほどよい言語である <!-- .element: style="font-size:66%" --> 引用元:http://www.slideshare.net/Nikoriks/julia-28059489/8 <!-- .element: style="font-size:71%" --> ---- ## Julia とは?(3) > + C のように高速だけど、 Ruby のような動的型付言語である > + Lisp のようにプログラムと同等に扱えるマクロがあって、しかも Matlab のような直感的な数式表現もできる > + Python のように総合的なプログラミングができて、 R のように統計処理も得意で、 Perl のように文字列処理もできて、 Matlab のように線形代数もできて、 shell のように複数のプログラムを組み合わせることもできる > + 超初心者にも習得は簡単で、 超上級者の満足にも応えられる > + インタラクティブにも動作して、コンパイルもできる <!-- .element: style="font-size:50%" --> ([Why We Created Julia](http://julialang.org/blog/2012/02/why-we-created-julia) から抜粋・私訳) <!-- .element: style="font-size:71%" --> Note: 要するに、いろんな言語の「いいとこどり」言語!ってことでっ ---- ## 主な機能 + [多重ディスパッチ](https://ja.wikipedia.org/wiki/%E5%A4%9A%E9%87%8D%E3%83%87%E3%82%A3%E3%82%B9%E3%83%91%E3%83%83%E3%83%81) + 動的型システム + [並行・並列処理](https://docs.julialang.org/en/v1/manual/parallel-computing/)、コルーチン + **[組込パッケージマネージャ](https://docs.julialang.org/en/v1/stdlib/Pkg/)** Note: v0.7から組込パッケージマネージャに大きな仕様変更が入ったんですけれど今回はその話っ ---- ## 文法・関数 Note: 以降、ほぼ過去スライドからのコピペ。すっ飛ばして先へ進んで戴いてもOKっ ---- ### 基本的な演算 ```julia julia> 1 + 2 - 3 * 4 # 四則演算(除算以外) -9 julia> 7 / 5 # `整数 / 整数` の結果は浮動小数 1.4 julia> 7 ÷ 5 # `整数 ÷ 整数` の結果は整数 1 julia> 2 ^ 10 # 冪乗は `^` 1024 julia> 123 & 234 | 345 # 論理積 / 論理和 376 julia> 123 ⊻ 234 # 排他的論理和(==`xor(123, 234)`) 145 ``` <!-- .element: style="font-size:46%" --> Note: 整数同士の除算は実数になりますっ 整数除算演算子 `÷` が別に存在します(Python の `//` 相当)っ また冪乗も(`**` ではなく)`^` ですっ `⊻` は `\veebar`+<kbd>Tab</kbd>で変換できますっ ちなみに先ほどの `÷` も `\div`+<kbd>Tab</kbd>で(基本的に $\TeX$ の書式)っ ---- ### 配列 ```julia julia> a = [1, 2, 3, 4, 5] 5-element Array{Int64,1}: 1 2 3 4 5 julia> a[1] # Julia は 1-origin 1 julia> println(a[2:3]) # 範囲指定は両端含む [2, 3] ``` <!-- .element: style="font-size:50%" --> Note: 1-origin であることに注意すればあとは普通の配列っ あと `a:b` は範囲(`Range`)の記法。両端を含む(Ruby の `a..b` と同じ)っ ---- ### 配列の内包表記 (1) ```julia julia> a = [n^2 for n=1:5] 5-element Array{Int64,1}: 1 4 9 16 25 julia> A = [x+10y for y=1:3, x=1:3] 3×3 Array{Int64,2}: 11 12 13 21 22 23 31 32 33 ``` <!-- .element: style="font-size:50%" --> Note: 内包表記の記法は Python に類似っ かつ、`for` にカンマ区切りで複数のイテレータを渡すことで2次元以上の配列も作成可能っ ---- ### 配列の内包表記 (2) ```julia julia> [(a,b,c) for c=1:15,b=1:15,a=1:15 if a^2+a*b+b^2==c^2] 6-element Array{Tuple{Int64,Int64,Int64},1}: (3, 5, 7) (5, 3, 7) (6, 10, 14) (7, 8, 13) (8, 7, 13) (10, 6, 14) ``` Note: Python と同様に `if` で条件を指定することも可能っ あと Python と同様、`[○ for ○=○]` を `(○ for ○=○)` と書くと配列ではなくて `Generator` が返りますっ ---- ### ベクトル ```julia julia> x = [1., 2., 3.]; y = [3., 1., 2.]; julia> x + y # `x .+ y` と書いても同じ(elementwise operation) [4., 3., 5.] julia> x .* y # これは `x * y` と書くとNG [3., 2., 6.] julia> using LinearAlgebra julia> x ⋅ y # 内積(dot積、`dot(x, y)` と書いても同じ) 11.0 julia> x × y # 外積(cross積、`cross(x, y)` と書いても同じ) [1., 7., -5.] ``` <!-- .element: style="font-size:50%" --> Note: Julia では実は1次元配列がベクトルの扱いっ `⋅` は `\cdot`+<kbd>Tab</kbd>、`×` は `\times`+<kbd>Tab</kbd>(これらを利用するには `using LinearAlgebra` 必要)っ あとこれらや先ほどの `÷` や `⊻` などのように、ASCIIの範囲を超えたUnicode文字の演算子(そのほとんどが $\TeX$ 由来)が Julia にはたくさんあります(他には例えば比較演算子の `≤` `≥` や、集合の要素 `∈` や包含関係 `⊆` などなど) ---- ### 行列 ```julia julia> A = [1 2; 3 4] # この記法は MATLAB/Octave 由来 2×2 Array{Int64,2}: 1 2 3 4 julia> A' # `○'` は転置行列の記法(これも MATLAB/Octave 由来) 2×2 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}: 1 3 2 4 julia> transpose(A) # 正確には転置行列はこっち 2×2 LinearAlgebra.Transpose{Int64,Array{Int64,2}}: 1 3 2 4 ``` <!-- .element: style="font-size:50%" --> Note: Julia では2次元配列が行列の扱いっ あと `○.'` という書式は廃止されました(`transpose(A)` 使ってね)っ ---- ### 行列の演算 ```julia julia> A = [1 2; 3 4]; B = [3 0; 0 6]; julia> A + B # A .+ B でも同様 2×2 Array{Int64,2}: 4 2 3 10 julia> A * B # matrix multiply 2×2 Array{Int64,2}: 3 12 9 24 julia> A .* B # elementwise multiply 2×2 Array{Int64,2}: 3 0 0 24 ``` <!-- .element: style="font-size:48%" --> Note: 行列は `*` で通常の行列積になりますっこれ便利っ ---- ### ブロードキャスト ```julia julia> sin(0.1) 0.09983341664682815 julia> sin.([0.1, 0.2, 0.3, 0.4]) 4-element Array{Float64,1}: 0.0998334 0.198669 0.29552 0.389418 julia> [0.1, 0.2, 0.3, 0.4] .^ 2 # => [0.01, 0.04, 0.09, 0.16] ``` <!-- .element: style="font-size:50%" --> Note: 関数名と `(` の間に `.` を置くと、普通の関数を配列に拡張してくれる(ブロードキャスト)っ `.^` のように演算子の前に `.` を書いても同様(先ほど出た `.+` `.*` もブロードキャスト)っ ---- ### 関数定義 ```julia julia> f(x) = x^2 + 2x - 1 f (generic function with 1 method) julia> f(1) 2 julia> f.(1:5) # => [2, 7, 14, 23, 34] ``` Note: 数学のように直感的な記述で関数を定義可能っ `2x` は `2*x` の省略形、曖昧さがなければリテラルと他の識別子が続く場合などに勝手に乗算と解釈してくれるっ またユーザ定義関数も `.` をつけて自動的にブロードキャスト対応っ ---- ### 有理数・複素数 ```julia julia> 1//2 == 0.5 true julia> 1//2 - 1//3 1//6 julia> 1im ^ 2 == -1 true julia> (1.0 + 0.5im) * (2.0 - 3.0im) 3.5 - 2.0im ``` Note: 有理数・複素数を標準サポート。 `//` は有理数除算(結果は有理数) `im` は虚数単位。 どちらも四則演算も普通に書けますっ --- # Julia のパッケージマネージャ <!-- .element: style="font-size:290%" --> ---- ## Julia のパッケージマネージャ <!-- .element: style="font-size:177%" --> + <!-- .element: class="fragment" data-fragment-index="1" -->≒ pip + pipenv + α<span><!-- .element: class="fragment" data-fragment-index="2" --> (in Python)</span> + <!-- .element: class="fragment" data-fragment-index="2" -->≒ rubygems + bundler + α (in Ruby) ---- ```bash $ julia _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.0.0 (2018-08-08) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia> # `]` キーをタイプするとパッケージマネージャREPL起動 (v1.0) pkg> ``` Note: 以降「パッケージモード」と呼ぶことにします。 ---- ### パッケージの追加 ```julia (v1.0) pkg> add Plots GR UnicodePlots 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` [28b8d3ca] + GR v0.33.1 [91a5bcdd] + Plots v0.20.1 [b8865327] + UnicodePlots v0.3.1 Updating `~/.julia/environments/v1.0/Manifest.toml` : 《中略》 ``` Note: パッケージモードで `pkg> add HOGE` とすることで、外部パッケージをインストールできます。 このように複数指定してまとめてインストール可能。依存関係も自動的に解決してまとめてインストールしてくれます(ここでは詳細省略) ---- ### パッケージの削除 ```julia (v1.0) pkg> rm UnicodePlots Updating `~/.julia/environments/v1.0/Project.toml` [b8865327] - UnicodePlots v0.3.1 Updating `~/.julia/environments/v1.0/Manifest.toml` [b8865327] - UnicodePlots v0.3.1 ``` Note: `pkg> rm 〜` で、指定したパッケージをアンインストールできます。 ---- ### パッケージの更新 ```julia (v1.0) pkg> up 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` [28b8d3ca] ↑ GR v0.33.1 ⇒ v0.34.1 [91a5bcdd] ↑ Plots v0.20.1 ⇒ v0.20.2 Updating `~/.julia/environments/v1.0/Manifest.toml` : 《中略》 ``` Note: `pkg> up` でインストールしたパッケージをまとめて更新してくれます。 これは bundler の `bundle update` に相当。 `pkg> up HOGE` のように指定したパッケージだけを更新することも可能。この場合、依存関係で自動的にインストールされたパッケージも同時に更新してくれます。 こちらは pip の `pip install -U HOGE` に相当。 ---- ### パッケージの確認 ```julia (v1.0) pkg> st Status `~/.julia/environments/v1.0/Project.toml` [28b8d3ca] GR v0.34.1 [7073ff75] IJulia v1.11.1 [91a5bcdd] Plots v0.20.2 [27ebfcd6] Primes v0.4.0 ``` Note: `st` は `status` の略。現在インストールされているパッケージとそのバージョン情報を表示します。 ここに表示されるのは、明示的に `add` で追加したパッケージのみ。依存関係で自動でインストールされたものはここには表示されません。 ---- ### パッケージのテスト ```julia (v1.0) pkg> test Primes Testing Primes Resolving package versions... Status `/var/folders/ff/txf9513j0hsbvpsbp7b7cnzm0000gn/T/tmpx5KHFf/Manifest.toml` [864edb3b] DataStructures v0.12.0 [27ebfcd6] Primes v0.4.0 [2a0f44e3] Base64 [`/Applications/Julia-1.0.app/Contents/Resources/julia/bin/../share/julia/stdlib/v1.0/Base64`] : 《中略》 Test Summary: | Pass Total prime(::Int32) | 20 20 Test Summary: | Pass Total prime(::BigInt) | 20 20 Testing Primes tests passed ``` Note: インストールしたパッケージが自分の環境できちんと動くかどうか確認できます。開発時にもよく使うコマンド。 ---- ### 環境管理 (1) ```bash $ mkdir NewEnv && cd $_ $ pwd /path/to/NewEnv $ julia ``` ```julia julia> # `]` (v1.0) pkg> activate . (NewEnv) pkg> status Status `Project.toml` (NewEnv) pkg> # `BackSpace` julia> # `;` shell> ls -l total 0 ``` Note: ディレクトリを作ってその中で Julia REPL を起動し、パッケージモードで `pkg> activate .` とすると、そのディレクトリ名の仮想環境を作成します。 `pkg> status` を実行すると、それまでインストールしたはずのパッケージがまったく表示されないのが分かります。 `pipenv --python x` とか `bundle init` とかに相当。 ---- ### 環境管理 (2) ```julia julia> # `]` (NewEnv) pkg> add Plots UnicodePlots Resolving package versions... Updating `Project.toml` [91a5bcdd] + Plots v0.20.2 [b8865327] + UnicodePlots v0.3.1 Updating `Manifest.toml` : 《中略》 shell> ls -l total 24 -rw-r--r-- 1 antimon2 staff 6677 9 13 15:22 Manifest.toml -rw-r--r-- 1 antimon2 staff 108 9 13 15:22 Project.toml ``` Note: 何かしらパッケージを `add` して初めて、Project.toml / Manifest.toml ファイルが生成されます。 ---- ### 環境管理 (3) Project.toml: <!-- .element: style="text-align:left" --> ```toml [deps] Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228" ``` Note: Project.toml には、`add` で追加したパッケージとその uuid が格納されます。 pipenv の Pipfile、bundler の Gemfile に相当。 ---- ### 環境管理 (4) Manifest.toml: <!-- .element: style="text-align:left" --> ```toml [[Base64]] uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" [[ColorTypes]] deps = ["FixedPointNumbers", "Random", "Test"] git-tree-sha1 = "0e3209ba7418aed732e5c3818076b4400ee36c08" uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" version = "0.7.4" : 《中略》 [[Plots]] deps = ["Base64", "Contour", "Dates", "FixedPointNumbers", "GR", "JSON", "LinearAlgebra", "Measures", "NaNMath", "Pkg", "PlotThemes", "PlotUtils", "Printf", "Random", "RecipesBase", "Reexport", "Requires", "Showoff", "SparseArrays", "StaticArrays", "Statistics", "StatsBase", "Test", "UUIDs"] git-tree-sha1 = "fa4f80b7e2173b8605353bdadf10d3ee8569efc6" uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" version = "0.20.2" : 《中略》 [[UnicodePlots]] deps = ["Dates", "Pkg", "SparseArrays", "StatsBase", "Test"] git-tree-sha1 = "d26d1e85fb7e9d432a7bb0cd6e421e8a82928604" uuid = "b8865327-cd53-5732-bb35-84acbb429228" version = "0.3.1" ``` <!-- .element: style="font-size:30%" --> Note: Manifest.toml には、依存関係解決で自動でインストールされた全パッケージ、および依存関係の定義等が格納されます。 pipenv の Pipfile.lock、bundler の Gemfile.lock に相当。 ---- ### 環境管理 (5) ```bash $ pwd /path/to/NewEnv $ ls -l total 24 -rw-r--r-- 1 antimon2 staff 6677 9 13 15:22 Manifest.toml -rw-r--r-- 1 antimon2 staff 108 9 13 15:22 Project.toml $ julia --project ``` ```julia julia> # `]` (NewEnv) pkg> ``` Note: または `env JULIA_PROJECT=$(pwd) julia`。 Project.toml のあるディレクトリでこれを実行すると、初めからそこが `activate` された状態で起動します。 `pipenv run ipython` とか `bundle exec pry` とかに相当。 --- # Julia で <span><!-- .element: style="white-space:nowrap" -->プロジェクト開発</span> <!-- .element: style="font-size:300%" --> ---- ## プロジェクト作成 ```julia (v1.0) pkg> generate MyProject Generating project MyProject: MyProject/Project.toml MyProject/src/MyProject.jl (v1.0) pkg> activate MyProject (MyProject) pkg> status Project MyProject v0.1.0 Status `/path/to/MyProject/Project.toml` (MyProject) pkg> ``` Note: パッケージREPLで `generate HOGE` すると、`HOGE` ディレクトリを生成し、その中に `Peoject.toml` と `src/HOGE.jl` まで自動生成してくれます。 ---- MyProject/Project.toml: <!-- .element: style="text-align:left" --> ```toml authors = ["antimon2 <antimon2@example.com>"] name = "MyProject" uuid = "5232ddb8-b72a-11e8-07f4-2f75dd16c0e1" version = "0.1.0" [deps] ``` Note: 自動生成された `Project.toml` には、プロジェクト名・uuid・著者情報・バージョンが初めから設定されます。パッケージ開発に便利♪ ---- MyProject/src/MyProject.jl: <!-- .element: style="text-align:left" --> ```julia module MyProject greet() = print("Hello World!") end # module ``` Note: 自動生成された `src/MyProject.jl` には、`greet()` 関数の定義("Hello World!" と画面に表示するだけ)が記述されています。これを書き換えてパッケージ(モジュール)を作る。 ---- ```julia # $ julia --project julia> using MyProject [ Info: Precompiling MyProject [5232ddb8-b72a-11e8-07f4-2f75dd16c0e1] julia> MyProject.greet() Hello World! ``` Note: プロジェクト内のモジュールは、そのまま `import`/`using` できます。 初回はこのように precompile が走ります。 ---- ## テスト追加 MyProject/Project.toml(追記): <!-- .element: style="text-align:left" --> ```toml authors = ["antimon2 <antimon2@example.com>"] name = "MyProject" uuid = "5232ddb8-b72a-11e8-07f4-2f75dd16c0e1" version = "0.1.0" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] test = ["Test"] ``` Note: `[extras]` 以下をエディタで追記する(コピペでOK)。 これを自動生成してくれるようになるとウレシイんだけどね…。 ---- MyProject/test/runtests.jl(新規作成): <!-- .element: style="text-align:left" --> ```julia using MyProject using Test @test 1 + 1 == 2 # 作成中のモジュールのテストを書く # : 《以下略》 ``` Note: このファイルも自動生成はしてくれない(現状)ので、自分で作る。 `@test` はテストを実行するマクロ。式(文)の結果が `true` なら何もせず、それ以外なら例外を送出する。 もちろん `@test 1 + 1 == 2` は「とりあえず必ず成功するテストを書いてみた」というサンプル。 ---- ### プロジェクトのテスト実行 ```julia # $ julia --project (MyProject) pkg> test Updating registry at `~/.julia/registries/General` : 《中略》 Testing MyProject Testing MyProject tests passed ``` Note: 先ほどパッケージのテストでは `pkg> test PkgName` のように書いたけれど、`julia --project` で特定の仮想環境(プロジェクト)がアクティベートされた状態の場合、引数無しでそのプロジェクトのテスト(=`(プロジェクトのディレクトリ)/test/runtests.jl`)が実行される。これ便利。 ---- ## プロジェクトの clone ```bash $ git clone http://xxx.xxx/SomeProject.git $ cd SomeProject $ ls -l total 24 -rw-r--r-- 1 antimon2 staff 266 9 14 17:36 Project.toml -rw-r--r-- 1 antimon2 staff 586 9 14 17:36 main.jl drwxr-xr-x 3 antimon2 staff 102 9 14 17:36 src drwxr-xr-x 3 antimon2 staff 102 9 14 17:36 test ``` Note: ここからは、他の人が作ったプロジェクトをローカルにクローンして動作確認・開発する手順です。 git clone したリポジトリの内容に、`Project.toml` はあるけれど `Manifest.toml` はないことに注目! ---- ```jl # $ julia --project (SomeProject) pkg> instantiate Updating registry at `~/.julia/registries/General` Updating git-repo `https://github.com/JuliaRegistries/General.git` Resolving package versions... Updating `Project.toml` [9a3f8284] + Random Updating `Manifest.toml` [9a3f8284] + Random [9e88b42a] + Serialization (SomeProject) pkg> ``` Note: `pkg> instantiate` すると、こんな感じで「そのプロジェクトでの動作に必要なパッケージ類をインストール」してくれます。 この時、リポジトリに `Project.toml` しかなくても、インストール完了時に `Manifest.toml` も作られます。 `pipenv install` とか `bundle install` とかに相当。 ---- ### プロジェクト環境下でのスクリプト実行 SomeProject/main.jl: <!-- .element: style="text-align:left" --> ```jl # samplemain.jl using SomeProject function main() SomeProject.greet() end abspath(PROGRAM_FILE) == @__FILE__() && main() ``` Note: よくある `main.jl` の例(だと思う)。自動生成されたものではなくて自作しました。 `abspath(PROGRAM_FILE) == @__FILE__() && main()` は、Python の ```python if __name__ == '__main__': main() ``` 相当。Rubyなら ```ruby main if $0 == __FILE__ ``` ---- ```bash $ julia --project main.jl Hello World! ``` Note: `julia --project hoge.jl` で、その環境(プロジェクト)をアクティベートした状態でスクリプトを実行できます。 `pipenv run python main.py` とか `bundle exec ruby main.rb` とかに相当。 --- # まとめ ---- + Julia v1.0 のパッケージマネージャ良いよ! + Julia 良いよ! --- ## 参考リンク ---- ### Julia 全般 + [The Julia Language](https://julialang.org/) + [Julia Documentation](https://docs.julialang.org) ---- ### Julia パッケージ・プロジェクト関連 + [Pkg - The Julia Language](https://docs.julialang.org/en/v1/stdlib/Pkg/ "Pkg - The Julia Language") + [JULIA_PROJECT and julia --project | discourse](https://discourse.julialang.org/t/julia-project-and-julia-project/13458 "JULIA_PROJECT and julia --project - Usage - JuliaLang") --- ご清聴ありがとうございます。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully