Questions tagged [coerce]

73 questions
62
votes
2 answers

In Ruby, how does coerce() actually work?

It is said that when we have a class Point and knows how to perform point * 3 like the following: 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 *…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
57
votes
11 answers

Converting a data frame to xts

I'm trying to convert a data frame to xts object using the as.xts()-method. Here is my input dataframe 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…
user442446
  • 1,099
  • 3
  • 12
  • 13
23
votes
2 answers

Can all typechecking occurrences of `coerce` safely be replaced with `unsafeCoerce`?

I believe the following is as safe as Set.mapMonotonic coerce. i.e. the worst that can happen is I will break the Set invariants if a or b have different Ord instances: coerceSet :: Coercible a b=> Set.Set a -> Set.Set b coerceSet = unsafeCoerce Is…
jberryman
  • 16,334
  • 5
  • 42
  • 83
13
votes
4 answers

Coerce types in different namespaces with Identical layout in C#

I've started writing an interface for FedEx's webservice APIs. They have 3 different APIs that I'm interested in; Rate, Ship, and Track. I am generating the service proxies with SvcUtil.exe. The different service endpoints are each specified by…
joshperry
  • 41,167
  • 16
  • 88
  • 103
11
votes
1 answer

Convert from list to numeric

I am tying to coerce from list form to numeric form. If it is of use, the list is originally drawn from a factor (and is 1x33 rows). My list is defined by: tmpseqsf[[1]] which provides: TradeValue 1 72914431 2 25325 3 20139 4 …
Tim
  • 113
  • 1
  • 1
  • 7
11
votes
1 answer

Force Propagation of Coerced Value

tl;dr: Coerced values are not propagated across data bindings. How can I force the update across the data binding when code-behind doesn't know the other side of the binding? I'm using a CoerceValueCallback on a WPF dependency property and I'm…
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
10
votes
1 answer

Type roles and confusing behavior by `coerce`

I have a type Id a and I'm trying to prevent accidentally coercing, e.g., an Id Double to an Id Int. If I understand type roles correctly, the following should not compile. {-# LANGUAGE RoleAnnotations #-} import Data.Coerce (coerce) type role Id…
Fried Brice
  • 769
  • 7
  • 20
9
votes
2 answers

Cannot 'coerce' data type with 'Reader' as a field

I have the following Haskell code, that compiles perfectly: 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 …
Shersh
  • 9,019
  • 3
  • 33
  • 61
8
votes
3 answers

Is it possible to establish Coercible instances between custom types and standard library ones?

For a simple example, say I want a type to represent tic-tac-toe marks: data Mark = Nought | Cross Which is the same as Bool Prelude> :info Bool data Bool = False | True -- Defined in ‘GHC.Types’ But there's no Coercible Bool Mark between them,…
Javran
  • 3,394
  • 2
  • 23
  • 40
8
votes
3 answers

Is there a shorthand for operations like `fromNewtype . f . toNewtype`?

A pattern that presents itself the more often the more type safety is being introduced via newtype is to project a value (or several values) to a newtype wrapper, do some operations, and then retract the projection. An ubiquitous example is that of…
Ignat Insarov
  • 4,660
  • 18
  • 37
8
votes
2 answers

`coerce` and instantiation of type variables

Consider the following GHCi session: >: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 :21:57: error: *…
Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
8
votes
5 answers

Python: coerce new-style class

I want this code to "just work": 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 Of course, the easy way out is to write int(c)/3, but I'd like to…
bukzor
  • 37,539
  • 11
  • 77
  • 111
8
votes
1 answer

Haskell types with coercible representations identical to their C counterparts?

How can I determine if a Haskell type has an equivalent Coercible instance on a given platform? I've just been told about Coercible in GHC 7.8, which seems great. In that context, I guess an equally good question to solve my specific problem is: Is…
gspr
  • 11,144
  • 3
  • 41
  • 74
7
votes
1 answer

Inconsistent behavior when coercing an Array to a List?

my @foo; @foo = (1, (2, 3), (4, (5, 6), 7), (8), 9).List; say @foo.flat; # OUTPUT: (1 (2 3) (4 (5 6) 7) 8 9) # this is NOT the output I expected. @foo = (1, (2, 3), (4, (5, 6), 7), (8), 9); say @foo.List.flat; # OUTPUT: (1 2 3 4 5 6 7 8 9) # this…
Jim Bollinger
  • 1,671
  • 1
  • 13
5
votes
1 answer

How to avoid "deep recursion" error/warning in Type::Tiny::_build_coercion

I have inherited the following code which was written and works against Type-Tiny-1.004004: package Company::Types; use Type::Library -base, -declare => qw< TruncatedString >; use Type::Utils -all; declare TruncatedString, as Str, where {…
j1n3l0
  • 517
  • 4
  • 16
1
2 3 4 5