朱莉娅(Julia):解开风格 [英] Julia: unpack in style

查看:84
本文介绍了朱莉娅(Julia):解开风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以打开一个元组的包装.我正在尝试编写一个函数(或宏),该函数将从类型构造器Parameters()的实例中解压缩这些子集.也就是说,我知道该怎么做:

I can unpack a tuple. I'm trying to write a function (or macro) that would unpack a subset of these from an instance of the type-constructor Parameters(). That is, I know how to do:

a,b,c = unpack(p::Parameters)

但是我想做这样的事情:

But I would like to do something like this:

b,c = unpack(p::Parameters, b,c)

或者甚至更懒惰:

unpack(p::Parameters, b, c)

这是为了避免编写类似的内容:

This is to avoid writing things like:

function unpack_all_oldstyle(p::Parameters)
    a=p.a; b=p.b; c=p.c; ... z=p.z;
    return a,b,c,...,z
end

我的方法有问题,但希望有个解决方法.

There's something wrong with my approach, but hopefully there is a fix.

如果我的问题的措词不清楚,我完全是无知的.我在这里阅读了有关打开省略号的信息: how-to-pass-tuple-as-功能参数

In case it wasn't clear from the wording of my question, I'm a total ignoramus. I read about unpacking the ellipsis here: how-to-pass-tuple-as-function-arguments

    workspace()

    "module UP tests Unpacking Parameters"
    module UP

    "this type declaration initiates a constructor function"
    type Parameters
      a::Int64
      b::Int64
      c::Int64
    end

    "this function sets default parameters and returns a tuple of default values"
    function Parameters(;
      a::Int64 = 3,
      b::Int64 = 11,
      c::Int64 = 101
      )
      Parameters(a, b, c)
    end

    "this function unpacks all parameters"
    function unpack_all(p::Parameters)
        return p.a, p.b, p.c
    end

    "this function tests the unpacking function: in the body of the function one can now refer to a rather than p.a : worth the effort if you have dozens of parameters and complicated expressions to compute, e.g. type (-b+sqrt(b^2-4*a*c))/2/a instead of (-p.b+sqrt(p.b^2-4*p.a *p.c))/2/p.a"
    function unpack_all_test(p::Parameters)
        a, b, c = unpack_all(p)
        return a, b, c
    end

    """
    This function is intended to unpack selected parameters. The first, unnamed argument is the constructor for all parameters. The second argument is a tuple of selected parameters.
    """
    function unpack_selected(p::Parameters; x...)
        return p.x
    end

    function unpack_selected_test(p::Parameters; x...)
        x = unpack_selected(p, x)
        return x
    end

    export Parameters, unpack_all, unpack_all_test, unpack_selected, unpack_selected_test

    end

    p = UP.Parameters() # make an instance
    UP.unpack_all_test(p)
    ## (3,11,101) ## Test successful

    UP.unpack_selected_test(p, 12)
    ## 12  ## intended outcome

    UP.unpack_selected_test(p, b)
    ## 11  ## intended outcome

    UP.unpack_selected_test(p, c, b, a)
    ## (101,11,3)  ## intended outcome

推荐答案

已经存在一个: Parameters.jl .

julia> using Parameters

julia> type Params
           a::Int64
           b::Int64
           c::Int64
       end

julia> @unpack a, c = Params(1,2,3)
Params(1,2,3)

julia> a,c
(1,3)

julia> @with_kw type Params
           a::Int64 = 3
           b::Int64 = 11
           c::Int64 = 101
       end
julia> @unpack c,b,a = Params()
Params
  a: Int64 3
  b: Int64 11
  c: Int64 101


julia> c,b,a
(101,11,3)

顺便说一句,您可以通过以下方式修复unpack_selected:

BTW, you can fix your unpack_selected by:

unpack_selected(p::Parameters, fields...) = map(x->getfield(p, x), fields).

# note that, the selected field names should be Symbol here
julia> unpack_selected(p, :b)
(11,)

julia> unpack_selected(p, :c, :b, :a)
(101,11,3)

这篇关于朱莉娅(Julia):解开风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