Julia の勉強をした

The Julia Language を勉強した.

なぜ Julia を作ったか Why We Created Julia

Twitter でこのリンクを見かけて変態な人達がいるな〜と思って
勉強してみた.

We want the speed of C with the dynamism of Ruby.

We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab.

We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell.

って,凄いよね.



早いし書きやすいから,
機械学習とかに使われてたりするみたい
どこ見ても Jupyter Notebook で動かすとか書いててうーん…って思った

とりあえず,

brew install julia

でインストールしてやってみた




最初に読みやすそうな Julia By Example を読んだ

f(x) = x + 1

みたいに関数が定義できたり,

3x
x^2

のように数学記号みたいに書けたりする.
他にも,配列の中に配列が入っているものと,2次元以上の配列(行列?)が区別されていたり,
良さげな感じがした.


これを読んだ後に v1.0.0 が出ていることに気がついた.
ということで, v1.0.0 をインストールして,

github.com を読んでみた.

基本的なことは, Julia By Example とほぼ同じだったかな


09. Julia is fast で他の言語と比べてるけど,とても早い!!

Julia hand-written simd.....3.2
Python numpy................3.5
Julia built-in..............3.5
C...........................9.4
Julia hand-written..........9.5
Python built-in...........673.0
Python hand-written.......791.8



round(value, 1)round(value, digits=1) って書かないといけないけど.


Python の関数を呼び出したりできるのも良さげ.
他の言語も呼び出せるのかな?



後は, 10. Multiple dispatch にあるけど

foo(x::String, y::String) = println("My inputs x and y are both strings!")
foo(x::Int, y::Int) = println("My inputs x and y are both integers!")

みたい書いて,引数・型の異なる同名の関数が定義できるのは良いと思う.
さらに,

foo(x, y) = println("I accept inputs of any type!")

って書いて,その他を受けることができるのもとても良いと思った.




String の replace

Julia By Example では,

relipace(string, pattern, replacement)

という感じで書かれていたけど,実際に書いてみると

ERROR: MethodError: no method matching replace(::String, ::String, ::String)
Closest candidates are:
  replace(::String, ::Pair{#s55,B} where B where #s55<:AbstractChar; count) at strings/util.jl:414
  replace(::String, ::Pair{#s52,B} where B where #s52<:Union{Tuple{Vararg{AbstractChar,N} where N}, Set{#s49} where #s49<:AbstractChar, AbstractArray{#s50,1} where #s50<:AbstractChar}; count) at strings/util.jl:419
  replace(::String, ::Pair; count) at strings/util.jl:423
  ...
Stacktrace:
 [1] top-level scope at none:0

とかエラーが吐かれた.

ドキュメント を読むと
Strings · The Julia Language

replace(s::AbstractString, pat=>r; [count::Integer])

変わってるじゃん!

v1.0.0 で String の置換は,

replace("The Ruby Programming Language", "Ruby" => "Julia")

って書くみたい.

割りと書きやすそうだし,早いみたいだし
普段軽く書くときに使っても良いかもしれない.