generic-programming

Are binary operators / infix functions in R generic? And how to make use of?

百般思念 提交于 2019-12-13 06:38:48
问题 From http://adv-r.had.co.nz/Functions.html or R: What are operators like %in% called and how can I learn about them? I learned that it is possible to write own "binary operators" or "infix functioncs" using the % -sign. One example would be '%+%' <- function(a, b) a*b x <- 2 y <- 3 x %+% y # gives 6 But is it possible to use them in a generic way if they are from a pre-defined class (so that in some cases I don't have to use the % -sign)? For exampple x + y shall give 6 if they are from the

How can implement a C++ vector that points to other, multiply typed vectors?

て烟熏妆下的殇ゞ 提交于 2019-12-13 05:20:19
问题 I want to store elements of multiple types in a single vector, while keeping elements of the same type contiguous . The types are derived from a base class and I expect different types to be implemented throughout the development cycle. For this reason it would help if the process of adding a new type to the list is very straightforward. I can achieve this (to an extent) in the following manner: //header enum TypeID { TypeA_ID, TypeA_ID, TypeA_ID, TypeIDAmount }; vector<TypeA> vectorA; vector

Get location data from list comparison and plot

若如初见. 提交于 2019-12-13 02:59:22
问题 In my code, the user inputs a text file which is saved as the variable "emplaced_animals_data." This variable has four columns (Animal ID, X location, Y location, and Z location) and the number of rows varies depending on which text file is uploaded. I then have another list (listed_animals) which contains animals that we want to gather location data about from the emplaced_animals_data. So far, I have created a new variable for each item in the listed_animals list. I want to be able to

Holding a generic type's instance - C++

人盡茶涼 提交于 2019-12-12 14:25:34
问题 I have a tree_node class and a tree class. template<typename T> class tree_node { public: tree_node(const std::string& key_, const T& value_) : key(key_), value(value_) { } private: T value; std::string key; }; template<typename T> class tree { public: tree() : root(new tree_node<T>("", ???)) { } private: tree_node<T>* root; }; tree_node expects an instance of T when creating. How can I pass it in the ??? place? I can say T() , but it will work only if T has a parameterless constructor. I can

Generalizing fold such that it becomes expressive enough to define any finite recursion?

孤街醉人 提交于 2019-12-12 09:37:31
问题 So, there is something known as a "universal property of fold", stating exactly following: g [] = i; g (x:xs) = f x (g xs) <=> g = fold f i However, as you probably now, there are rare cases like dropWhile , which can not be redefined as fold f i unless you generalize it . The simplest yet obvious way to generalize is to redefine universal property: g' y [] = j y; g' y (x:xs) = h y x xs (g' y xs) <=> g' y = fold (?) l At this point I can make my assumption: I assume existence of somewhat

Java - a single generic method to reverse ArrayList or List of objects

自作多情 提交于 2019-12-12 04:33:12
问题 I have seen that there is a very convenient way to reverse ArrayList like here using Collections.reverse() method, but I need to achieve such a reverse operation for several types of primitive objects, like at least ArrayList and List of my custom objects. So Collections can't save my life for an extended usage of a single method. So far I managed to get stg working (See code below) but I have to write it twice: once for ArrayList and once for List. I named them both the same so usage is very

How do I define a function parameter default associated with a generic parameter?

为君一笑 提交于 2019-12-12 04:17:31
问题 I am attempting to refactor a function (found towards the end of this StackOverflow answer) to make it slightly more generic. Here's the original function definition: def tryProcessSource( file: File, parseLine: (Int, String) => Option[List[String]] = (index, unparsedLine) => Some(List(unparsedLine)), filterLine: (Int, List[String]) => Option[Boolean] = (index, parsedValues) => Some(true), retainValues: (Int, List[String]) => Option[List[String]] = (index, parsedValues) => Some(parsedValues)

C++ - Generic programming - Type selection

筅森魡賤 提交于 2019-12-12 01:17:01
问题 The following fragment returns the next highest power of two for an (assumed unsigned) integer of type T. It does this by shifting the bits repeatedly. For all intents and purposes the unsigned type i used in the bit shifting loop is sufficiently large to represent a (nominally) 65536 bit number. Practically therefore it's fine to leave it 'as is'. template <class T> T supremum_2(T k) { if (k == T(0)) return T(1); k--; for (int i=1; i<sizeof(T)*8; i++) k |= k >> i; return k+1; } To do a

Function array initialization at compile time with metaprograming

隐身守侯 提交于 2019-12-11 23:23:49
问题 In video-games is common that resources are loaded in a step fashion way, so within a single thread a loading bar can update at each loading step. By example: 1 -> Load texture A 2 -> Update Loading Bar to 2% 3 -> Load texture B 4 -> Update Loading Bar to 4% 5 ... This can be done in many ways. One of these is define a function for each loading step. void LoadTextureA() { //Loading routine ... } This has the advantage of readability, not need too much nested code and even possible in some

Swift generic func cannot convert value of type to expected argument type

≯℡__Kan透↙ 提交于 2019-12-11 14:58:28
问题 I try to create generic func func importArray<T: ImportableUniqueObject>(from exercisesDict: [[String: Any]], transaction: BaseDataTransaction) -> [T] { if let managedObject = try? transaction.fetchOne(From<T>()){ transaction.delete(managedObject) } let managedObjects = try! transaction.importUniqueObjects( Into<T>(), sourceArray: jsonObjects) return managedObjects } So first part works good: if let managedObject = try? transaction.fetchOne(From<T>()){ ,but second does not work: let