プログラミング/julia/juliaの文法

(2178d) 更新


プログラミング/julia

ここを見ると大体分かる

変数のスコープを制限する

特に jupyter などで不用意にグローバル変数を作らないようにするために、 try end ブロックを使うと良い気がする。

LANG:julia
import Plots
Plots.gr()
try
  data = rand(10)
  Plots.plot(data)
end

# ここでは data は未定義

gist: https://gist.github.com/osamutake/fbbbd99c8f670335fa55afc0bfaa0438

あーと、これ、内部のエラーを無視しちゃうので良くないな。

もっと良い方法あるかしら。

ネストした内包表現

大きな違いがある。

LANG:julia
#               行          列
In : [(i,j) for i in [1,2], j in [2,3]]
Out: 2×2 Array{Tuple{Int64,Int64},2}:
      (1, 2)  (1, 3)
      (2, 2)  (2, 3)

#               外             中
In : [(i,j) for i in [1,2] for j in [1,2]]
Out: 4-element Array{Tuple{Int64,Int64},1}:
      (1, 2)
      (1, 3)
      (2, 2)
      (2, 3)

行列の書き方

LANG:julia
In : [1 2; 3 4]
Out: 2×2 Array{Int64,2}:
      1  3
      2  4

In : [[1, 2] [3, 4]]
Out: 2×2 Array{Int64,2}:
      1  3
      2  4

In : [[1 2], [2 3]]
Out: 2-element Array{Array{Int64,2},1}:
      [1 2]
      [2 3]

関数の機微

いろいろ勉強した:
http://nbviewer.jupyter.org/gist/osamutake/5b6aa4f2cf74297e1bc4a6b22fd88a22

コメント・質問





Counter: 5660 (from 2010/06/03), today: 1, yesterday: 0