julia-模組Core(第9講)

模組Core
==========================================================================
1.用途:Core語言“內置”的標識符模塊,標識符是核心語言的一部分,而不是庫的一部分。
               每個模塊都隱式指定using Core,因爲沒有這些定義您將無法做任何事情。

==========================================================================
2.函數:

===(x,y) -> Bool
≡(x,y) -> Bool                #在沒有程序可以區分它們的意義上,確定x和y是否相同

a,b= [1, 2], [1, 2]
a == b,a === b,a === a#(true, false, true)
≡(a,b)==(a === b)        # true
==========================================================================
isa(x, type) -> Bool      #確定x是否屬於給定type;可用中綴運算符x isa type
isa(1, Int)==(1 isa Int)   # true

isa(1, Matrix),isa(1, Char), isa(1, Number)#(false, false, true)
==========================================================================
isequal(x, y)# 類似==,除處理浮點數和缺失值:NaN=NaN,-0.0!=0.0,missing=missing
                   # 標量,int數組直接用==;自定義時必須暗示hash(x) == hash(y)

[1., NaN] == [1., NaN],isequal([1., NaN], [1., NaN])# (false, true)
(0.0 == -0.0),isequal(0.0, -0.0)# (true, false)
==========================================================================
isequal(x)
# 創建一個將其參數與xusing 比較的函數isequal,即一個等效於的函數y -> isequal(y, x)。

返回的函數類型爲Base.Fix2{typeof(isequal)},可用於實現專門的方法。
==========================================================================
isless(x, y)# 根據固定順序測試是否x小於y;比較關係可傳遞isless(x, y) && isless(y, z)==>isless(x, z)

 x,y=[1,2],[1,3]
 isless(x,y)# true
==========================================================================
ifelse(condition::Bool, x, y)# 返回x如果condition是true,否則返回y

ifelse(1 > 2, 1, 2)# 2
==========================================================================
typeassert(x, type)# 拋出一個TypeError除非x isa type;x::type調用此函數

typeassert(2.5, Int)#TypeError
==========================================================================
typeof(x)                           # 獲取x數據類型

a = 1//2;typeof(a)             # Rational{Int64}
M = [1 2; 3.5 4];typeof(M)# Array{Float64,2}
==========================================================================
tuple(xs...)                            #構造元組
ntuple(f::Function, n::Integer)#創建長度爲n的元組,將每個元素計算爲f(i),i爲元素索引

tuple(1, 'a', pi)   # (1, 'a', π)
ntuple(i -> 2*i, 4)# (2, 4, 6, 8)
==========================================================================
objectid(x)#獲取x對象哈希值

x,y=1,1
objectid(x)==objectid(y)#如x === y   UInt64 0xa6165bea1dfbfae6
==========================================================================
hash(x[, h::UInt])計算一個整數哈希碼,isequal(x,y)意味hash(x)==hash(y);參數h是要與結果混合的哈希碼

hash(a)    # 0x02011ce34bce797f
objectid(a)# 0xa6165bea1dfbfae6
==========================================================================

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章