Skip to content

Instantly share code, notes, and snippets.

@informationsea
Last active August 29, 2015 14:05
Show Gist options
  • Save informationsea/d496563aa3057673a098 to your computer and use it in GitHub Desktop.
Save informationsea/d496563aa3057673a098 to your computer and use it in GitHub Desktop.
Juliaを使ってみる

Juliaをはじめてみる

計算をしてみる

1 + 2
3
log(10)
2.302585092994046
sqrt(36)
6.0

変数を使ってみる

a = 12
12
b = a/2
6.0

組み込みの変数

pi
π = 3.1415926535897...
e
e = 2.7182818284590...

変数の型

typeof(1)
Int64
typeof(1.0)
Float64
WORD_SIZE
64
typeof(0x01)
Uint8
typeof(0xffff)
Uint16

虚数

1 + 2im
1 + 2im
(1 + 2im)*(2 - 3im)
8 + 1im

文字列

typeof('x')
Char
typeof("aaa")
ASCIIString (constructor with 2 methods)
"ABCD"[2]
'B'

文字列の中に変数が埋め込める

"ABC$(a)D"
"ABC12D"
"TEST$(1+2)"
"TEST3"

正規表現

ismatch(r"\d{4}-\d\d-\d\d", "2014-08-31")
true
match(r"(\d{4})-(\d\d)-(\d\d)", "2014-08-31")
RegexMatch("2014-08-31", 1="2014", 2="08", 3="31")

関数

function f(x, y)
    x + y
end

f(1, 3)
4
g(x, y) = x + y
g(1,2)
3
+(1,2,3)
6
h(x ,y) = x+y, x*y
h(2,3)
(5,6)

無名関数

(x -> x^2 + 2x - 1)(2)
7

関数その他

function pp(x, y=10)
    return x * y
end

pp(2)
20

キーワード付き引数

function test1(x, y; a="test2", b="test2")
    print("$x, $y, $a, $b\n")
end
test1(1,2)
test1(3,2, b="kk")
1, 2, test2, test2
3, 2, test2, kk

Block syntax

map(x->begin
    if x > 10
        x^3
    else
        x^2
        end
    end
, [2,10,20])
3-element Array{Int64,1}:
    4
  100
 8000
map([2,10,20]) do x
    if (x > 10)
        x ^ 3
    else
        x^2
    end
end
3-element Array{Int64,1}:
    4
  100
 8000

制御構文

http://docs.julialang.org/en/release-0.3/manual/control-flow/ から

複合表現

z = begin
    x = 1
    y = 2
    x + y
end
3
z = (x = 1; y = 2; x+y)
3

条件分岐

function test(x)
    if x < 10
        println("x is less than 10")
    elseif x > 10
        println("x is greater than 10")
    else
        println("x is 10")
    end
end

test(10)
test(11)
test(9)
x is 10
x is greater than 10
x is less than 10
is_greater_than_10(x) = x > 10 ? true : false
println(is_greater_than_10(10))
println(is_greater_than_10(11))
false
true
t(x) = (print("$x "); true)
f(x) = (print("$x "); false)

println(t(1) && t(2))
println(t(1) || t(2))
println(f(1) && t(2))
println(f(1) || t(2))
1 2 true
1 true
1 false
1 2 true

ループ

i = 1
while i < 5
    println(i)
    i += 1
end
1
2
3
4
for j = 1:5
    println(j)
end
1
2
3
4
5
j
j not defined
while loading In[18], in expression starting on line 1
i = 1
while true
    println(i)
    if i >= 5
        break
    end
    i += 1
end
1
2
3
4
5

エラー構文

lookup_or_0(x, i) = begin
    try
        x[i]
    catch e
        if isa(e, BoundsError)
            warn("Out of index")
            0
        end
    end
end

println(lookup_or_0([1,2,3], 5))
println(lookup_or_0([1,2,3], 1))
println(lookup_or_0([1,2,3], 3))
WARNING: Out of index
0
1
3

タスク

function test2()
    produce("start")
    for i = 1:3
        produce(i)
    end
    produce("stop")
end

p = Task(test2)
for i = 1:5
    println(consume(p))
end
start
1
2
3
stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment