2016년 8월 2일 화요일

Julia 연습 8

In [ ]:
#
# 나는 julia 를 사랑하는가.
#
In [1]:
z = begin
    x = 1
    y = 2
    x + y
end
Out[1]:
3
In [2]:
x
Out[2]:
1
In [3]:
y
Out[3]:
2
In [ ]:
# x 와 y 가 현재 범위에 존재한다.
# 이것은 내가 원하는것이 아니다.
In [4]:
z = (a = 1; b = 2; a + b)
Out[4]:
3
In [5]:
a
Out[5]:
1
In [6]:
b
Out[6]:
2
In [ ]:
# a 와 b 가 현재 범위에 존재한다.
# 이것은 내가 원하는것이 아니다.
In [8]:
function test( x, y)
    if x < y
        println("x 가 y 보다 작다")
    elseif x > y
        println("x 가 y 보다 크다")
    else
        println("x 가 y 와 같다")
    end
end
Out[8]:
test (generic function with 1 method)
In [9]:
test(1, 2)
x 가 y 보다 작다
In [10]:
test(2, 1)
x 가 y 보다 크다
In [11]:
test(1, 1)
x 가 y 와 같다
In [13]:
# if 조건문의 값이 boolean 값이 아니면 이런꼴을 보게된다.
# Bool 타입이 아니면 안된다.



if 1
    println("true")
end
LoadError: TypeError: non-boolean (Int64) used in boolean context
while loading In[13], in expression starting on line 1
In [14]:
typeof( 1 < 2)
Out[14]:
Bool
In [15]:
v(x) = (println(x); x)
Out[15]:
v (generic function with 1 method)
In [16]:
# ternary operator
# 삼항 연산자.
# C 언어와 같다.


1 < 2 ? v("yes") : v("no")
yes
Out[16]:
"yes"
In [17]:
1 > 2 ? v("yes") : v("no")
no
Out[17]:
"no"
In [18]:
# short-circuit evaluation


t(x) = (println(x); true)
Out[18]:
t (generic function with 1 method)
In [19]:
f(x) = (println(x); false)
Out[19]:
f (generic function with 1 method)
In [20]:
t(1) && t(2)
1
2
Out[20]:
true
In [21]:
t(1) && f(2)
1
2
Out[21]:
false
In [23]:
# t(2) 가 평가되지 않는다.

f(1) && t(2)
1
Out[23]:
false
In [24]:
# f(2) 가 평가되지 않는다.

f(1) && f(2)
1
Out[24]:
false
In [25]:
# t(2) 가 평가되지 않는다.

t(1) || t(2)
1
Out[25]:
true
In [26]:
t(1) || f(2)
1
Out[26]:
true
In [27]:
f(1) || t(2)
1
2
Out[27]:
true
In [28]:
f(1) || f(2)
1
2
Out[28]:
false
In [29]:
# short-circuit evaluation 을
# if 문 대용으로 쓸수 있다.


function factorial(n::Int)
    n >= 0 || error("n 은 반드시 양수이어야 한다")
    n == 0 && return 1
    n * factorial(n-1)
end
Out[29]:
factorial (generic function with 1 method)
In [30]:
factorial(5)
Out[30]:
120
In [31]:
factorial(0)
Out[31]:
1
In [32]:
factorial(-1)
LoadError: n 은 반드시 양수이어야 한다
while loading In[32], in expression starting on line 1

 in factorial at In[29]:2
In [33]:
# short-circuit evaluation 의 부작용이 없는 
# boolean operation 을 쓰려면
# bitwise boolean operator 를 쓰면된다.

f(1) & t(2)
1
2
Out[33]:
false
In [34]:
t(1) | t(2)
1
2
Out[34]:
true
In [35]:
# 반드시 boolean value 이어야 한다.

1 && true
LoadError: TypeError: non-boolean (Int64) used in boolean context
while loading In[35], in expression starting on line 1
In [36]:
# 조건문의 마지막 표현식은
# boolean value 가 아니어도 된다.
# 어떤 식을 써도 된다.
# 상황에따라 리턴값으로 사용된다.


true && (x = rand(2,2))
Out[36]:
2x2 Array{Float64,2}:
 0.424187  0.493812
 0.663082  0.711009
In [37]:
false && (x = rand(2,2))
Out[37]:
false
In [38]:
# while 문을 보자.

i = 1;

while i <= 5
    println(i)
    i += 1
end
1
2
3
4
5
In [43]:
# for 문을 보자.

for k = 1:5
    println(k)
end
1
2
3
4
5
In [44]:
# k 는 현재 범위에 존재하지 않는다.
# 이것이 내가 원하는것이다.

k
LoadError: UndefVarError: k not defined
while loading In[44], in expression starting on line 3
In [46]:
# for 문에서 = 대신 in 을 쓰면
# 가독성이 좋은가.


for i in [1,4,0]
    println(i)
end
1
4
0
In [47]:
for i in 1:5
    println(i)
end
1
2
3
4
5
In [51]:
# break


