Julia:1.0与0.6 的几点不同

在从0.6升1.0过程中,还是有不少的变化。就我库存代码,在升级过程中,现在简单总结一下:

一、Julia的标准库变得更轻

有些在1.0以下版本的库在1.0标准库内和Julia一起发布,但是在1.0版本内,不再会作为语言的基础依赖,但用之前需要显性引用或导入。

比如Pkg,Printf
1、using Pkg

using Pkg
Pkg.add("HDF5")

2、using Printf

using  Printf
d12 = @sprintf("%0.1f",maxProfitAmountOfWeekTrade)

3、using Dates

using  Dates
function getDateTime(dt::SubString{T},tm::SubString{T}) where T <: String
    # 两个substring
    ddate = getDate(dt);
    (nhour,nminute,nsecond) = getTupleTime(tm)
    nyear = year(ddate)
    nmonth = month(ddate)
    nday = day(ddate)
    return Dates.DateTime(nyear,nmonth,nday,nhour,nminute,nsecond)//原来直接用Dates.DateTime就行了。
end

二、Atom 上有一些变化

原来在装好Atom,julia0.6后,在repl中 Pkg.add(“Atom”),后面在Atom中,

原来装:ink, julia-client两个主要的库。
现在变成装:ink, uber-Juno两个库。

在升1.0的前几天,还有各种装不上去的情况。特别是在windows环境中。不过,还好,折腾折腾就好了,好事多磨呀

三、where强制使用了

原来可以:

function getDateTime{T <: String}(dt::SubString{T},tm::SubString{T}) 
    # 两个substring
    ddate = getDate(dt);
    (nhour,nminute,nsecond) = getTupleTime(tm)
    nyear = year(ddate)
    nmonth = month(ddate)
    nday = day(ddate)
    return Dates.DateTime(nyear,nmonth,nday,nhour,nminute,nsecond)
end

现在必须使用where了:

function getDateTime(dt::SubString{T},tm::SubString{T}) where T <: String
    # 两个substring
    ddate = getDate(dt);
    (nhour,nminute,nsecond) = getTupleTime(tm)
    nyear = year(ddate)
    nmonth = month(ddate)
    nday = day(ddate)
    return Dates.DateTime(nyear,nmonth,nday,nhour,nminute,nsecond)
end

四、repl上的一些变化

原来的一些操作有一些变化。主要是有一个全新的内建包管理器。

当你按下“]”键后,就会发生变化:

这里写图片描述

你可以直接add, rm,build…..等操作了,不需要,Pkg.add(),…..

退出: 请按 ctrl +c

这里写图片描述

五、Missing
missing 将替代 Julia在0.4时推出的Nullable类型。

julia> a =Missing
Missing

julia> typeof(a)
DataType

julia> ["Julia",1, missing]
3-element Array{Any,1}:
  "Julia"
 1
  missing

julia> a=[1, missing]
2-element Array{Union{Missing, Int64},1}:
 1
  missing

julia> a=["Julia", missing]
2-element Array{Union{Missing, String},1}:
 "Julia"
 missing

julia> 100 + missing
missing

julia> mean([99, missing, 2])
missing

julia> sum(skipmissing([1, missing, 2])) # skipmissing 跳过missing
3

六、广播
已经成为Julia 1.0 的核心。

julia> @time a=([1, 2, 3] .+ [10 20 30 40]) ./ 10
  0.100496 seconds (443.97 k allocations: 22.113 MiB, 4.89% gc time)
3×4 Array{Float64,2}:
 1.1  2.1  3.1  4.1
 1.2  2.2  3.2  4.2
 1.3  2.3  3.3  4.3

julia> @time b=([1, 2, 3] + [10 20 30 40]) / 10
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape at .\indices.jl:129 [inlined]
 [2] promote_shape at .\indices.jl:125 [inlined]
 [3] promote_shape(::Array{Int64,1}, ::Array{Int64,2}) at .\indices.jl:120
 [4] +(::Array{Int64,1}, ::Array{Int64,2}) at .\arraymath.jl:45
 [5] top-level scope at util.jl:156

julia> @time b=([1, 2, 3] + [10 20 30 40]) ./ 10
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape at .\indices.jl:129 [inlined]
 [2] promote_shape at .\indices.jl:125 [inlined]
 [3] promote_shape(::Array{Int64,1}, ::Array{Int64,2}) at .\indices.jl:120
 [4] +(::Array{Int64,1}, ::Array{Int64,2}) at .\arraymath.jl:45
 [5] top-level scope at util.jl:156

julia> @time b=([1, 2, 3] + [10 20 30 40])
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape at .\indices.jl:129 [inlined]
 [2] promote_shape at .\indices.jl:125 [inlined]
 [3] promote_shape(::Array{Int64,1}, ::Array{Int64,2}) at .\indices.jl:120
 [4] +(::Array{Int64,1}, ::Array{Int64,2}) at .\arraymath.jl:45
 [5] top-level scope at util.jl:156

julia> using LinearAlgebra

julia> d = Diagonal(1:3)
3×3 Diagonal{Int64,UnitRange{Int64}}:
 1  ⋅  ⋅
 ⋅  2  ⋅
 ⋅  ⋅  3

julia> e=d ./ 10
3×3 Diagonal{Float64,Array{Float64,1}}:
 0.1   ⋅    ⋅
  ⋅   0.2   ⋅
  ⋅    ⋅   0.3

julia> t = d .+ LowerTriangular(rand(3,3))
3×3 LowerTriangular{Float64,Array{Float64,2}}:
 1.11143    ⋅         ⋅
 0.920267  2.599770.484313  0.707666  3.56498

julia> t .+ 100
3×3 Array{Float64,2}:
 101.111  100.0    100.0
 100.92   102.6    100.0
 100.484  100.708  103.565

七、可命名元组

变得更加友好,更加轻量级。据说,性能还不错,待测。

julia> using Dates

julia> t =(code ="600036.sh",date =Date(2018,8,18),close =10.86,high =10.99)
(code = "600036.sh", date = 2018-08-18, close = 10.86, high = 10.99)

julia> t.code
"600036.sh"

julia> t.date
2018-08-18
julia> t.close
10.86

八、其它,后面再补充。

猜你喜欢

转载自blog.csdn.net/wowotuo/article/details/81750466