标签: coerce

在Ruby中,coerce()实际上是如何工作的?

据说,当我们有一个班级Point并知道如何执行point * 3如下操作:

class Point
  def initialize(x,y)
    @x, @y = x, y
  end

  def *(c)
    Point.new(@x * c, @y * c)
  end
end

point = Point.new(1,2)
p point
p point * 3
Run Code Online (Sandbox Code Playgroud)

输出:

#<Point:0x336094 @x=1, @y=2>
#<Point:0x335fa4 @x=3, @y=6>
Run Code Online (Sandbox Code Playgroud)

但是之后,

3 * point
Run Code Online (Sandbox Code Playgroud)

不明白:

Point不能强迫进入Fixnum(TypeError)

所以我们需要进一步定义一个实例方法coerce:

class Point
  def coerce(something)
    [self, something]
  end
end

p 3 * point
Run Code Online (Sandbox Code Playgroud)

输出:

#<Point:0x3c45a88 @x=3, @y=6>
Run Code Online (Sandbox Code Playgroud)

所以据说3 * point是一样的3.*(point).也就是说,instance方法 …

ruby coercion coerce type-coercion

60
推荐指数
1
解决办法
9270
查看次数

将数据帧转换为xts

我正在尝试使用as.xts()方法将数据帧转换为xts对象.这是我的输入数据帧q:

q
                      t x  
1  2006-01-01 00:00:00  1  
2  2006-01-01 01:00:00  2  
3  2006-01-01 02:00:00  3

str(q)
    'data.frame':   10 obs. of  2 variables:
 $ t: POSIXct, format: "2006-01-01 00:00:00" "2006-01-01 01:00:00" "2006-01-01 02:00:00" "2006-01-01 03:00:00" ...  
 $ x: int  1 2 3 4 5 6 7 8 9 10
Run Code Online (Sandbox Code Playgroud)

结果是:

> as.xts(q)
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format
Run Code Online (Sandbox Code Playgroud)

这是我能想到的最简单的例子,所以没有让它工作是非常令人沮丧的......任何帮助都表示赞赏!

r time-series coerce dataframe xts

47
推荐指数
3
解决办法
7万
查看次数

