Slack 目前不付费无法查看历史消息,所以摘抄一些有趣的 Julia 用法。
在回复中更新
Slack 目前不付费无法查看历史消息,所以摘抄一些有趣的 Julia 用法。
在回复中更新
Q: 如何指定一种 Int8
类型的字面量写法?
A: #general@kristofferc
julia> struct I8 end; const i8 = I8(); Base.:*(a::Integer, b::I8) = Int8(a)
julia> 2i8
2
julia> 2i8 |> typeof
Int8
#random@harmen_stoppels
:找到 @which
是在哪里定义的:
julia> @which @which @which
var"@which"(__source__::LineNumberNode, __module__::Module, ex0) in InteractiveUtils at C:\Users\a309\AppData\Local\julias\julia-1.7\share\julia\stdlib\v1.7\InteractiveUtils\src\macros.jl:200
#general@DaveMacMahon
:shell 里的 using
$ using Statistics
using: command not found
$ function using() { julia -i -e "using $@"; }
$ using Statistics
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.7.3 (2022-05-06)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> mean(1:10)
5.5
来点 C 系语法
#random@mose
:
do you miss from C the ability to index arrays as
index[array]
? fear not, Julia’s got you covered:
julia> Base.getindex(n::Int, a::AbstractArray) = a[n]
julia> A = rand(3)
3-element Vector{Float64}:
0.9089330803737465
0.6795629112987226
0.24067299343429482
julia> 2[A]
0.6795629112987226
julia> A[2]
0.6795629112987226
julia> struct Horror{T}
A::T
offset::Int
end
julia> Base.:(*)(A::AbstractArray) = first(A)
julia> Base.:(+)(A::AbstractArray, n::Int) = Horror(A, n)
julia> Base.:(*)(H::Horror) = H.A[firstindex(H.A) + H.offset]
julia> A = rand(3)
3-element Vector{Float64}:
0.9247879490627814
0.559416394770332
0.5540632603178985
julia> *(A)
0.9247879490627814
julia> *(A + 2)
0.5540632603178985