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

4、global 、local
具体参考本博文。

否则容易报“变量未定义的错误!”

5、Statistics

Statistics.mean — Function.
Statistics.std — Function.

在用mean、std等统计函数时,现在需要

using Statistics  # 不加会报错

6、关于 ?

julia> a =true   # bool
true

julia> a =true ? b=1 : b=2
1
julia> a,b  
(1, 1)
julia> (c=true) ? d= 1 : d=2
1
julia> c
true

julia> typeof(a)
Int64

两方面:
(1) ? ,:之间要有标准的空隔,不能相连。
(2) a 被改变;需要加括号;

二、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.59977    ⋅
 0.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

八、isdefined 与@isdefined

在0.6和1.0中,isdefined的用法有所不同。

# 1.0 
julia> a =1;
julia> @isdefined a   # 0.6 isdefined(:a),这个用法在1.0中不行了,只能改成@isdefined
true

julia> b = 1//2;

julia> isdefined(b,1) # 1是否出现在b中?
true

julia> isdefined(b,2) #2是否出现在b中?
true

julia> isdefined(b,3) #
false

julia> isdefined(b,:num) # num类型是否出现在b中?
true

九、 启动时自动运行某程序有变化

(一)在0.6或此前版本
1、找到homedir()目录

  julia> homedir()
 "C:\\Users\\Administrator"

2、写一个名为:.juliarc.jl 的文件,放在"C:\Users\Administrator"。其内容如下:

 # .juliarc.jl文件内容 
 println("hello julia.....") # 表示启动JULIA时,自动运行这个命令
 include("D:\\strategyJulia\\addpath.jl"); # 表示启动JULIA时,自动启动这个文件 !

在1.0版本,改成了用startup.jl. 不仅名字换了,而且连放的位置也换了。

(二)1.0 版本
(1)找到homedir()里对应的目录, 这里定义为“~”
(2)在~\.julia\中,手工创建config文件夹。【说明一下,原来并没有config文件夹】;
(3)在~\.julia\config\中创建 startup.jl文件。
即: 绝对路径为:~\.julia\config\startup.jl

println("welcome julialang world !"); # startup.jl

这里写图片描述

其它,后面再补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值