Baggy add (+) 是否适用于 MixHash 权重? [英] Does Baggy add (+) work on MixHash weights?

查看:39
本文介绍了Baggy add (+) 是否适用于 MixHash 权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MixHash 将两个哈希与 Bag add (+) 运算符组合在一起.这似乎有效 - 但是......我有点惊讶联合的结果需要重新强制回MixHash.

I am using a MixHash to combine two Hashes with the Bag add (+) operator. This seems to work - but ... I am a bit surprised that the result of the union needs to be re-coerced back to a MixHash.

我的猜测是 Bag add (+) 中缀运算符首先将所有内容强制转换为 Bag 并将结果作为 Bag 返回.这对我来说可能有风险,因为我的一些权重是负的(因此首先是混合).这会正确添加负权重吗?

My guess is that the Bag add (+) infix operator coerces everything to a Bag first and returns the result as a Bag. This may be risky for me as some of my weights are negative (thus the Mix in the first place). Will this properly add negative weights?

或者,是否有混合加 (+) 运算符?

Alternatively, is there a Mix add (+) operator?

my MixHash $dim-mix;
for ... {
    my $add-mix = $!dims.MixHash;
    $dim-mix = $dim-mix ?? ( $dim-mix (+) $add-mix ).MixHash !! $add-mix;
}
dd $dim-mix;

现在我看看这段释义的代码,也许有一些三元的公式??!!可以避免在测试中拼出 $dim-mix 因为已经在左边了吗?

Now I look at this paraphrased code, perhaps there is some formulation of ternary ?? !! that can avoid spelling out $dim-mix in the test since already on the left?

非常感谢您的建议!

推荐答案

my $add-mix = (foo => 0.22, bar => -0.1).Mix;

my $dim-mix;
for ^5 {
  $dim-mix (+)= $add-mix;
}
dd $dim-mix; # Mix $dim-mix = ("foo"=>1.1,"bar"=>-0.5).Mix

显然我没有使用过 MixHash,但是如果您需要在循环之后进行排序,您可以对其进行排序.

Obviously I've not used a MixHash, but you can sort that out if you need to after the loop.

(当然,您可能会想但是 Mix 不是不可变的吗?"它是 - 但您必须区分变量和值.$dim-mix是一个变量,一个Scalar变量.即使你输入它——my Mix $dim-mix;它仍然是一个Scalar variable 持有一个 Mix value.你总是可以分配给一个 Scalar.)

(And of course you might be thinking "but isn't a Mix immutable?" It is -- but you have to distinguish variables and values. $dim-mix is a variable, a Scalar variable. Even if you type it -- my Mix $dim-mix; it's still a Scalar variable holding a Mix value. You can always assign to a Scalar.)

我开始为这样的问题制定一个例行程序,我不知道发生了什么,但我认为我应该能够弄清楚.这是我的过程:

I'm starting to get a routine for questions like this where I don't know what's going on but I think I ought to be able to figure it out. Here was my process:

  • 我让你的代码运行看看它做了什么.我试图简化三元.嗯.

  • I got your code to run to see what it did. I tried to simplify the ternary. Hmm.

我转向了文档.有 (+) 的文档页面.这称之为Baggy加法".鉴于 Bag 只能容纳(正)整数,这令人担忧.

I turned to the doc. There was the doc page for (+). That called it "Baggy addition". That was worrisome given that a Bag only holds (positive) integers.

我转向了消息来源.我解雇了 搜索Baggy 添加"的 rakudo 来源.结果之一.我专注于(Mixy:D$a, QuantHash:D $b) 签名.这告诉我结果应该保持 Mixy,即文档暗示它会或可能会去 Baggy 是一个红鲱鱼.

I turned to the source. I fired off a search of the rakudo sources for "Baggy addition". One result. I focused on the multi with (Mixy:D $a, QuantHash:D $b) signature. This showed me that the result should stay Mixy, i.e. the doc's implication it would or could go Baggy is a red herring.

我回到代码并开始想知道我能做什么.当我最初尝试使用 (+)= 来简化主要分配时,编译器抱怨 期望 MixHash 但得到了 Mix.我尝试了六种无效的方法,然后将 $dim-mix 上的 MixHash 约束更改为 Mixy 并且它起作用了.

I returned to the code and started wondering what I could do. When I initially tried to use (+)= to simplify the main assignment the compiler complained expected MixHash but got Mix. I tried a half dozen things that didn't work then just changed the MixHash constraint on $dim-mix to Mixy and it worked.

然后我仔细考虑了正在发生的事情,并意识到几乎所有类型都在妨碍 P6 做正确的事情.

Then I thought through what was going on and realized that almost all the types were getting in the way of P6 just doing the right thing.

如果您确实需要它们,您可以重新添加它们.

You can add some types back in if you really need them.

(但是你真的需要它们吗?当类型绝对必要时,它们很棒.否则,imo,在引入它们之前三思而后行.它们很容易使代码变得更难阅读、推理、撰写和更慢.)

(But do you really need them? When types are absolutely necessary they're great. Otherwise, imo, think twice, and then twice again, before introducing them. They can easily make code harder to read, reason about, compose, and slower.)

(当然,在某些情况下它们不是绝对必要的,但确实对整体有帮助.我和所有事情一样,一开始要保持简单,只有在您看到特定代码行的明显好处时才会复杂化.)

(Of course there are occasions on which they're not strictly necessary but do really help overall. Imo, as with all things, keep it simple at first and only complexify if you see clear benefits for a particular line of code.)

这篇关于Baggy add (+) 是否适用于 MixHash 权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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