Julia
set
setdiff(Set([1, 2]), Set([0])) # => {1, 2}
symdiff(Set([1, 2]), Set([0])) # => {1, 2, 0}enum
# https://riptutorial.com/ja/julia-lang/example/23840/%E5%88%97%E6%8C%99%E5%9E%8B%E3%81%AE%E5%AE%9A%E7%BE%A9
import Base.*
@enum Suit ♣ ♦ ♥ ♠
@enum Rank ace=1 two three four five six seven eight nine ten jack queen king
struct Card
rank::Rank
suit::Suit
end
r::Int * s::Suit = Card(Rank(r), s)
Int(♦)
# => 1
string(Rank(4))
# => four
10♣
# => Card(ten::Rank = 10, ♣::Suit = 0)@code_llvm
@code_llvm sq(4)
# ; @ REPL[1]:1 within `sq'
# define i64 @julia_sq_12223(i64) {
# top:
# ; ┌ @ intfuncs.jl:243 within `literal_pow'
# ; │┌ @ int.jl:54 within `*'
# %1 = mul i64 %0, %0
# ; └└
# ret i64 %1
# }@code_native
Native codeが見れる
sq(x) = x * x
@code_llvm sq(4)
# .section __TEXT,__text,regular,pure_instructions
# ; ┌ @ REPL[1]:1 within `sq'
# ; │┌ @ intfuncs.jl:243 within `literal_pow'
# ; ││┌ @ REPL[1]:1 within `*'
# imulq %rdi, %rdi
# ; │└└
# movq %rdi, %rax
# retq
# nopl (%rax,%rax)Pkg
- install deps
julia>]
Pkg> activate .
Pkg> instantiate- remove package dir
julia> rm(Pkg.dir("CUDA"); recursive=true)
julia> Pkg.resolve()AST
ast = :(((x * x) * x) * (y * y))
# Expr has
# - head::Symbol
# - args::Array{Symbol|Expr}
dump(ast)
# Expr
# head: Symbol call
# args: Array{Any}((3,))
# 1: Symbol *
# 2: Expr
# head: Symbol call
# args: Array{Any}((3,))
# 1: Symbol *
# 2: Expr
# head: Symbol call
# args: Array{Any}((3,))
# 1: Symbol *
# 2: Symbol x
# 3: Symbol x
# 3: Symbol x
# 3: Expr
# head: Symbol call
# args: Array{Any}((3,))
# 1: Symbol *
# 2: Symbol y
# 3: Symbol y
dump(ast.head == Symbol("call"))
# true
dump(ast.args[2].args[2])
# Expr
# head: Symbol call
# args: Array{Any}((3,))
# 1: Symbol *
# 2: Symbol x
# 3: Symbol x
# String to AST
@show Meta.parse("(x * x * x) * (y * y)")Distributed
inbox
- 2022-05-30 行列とかの表示は
display(a)で
型
- 動的型付け, 多相的
- 実行時型
::型表明
function f(x)::Float64
return 1.0
end
- 抽象型
abstract type Hoge end
- extends subtype
abstract type HogeInt <: Int64 end
- トップ型:
Any. ボトム型Unit{} - 原始型
primitive type F16 <: AbstractFloat 16 end
- 複合型(不変)
struct Foo
bar::Int
baz
end
foo = Foo(1, 2)
fieldnames(foo) # :bar, :baz- フィールドのないstructはシングルトン
struct NoFields end
NoFields() == NoFields()- 可変複合型
mutable struct MutableFoo
bar::Int
end-
合併型
IntOrString = Union{Int, AbstractString}
-
パラメータ型
struct Point{T}
x::T
y::T
end- Juliaの型パラメータは不変
Float64 < Real == truePoint{Float64} <: Point{Real} == false
function norm(p::Point{<:Real})
sqrt(p.x ^ 2 + p.y ^ 2)
end
abstract type Pointy{T<:Real} end
struct Point2{T<:Real} <: Pointy{T}
x::T
end- 値型
struct Val{T} end
- null許容
a::Nullable{Int} = Nullable(1)get(a)