coercion

sprintf invalid format '%d'

痞子三分冷 提交于 2019-12-03 05:10:04
This works: > sprintf('%d', c(1, 1.5)) [1] "1" "1" and this doesn't: > sprintf('%d', c(1.5, 1)) Error in sprintf("%d", c(1.5, 1)) : invalid format '%d'; use format %f, %e, %g or %a for numeric objects Why? Mikael Jumppanen This is actually really interesting question. To start, %d stands for integer. The vector argument is recycled if possible but if it is c(1.5, 1) it will fail when sprintf() tries to replace %d with 1.5 (which is not integer). I thought it might be related to the fact that in R both integer and double are numeric mode, for example: storage.mode(c(1.5, 1)) # [1] "double"

Are there cases where a typedef is absolutely necessary?

隐身守侯 提交于 2019-12-03 02:03:50
Consider the following excerpt from the safe bool idiom : typedef void (Testable::*bool_type)() const; operator bool_type() const; Is it possible to declare the conversion function without the typedef? The following does not compile: operator (void (Testable::*)() const)() const; Ah, I just remembered the identity meta-function. It is possible to write operator typename identity<void (Testable::*)() const>::type() const; with the following definition of identity : template <typename T> struct identity { typedef T type; }; You could argue that identity still uses a typedef , but this solution

Casting vs. coercion in Python

十年热恋 提交于 2019-12-02 21:55:08
In the Python documentation and on mailing lists I see that values are sometimes "cast", and sometimes "coerced". What is the difference? I think "casting" shouldn't be used for Python; there are only type conversion, but no casts (in the C sense). A type conversion is done e.g. through int(o) where the object o is converted into an integer (actually, an integer object is constructed out of o). Coercion happens in the case of binary operations: if you do x+y , and x and y have different types, they are coerced into a single type before performing the operation. In 2.x, a special method _

Scala 2.10, its impact on JSON libraries and case class validation/creation

爷,独闯天下 提交于 2019-12-02 19:01:37
In Scala 2.10 apparently we're getting improved reflection. How will this impact lift-json, jerkson, sjson and friends? Furthermore, can we expect in the not too distant future a built-in JSON language feature a la Groovy's excellent GSON in Scala? The reason I ask is that I would dearly love to do: case class Foo(a: String, b: Int, bar: Bar) case class Bar(c: Int) val foo = Foo("hey", 10, Bar(23)) val json = foo.toJson without hoop jumping (i.e. boilerplate-ish prep work), even with arbitrarily complex object graphs. Perhaps I'm asking way too much, but one can always dream. Please shatter my

Coerce variables in data frame to appropriate format

社会主义新天地 提交于 2019-12-02 16:02:53
问题 I'm working a data frame which consists of multiple different data types (numerics, characters, timestamps), but unfortunately all of them are received as characters. Hence I need to coerce them into their "appropriate" format dynamically and as efficiently as possible. Consider the following example: df <- data.frame("val1" = c("1","2","3","4"), "val2" = c("A", "B", "C", "D"), stringsAsFactors = FALSE) I obviously want val1 to be numeric and val2 to remain as a character. Therefore, my

Convert a vector of lists with uneven length to a matrix

流过昼夜 提交于 2019-12-01 08:53:54
I have a vector, well a data frame with only one column, that contains lists of uneven lengths: data = list( c(349, 364, 393, 356, 357, 394, 334, 394, 343, 365, 349), c(390, 336, 752, 377), c(670, 757, 405, 343, 1109, 350, 372), c(0, 0), c(), c(1115, 394, 327, 356, 408, 329, 385, 357, 357)) and I would like to convert it to a matrix, filling the gaps with NA . So it should look something like this: 349, 364, 393, 356, 357, 394, 334, 394, 343, 365, 349 390, 336, 752, 377, NA, NA, NA, NA, NA, NA, NA 670, 757, 405, 343, 1109,350, 372, NA, NA, NA, NA 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA NA, NA

Convert a vector of lists with uneven length to a matrix

自闭症网瘾萝莉.ら 提交于 2019-12-01 06:01:36
问题 I have a vector, well a data frame with only one column, that contains lists of uneven lengths: data = list( c(349, 364, 393, 356, 357, 394, 334, 394, 343, 365, 349), c(390, 336, 752, 377), c(670, 757, 405, 343, 1109, 350, 372), c(0, 0), c(), c(1115, 394, 327, 356, 408, 329, 385, 357, 357)) and I would like to convert it to a matrix, filling the gaps with NA . So it should look something like this: 349, 364, 393, 356, 357, 394, 334, 394, 343, 365, 349 390, 336, 752, 377, NA, NA, NA, NA, NA,

Dependency Property Coercion binding issues

好久不见. 提交于 2019-12-01 03:39:49
I have both VS2008 and VS2010 installed, and I see a very strange behavior In VS2008, I have a simple WPF app: <TextBox x:Name="textbox" Text="{Binding Path=MyProperty,Mode=TwoWay}"></TextBox> and public Window1() { InitializeComponent(); DataContext = this; } public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(Window1), new PropertyMetadata("default",null,Coerce)); private static object Coerce

Dependency Property Coercion binding issues

怎甘沉沦 提交于 2019-12-01 01:47:29
问题 I have both VS2008 and VS2010 installed, and I see a very strange behavior In VS2008, I have a simple WPF app: <TextBox x:Name="textbox" Text="{Binding Path=MyProperty,Mode=TwoWay}"></TextBox> and public Window1() { InitializeComponent(); DataContext = this; } public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty",

Array Types In Powershell - System.Object[] vs. arrays with specific types

蹲街弑〆低调 提交于 2019-11-29 06:34:51
Why does caling GetType().Name on an array of strings return Object[] and not String[] ? This seems to happen with any element type, for example Import-Csv will give you an Object[] but each element is a PSCustomObject . Here's an example with an array of String $x = @('a','b','c') $x[0].GetType().Name #String $x.GetType().Name #Object[] Miroslav Adamec Because you have not specified the data type of the array explicitly. For instance, assigning an integer to $x[1] would work, because the array's type is Object[] . If you specify a data type while constructing the array, you won't be able to