i = 1;
while true
    println(i)
    if i >= 5
        break
    end
    i += 1
end
1
2
3
4
5
In [52]:
# break
# loop 를 즉시 끝내버린다.


for i = 1:1000
    println(i)
    if i >= 5
        break
    end
end
1
2
3
4
5
In [53]:
# continue
# loop 를 끝내지는 않고, 다음 반복으로 넘어간다.


for i = 1:10
    if i % 3 != 0
        continue
    end
    println(i)
end
3
6
9
In [54]:
# multiple nested for loop
# 여러개가 중첩된 for 문
# 이렇게 이쁘게 쓸수있다.
# cartesian product


for i = 1:2, j = 3:4
    println((i, j))
end
(1,3)
(1,4)
(2,3)
(2,4)
In [55]:
# Exception
# 예외

type MyCustomException <: Exception end
In [56]:
typeof(MyCustomException)
Out[56]:
DataType
In [57]:
f(x) = x >= 0 ? exp(-x) : throw(DomainError())
Out[57]:
f (generic function with 1 method)
In [58]:
f(1)
Out[58]:
0.36787944117144233
In [59]:
f(-1)
LoadError: DomainError:
while loading In[59], in expression starting on line 1

 in f at In[57]:1
In [60]:
throw(UndefVarError(:x))
LoadError: UndefVarError: x not defined
while loading In[60], in expression starting on line 1
In [1]:
type MyUndefVarError <: Exception
    var::Symbol
end
In [2]:
Base.showerror(io::IO, e::MyUndefVarError) = print(io, e.var, " <-- 이게 없다 이새끼야");
In [3]:
throw(MyUndefVarError(:x))
LoadError: x <-- 이게 없다 이새끼야
while loading In[3], in expression starting on line 1
In [6]:
error("죽어봐야 지옥맛을 알겠는가?")
LoadError: 죽어봐야 지옥맛을 알겠는가?
while loading In[6], in expression starting on line 1

 in error at error.jl:21
In [7]:
info("밥은먹고다니는가"); 1+1
INFO: 밥은먹고다니는가
Out[7]:
2
In [8]:
warn("정신나갔는가"); 1+1
WARNING: 정신나갔는가
Out[8]:
2
In [9]:
error("매운맛한번볼랑가"); 1+1
LoadError: 매운맛한번볼랑가
while loading In[9], in expression starting on line 1

 in error at error.jl:21
In [16]:
f(x) = try
    info("밥은먹고다니는가")
    sqrt(x)    
catch
    warn("정신나갔는가")
    sqrt(complex(x, 0))
end
Out[16]:
f (generic function with 1 method)
In [17]:
f(1)
INFO: 밥은먹고다니는가
Out[17]:
1.0
In [18]:
f(-1)
INFO: 밥은먹고다니는가
WARNING: 정신나갔는가
Out[18]:
0.0 + 1.0im
In [29]:
sqrt_second(x) = try
    
    sqrt(x[2])
    
    catch y
    if isa(y, DomainError)
        
        info("밥은먹고다니는가")
        sqrt(complex(x[2], 0))
        
    elseif isa(y, BoundsError)
        
        warn("정신나갔는가")
        
        try
            sqrt(x)
        catch
            error("죽어봐야정신차리것는가")
        end

    end
end
Out[29]:
sqrt_second (generic function with 1 method)
In [30]:
sqrt_second([1 4])
Out[30]:
2.0
In [31]:
sqrt_second([1 -4])
INFO: 밥은먹고다니는가
Out[31]:
0.0 + 2.0im
In [32]:
sqrt_second(9)
WARNING: 정신나갔는가
Out[32]:
3.0
In [33]:
sqrt_second(-9)
WARNING: 정신나갔는가
LoadError: 죽어봐야정신차리것는가
while loading In[33], in expression starting on line 1

 in sqrt_second at In[29]:16
In [34]:
warn("얼어죽을")
WARNING: 얼어죽을
In [36]:
try error() catch warn("얼어죽을") end
WARNING: 얼어죽을
In [39]:
try 
    error()
catch
    warn("얼어죽을")
finally
    warn("정신은있는가")
end
WARNING: 얼어죽을
WARNING: 정신은있는가
In [40]:
# Task
# Coroutine

function producer()
    produce("start")
    for n=1:4
        produce(2n)
    end
    produce("stop")
end;
In [41]:
p = Task(producer);
In [42]:
consume(p)
Out[42]:
"start"
In [43]:
consume(p)
Out[43]:
2
In [44]:
consume(p)
Out[44]:
4
In [45]:
consume(p)
Out[45]:
6
In [46]:
consume(p)
Out[46]:
8
In [47]:
consume(p)
Out[47]:
"stop"
In [48]:
consume(p)
Out[48]:
()
In [49]:
consume(p)
Out[49]:
()
In [50]:
for x in Task(producer)
    println(x)
end
start
2
4
6
8
stop

Firefly Algorithms

firefly algorithm 001 Firefly Algorithms ¶ 반딧불 알고리즘 번역 요약 ¶ References [1] X. S. Y...