是否可以用`unsafeCoerce`来安全地替换所有`coerce'类型检查?

我相信以下内容与一样安全Set.mapMonotonic coerce。即可能发生的最坏情况是,Set如果ab具有不同的Ord实例,我将打破不变式:

coerceSet :: Coercible a b=> Set.Set a -> Set.Set b
coerceSet = unsafeCoerce
Run Code Online (Sandbox Code Playgroud)

那是对的吗?

编辑:有关以下功能的问题Sethttps//github.com/haskell/containers/issues/308

haskell set coerce

22
推荐指数
2
解决办法
212
查看次数

在C#中使用相同布局强制不同命名空间中的类型

我已经开始为FedEx的webservice API编写一个接口.他们有3种不同的API,我很感兴趣; 费率,发货和跟踪.我正在使用SvcUtil.exe生成服务代理.

FedEx在自己的WSDL文件中指定了不同的服务端点.每个服务端点都有自己的xml命名空间(例如http://fedex.com/ws/rate/v5http://fedex.com/ws/ship/v5)

服务端点确实使用了相当多的相同类型,例如Address,Measurements,Weight,AuthenticationDetail,ClientDetail等......

这就是问题所在,我可以同时向SvcUtil.exe提供所有WSDL文件,通常它会将任何相同的类型合并为一个共享类型,但由于FedEx的每个服务都在它们自己的命名空间中,他们在该命名空间下的每个WSDL文件中重新声明这些类型,而不是每个命名空间的Address,Address1和Address2.

为了解决这个问题,我现在要做的是分别通过svcutil运行每个WSDL,并将它们分别放在自己的.NET命名空间中(例如FedEx.Rate,FedEx.Ship,FedEx.Track).这个问题是现在我在每个命名空间中都有一个不同的地址类型(Fedex.Rate.Address,FedEx.Ship.Address).

这使得很难概括像GetAuthenticationDetail()工厂方法之类的服务之间使用的代码,因此我不必在每个使用不同服务的地方重复该代码.

在C#中是否有任何方法可以将FedEx.Rate.Address强制转换为FedEx.Ship.Address?

.net c# types web-services coerce

12
推荐指数
2
解决办法
3018
查看次数

通过“强制”键入角色和令人困惑的行为

我有一个类型Id a,我试图防止意外强制,例如, anId Double到 an Id Int

如果我正确理解类型角色,则不应编译以下内容。

{-# LANGUAGE RoleAnnotations #-}
import Data.Coerce (coerce)

type role Id nominal
newtype Id a = Id String

badKey :: Id Int
badKey = coerce (Id "I point to a Double" :: Id Double)
Run Code Online (Sandbox Code Playgroud)

不幸的是,它确实:

Prelude> :load Id.hs
[1 of 1] Compiling Main             ( Id.hs, interpreted )
Ok, one module loaded.
*Main> :type badKey
badKey :: Id Int
Run Code Online (Sandbox Code Playgroud)

我缺少什么类型角色?

haskell roles coerce

11
推荐指数
1
解决办法
161
查看次数

无法使用“Reader”作为字段来“强制”数据类型

我有以下 Haskell 代码,可以完美编译:

import Control.Monad.Reader (Reader (..))
import Data.Coerce (Coercible, coerce)

data Flow i o = Flow (i -> o) (o -> i)

coerceFlow
    :: (Coercible i i', Coercible o o')
    => Flow i o
    -> Flow i' o'
coerceFlow = coerce
Run Code Online (Sandbox Code Playgroud)

但是,如果我将Flow类型的定义更改为以下内容:

data Flow i o = Flow (i -> Reader Int o) (o -> i)
Run Code Online (Sandbox Code Playgroud)

我开始看到一个奇怪的错误:

Coerce.hs:10:14: error:
    • Couldn't match type ‘o’ with ‘o'’ arising from a use of ‘coerce’
      ‘o’ is a rigid type …
Run Code Online (Sandbox Code Playgroud)

haskell coercion coerce newtype

10
推荐指数
1
解决办法
127
查看次数

强迫值的强制传播

tl; dr:强制值不会跨数据绑定传播.当代码隐藏不知道绑定的另一面时,如何强制更新数据绑定?


我正在使用一个CoerceValueCallbackWPF依赖属性,我坚持认为强制值不会传播到绑定的问题.

Window1.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace CoerceValueTest
{
    public class SomeControl : UserControl
    {
        public SomeControl()
        {
            StackPanel sp = new StackPanel();

            Button bUp = new Button();
            bUp.Content = "+";
            bUp.Click += delegate(object sender, RoutedEventArgs e) {
                Value += 2;
            };

            Button bDown = new Button();
            bDown.Content = "-";
            bDown.Click += delegate(object sender, RoutedEventArgs e) {
                Value -= 2;
            };

            TextBlock tbValue = new TextBlock();
            tbValue.SetBinding(TextBlock.TextProperty,
                               new Binding("Value") { …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf xaml dependency-properties coerce

9
推荐指数
1
解决办法
2363
查看次数

Python:强制新式的类

我希望这段代码"正常工作":

def main():
    c = Castable()
    print c/3
    print 2-c
    print c%7
    print c**2
    print "%s" % c
    print "%i" % c
    print "%f" % c
Run Code Online (Sandbox Code Playgroud)

当然,简单的方法是编写int(c)/3,但我想为配置迷你语言启用更简单的perl-ish语法.

值得注意的是,如果我使用"旧式"类(不从对象继承),我可以通过定义一个__coerce__方法来做到这一点,但旧式类已被弃用,将在python3中删除.

当我使用新式类做同样的事情时,我收到此错误:

TypeError: unsupported operand type(s) for /: 'Castable' and 'int'
Run Code Online (Sandbox Code Playgroud)

我相信这是设计的,但是我怎么能用__coerce__新式的类来模拟旧式的行为呢?您可以在下面找到我当前的解决方案,但它非常难看且冗长.

这是相关文件:(我认为)

奖励积分:

    print pow(c, 2, 100)
Run Code Online (Sandbox Code Playgroud)

python oop class coerce

8
推荐指数
2
解决办法
1084
查看次数

`coerce`和类型变量的实例化

考虑以下GHCi会话:

>:set -XTypeApplications
>import Data.Map.Strict
>import GHC.Exts
>newtype MySet a = MySet (Map a ())
>let member' :: Ord a => a -> MySet a -> Bool; member' = coerce member

<interactive>:21:57: error:
    * Couldn't match representation of type `a0' with that of `()'
        arising from a use of `coerce'
    * In the expression: coerce member
      In an equation for member': member' = coerce member
>let member' :: Ord a => a -> MySet a -> Bool; member' = coerce …
Run Code Online (Sandbox Code Playgroud)

haskell unification coerce

8
推荐指数
1
解决办法
216
查看次数

是否有像fromNewtype这样的操作的简写.F .toNewtype`?

呈现本身往往越式安全正在通过引入的模式newtype是项目的值(或多个值)newtype包装,做一些操作,然后收回投影.一个无处不在的例子是SumProduct幺半群:

? x + y = getSum $ Sum x `mappend` Sum y
? 1 + 2
3
Run Code Online (Sandbox Code Playgroud)

我想象可以为每个函数自动推出一系列函数,如withSum,withSum2等等newtype.或者也许Identity可以创建参数化,以供使用ApplicativeDo.或许还有其他一些我无法想到的方法.

我想知道是否有一些现有技术或理论.

PS   我不满意coerce,原因有两个:

  • 安全   我认为它不是很安全.在被指出它实际上是安全的之后,我尝试了一些事情并且我无法做任何有害的事情,因为它有可能存在模糊性时需要类型注释.例如:

    ? newtype F = F Int deriving Show
    ? newtype G = G Int deriving Show
    ? coerce . (mappend (1 :: Sum Int)) . coerce $ F 1 :: G
    G …
    Run Code Online (Sandbox Code Playgroud)

haskell coerce newtype

8
推荐指数
3
解决办法
402
查看次数