iterable - julia static field of composite type -


in julia allowed create & use static fields? let me explain problem simplified example. let's have type:

type foo   bar::dict()   baz::int   qux::float64   function foo(fname,baz_value,qux_value)      dict = jld.load(fname)["dict"] # simple dictionary loading special file      new(dict,baz_value,quz_value)   end end 

now, can see, load dictionary jld file , store foo type other 2 variables baz , qux_value. now, let's create 3 foo object type.

vars = [ foo("mydictfile.jld",38,37.0) i=1:3] 

here, can see, of foo objects load same dictionary. quite big file (~10gb)and don't want load many times. so, ask that, there way in julia that, load once , of there 3 types can reach it? (that's way use static keyword inside question)

for such simple question, approach might silly, next step, make foo type iterable , need use dictionary inside next(d::foo, state) function.

edit

actually, i've found way right now. want ask whether correct or not.

rather giving file name foo constructor, if load dictionary variable before creating objects , give same variable of constructors, guess constructors create pointer same dictionary rather creating again , again. right ?

so, modified version that:

 dict = jld.load("mydictfile.jld")["dict"]  vars = [ foo(dict,38,37.0) i=1:3] 

by way,i still want hear if same thing inside foo type (i mean constructor of it)

you making type "too special" adding inner constructor. julia provides default constructors if not provide inner constructor; these fill in fields in object of new type.

so can like:

immutable foo{k,v}     bar::dict{k,v}     baz::int     qux::float64 end  dict = jld.load("mydictfile.jld")["dict"] vars = [foo(dict, i, i+1) in 1:3] 

note syntax error include parentheses after dict in type definition.

the {k,v} makes foo type parametric, can make different kinds of foo type, different dict types inside, if necessary. if use single type of dict, give more efficient code, since type parameters k , v inferred when create foo object. see julia manual: http://docs.julialang.org/en/release-0.5/manual/performance-tips/#avoid-fields-with-abstract-containers

so can try code without having jld file available (as not, example):

julia> dict = dict("a" => 1, "b" => 2)  julia> vars = [foo(dict, i, float64(i+1)) in 1:3] 3-element array{foo{string,int64},1}:  foo{string,int64}(dict("b"=>2,"a"=>1),1,2.0)  foo{string,int64}(dict("b"=>2,"a"=>1),2,3.0)  foo{string,int64}(dict("b"=>2,"a"=>1),3,4.0) 

you can see indeed same dictionary (i.e. reference stored in type object) modifying 1 of them , seeing others change, i.e. point same dictionary object:

julia> vars[1].bar["c"] = 10 10  julia> vars 3-element array{foo{string,int64},1}:  foo{string,int64}(dict("c"=>10,"b"=>2,"a"=>1),1,2.0)  foo{string,int64}(dict("c"=>10,"b"=>2,"a"=>1),2,3.0)  foo{string,int64}(dict("c"=>10,"b"=>2,"a"=>1),3,4.0) 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -