Julia 중급
Anthony Markham
Quantitative Developer
function add_values(x, y)
x + y
end
add_values(1, 2) # 3
add_values("A", "B")
ERROR: MethodError: no method matching +(::String, ::String)
function add_values(x::String, y::String)
x * y
end
add_values("A", "B") # AB
(x -> x^2 + 3)(2)
7
익명 함수는 단순 계산 외의 용도에도 쓰입니다.
map()은 컬렉션의 각 값을 함수에 적용합니다.
map(x -> 2*x + x^2 + 1, [1, 2, 3])
3-element Vector{Int64}:
4 9 16
map((x, y) -> 2*x + x^2 + 1 + y, [1, 2, 3], [1, 1, 1])
3-element Vector{Int64}:
5 10 17
filter 함수는 조건으로 자료구조를 필터링합니다.
filter에 익명 함수를 쓰면 특정 값을 기준으로 필터링할 수 있습니다.
filter!("Date" => n -> n == "21/01/2022", stock_data)
Row | Date Open High Low Close Adj Close Volume
| String15 Float64 Float64 Float64 Float64? Float64 Int64
<----|-----------------------------------------------------------------------
1 | 21/01/2022 164.42 166.33 162.3 162.41 161.473 122848900
Julia 중급