coerce

In Ruby, how does coerce() actually work?

∥☆過路亽.° 提交于 2019-12-28 02:27:06
问题 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 * 3 Output: #<Point:0x336094 @x=1, @y=2> #<Point:0x335fa4 @x=3, @y=6> but then, 3 * point is not understood: Point can't be coerced into Fixnum ( TypeError ) So we need to further define an instance method coerce : class Point def coerce(something) [self, something]

writing a “.rtest” output to file, using the R program (ex) via write.table?

北战南征 提交于 2019-12-25 00:37:39
问题 I am using R to open up some saved .csv files in a particular pairwise manner and perform a statistical test ( mantel.rtest , found in the package "ade4"). The .csv files are sequentially named as either "fileAX" or "fileBY", where X and Y are integers. I'd like to save the results of this test in a single file, but am running into some issues. Here's the code (please forgive the inefficient usage of "paste": library(ade4) x <- 1:15; y <- 1:15 filename1 <- paste(paste(c("fileA"), 1:15, sep =

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

爷,独闯天下 提交于 2019-12-22 04:15:10
问题 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 Sum and Product monoids: λ x + y = getSum $ Sum x `mappend` Sum y λ 1 + 2 3 I imagine a collection of functions like withSum , withSum2 , and so on, may be automagically rolled out for each newtype . Or maybe a parametrized Identity may be created,

In Ruby, how to implement “20 - point” and “point - 20” using coerce()?

寵の児 提交于 2019-12-08 11:42:46
问题 In Ruby, the operation of point - 20 # treating it as point - (20,20) 20 - point # treating it as (20,20) - point are to be implemented. But the following code: class Point attr_accessor :x, :y def initialize(x,y) @x, @y = x, y end def -(q) if (q.is_a? Fixnum) return Point.new(@x - q, @y - q) end Point.new(@x - q.x, @y - q.y) end def -@ Point.new(-@x, -@y) end def *(c) Point.new(@x * c, @y * c) end def coerce(something) [self, something] end end p = Point.new(100,100) q = Point.new(80,80) p (

Convert from list to numeric

99封情书 提交于 2019-12-04 11:42:42
问题 Hi I've had a look at a lot of other qns/answers and haven't been able to resolve coercing 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 ... So based on other advice (Stackoverflow, etc) I use: tmpx <-as.numeric(tmpseqsf[[1]]) but I get the error: Error: (list) object cannot be coerced to type 'double' And just confirming nothing has

Force Propagation of Coerced Value

Deadly 提交于 2019-12-03 17:37:08
问题 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 stuck at the issue that coerced values don't get propagated through to bindings. Window1.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; namespace CoerceValueTest { public

Converting a data frame to xts

≯℡__Kan透↙ 提交于 2019-11-26 12:18:43
问题 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 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 The result is: > as.xts(q) Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard