蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

元组实例:合并固定字段与可选配置

JULIAJulia 1.10.13 官方便携版,Windows x64,本机隔离目录tuples.jl
tuples.jl51 行
include(joinpath(@__DIR__, "observations.jl"))

station = (7,"Hill",2.5)
nested = ((7,11),(13,17))
probe("tuple_index", (station,nested),
      (station[2:end], nested[1], nested[1][2], length(station), typeof(station)),
      (("Hill",2.5),(7,11),11,3,Tuple{Int,String,Float64}))
probe("tuple_literals", "(7,), (7), ()",
      (typeof((7,)), (7) isa Int, isempty(()), [7,11] isa Vector),
      (Tuple{Int},true,true,true))

mixed = [7,"Hill",2.5]
probe("tuple_type_comparison", (station,mixed), (typeof(station),eltype(mixed)), (Tuple{Int,String,Float64},Any))

mutable_member = ([7,11],)
mutable_member[1][1] = 31
vector = [7,11]
vector[2] = 41
probe("tuple_mutability", ((7,11),mutable_member,vector),
      (outcome(() -> setindex!((7,11),5,2)),mutable_member[1][1],vector[2]),
      ("MethodError",31,41))
a,b = (7,11)
a,b = b,a
_, ignored_first = [13,17]
factors() = (2,3)
probe("tuple_destructure", ((7,11),[13,17]), (a,b,ignored_first,factors()), (11,7,17,(2,3)))

fields = (:north,:south)
by_names = NamedTuple{fields}((7,11))
literal = (north=7,south=11,ready=true)
n,s,ok = literal
TypedReading = @NamedTuple{north::Float32, code::String}
converted = TypedReading((7,"Hill"))
probe("tuple_named_construct", (fields,(7,11),literal),
      (by_names.north,by_names.south,(n,s,ok),literal.ready,typeof(converted),converted.north),
      (7,11,(7,11,true),true,TypedReading,7.0f0))
merged = merge(literal,(south=31,west=41))
probe("tuple_named_merge", (literal,(south=31,west=41)), merged, (north=7,south=31,ready=true,west=41))

function report(site, period, sample; limit=20, unit="visits", ready=false)
    return (site,period,sample,limit,unit,ready)
end
opts = (limit=31,ready=true)
probe("tuple_keywords", (opts,45),
      (report("Hill",2,7;opts...),report("Hill",2,7;limit=45,opts...),
       report("Hill",2,7;opts...,limit=45)),
      (("Hill",2,7,31,"visits",true),("Hill",2,7,31,"visits",true),("Hill",2,7,45,"visits",true)))
table = Dict((7,11)=>"tuple",[13,17]=>"array")
probe("tuple_dictionary", ((7,11),[13,17]),
      (table[(7,11)],table[[13,17]],haskey(table,(7,13))), ("tuple","array",false))
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录

入口文件:projects/julia-containers-and-numbers/tuples.jl

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 tuples.jl,或下载右侧课程工程包。
  2. 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Julia

课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip

下载课程关联工程包:Julia

课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a207.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习