generic-programming

How to write below logic in Generic way?

拥有回忆 提交于 2019-12-05 19:53:36
I have a model like below public sealed class Person { public string MobileNo { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } } In my implmentation class, I have a method which takes an IEnumerable as a parameter public string PersonList(string listName, IEnumerable<Person> persons) { dictionary.Add("name", new String[1] { listname }); dictionary.Add("list", persons.ToArray()); PrivateMethod("personList", dictionary); } I have another private method private string PrivateMethod(string value, Dictionary<string, object[]> parameters) { foreach

Avoiding boilerplate when dealing with many unrelated types

旧时模样 提交于 2019-12-05 19:48:15
问题 I'm writing code that deals with values from Language.Exts.Annotated.Syntax, where a variety of types are defined that mirror the structure of a Haskell module: data Module l = ... data Decl l = ... data Exp t = ... -- etc I'd like to be able to write functions that walk these data structures and perform various transformations on them. Because there's no one common data type, I can't write one function that does everything. So far I've written a Tree type that wraps each of these types so

Adding a custom view to XML… but with a GENERIC-type

让人想犯罪 __ 提交于 2019-12-05 18:34:38
问题 I am working on a custom view with a hope of reusability. It should have a generic type, like this: public class CustomViewFlipper<someType> extends ViewFlipper { } I know how to bind a normal custom view to the XML file. But I couldn't find any example for this situation. Is there any way to define a generic type for a class in XML? 回答1: I don't think so, but you can create your own subclass: public class TheClassYouPutInTheLayoutFile extends CustomViewFlipper<someType> and use that class in

Refactoring a “dumb” function into generic STL-style with iterators to containers

风格不统一 提交于 2019-12-05 16:34:57
I've managed to wrap my head around some of C++'s functional capacities (for_each, mapping functions, using iterators...) but the construction of the templates and function argument lists for taking in generic containers and iterators still eludes me. I have a practical example I'm hoping someone can illustrate for me: Take the following function that processes an incoming std::vector and builds a running total of many data-points/iterations of a process: /* the for-loop method - not very savvy */ void UpdateRunningTotal (int_vec& total, int_vec& data_point) { for (int i = 0; i < V_SIZE; i++)

What kinds of types does qsort not work for in C++?

ぐ巨炮叔叔 提交于 2019-12-05 10:47:43
std::sort swaps elements by using std::swap , which in turn uses the copy constructor and assignment operators, guaranteeing that you get correct semantics when exchanging the values. qsort swaps elements by simply swapping the elements' underlying bits, ignoring any semantics associated with the types you are swapping. Even though qsort is ignorant of the semantics of the types you are sorting, it still works remarkably well with non-trivial types. If I'm not mistaken, it will work with all standard containers, despite them not being POD types. I suppose that the prerequisite for qsort

Using Typeable to partially apply function at run-time (any time types match)

早过忘川 提交于 2019-12-05 09:25:20
Generic programming time! If I have a function: f :: a1 -> a2 -> a3 -> ... -> an and a value v :: aX -- where 1 <= x < n Without knowing at compile time which of the arguments of f the value v is the right type for (if any), can I partially apply f to v ? (using Typeable, Data, TH, or any other trick) Slightly more solidly, can I construct the function g (below) at run-time? It doesn't actually have to be polymorphic, all my types will be monomorphic! g :: (a1 -> a2 -> a3 -> a4 -> a5) -> a3 -> (a1 -> a2 -> a4 -> a5) g f v = \x y z -> f x y v z I know that, using Typeable ( typeRepArgs

How to make controller endpoint to get two different objects in java spring?

走远了吗. 提交于 2019-12-05 06:04:43
问题 I have a server built with java and spring. What i am trying to do is that my controller with the same endpoint will get two different objects. This is an example for what I mean: I know I can do that: public class Option1{ private String name; ... //getter and setter } public class Option2{ private Long id; ... //getter and setter } @Controller public class Controller{ @RequestMapping(value = "service/getData/option1", method = RequestMethod.POST) @ResponseBody public String searchProv(

How do I determine the number of parameters of a std::function?

若如初见. 提交于 2019-12-05 04:04:49
问题 I have the following problem. Say you want to write a generic function that can take a lambda expression. I understand that if the parameter is of type std::function, then I could not only use lambdas, but also functions and even pointers to functions. So at a first step, I did the following: void print(std::function<void(int, int)> fn) { fn(1,2); } int main() { print([](int i, int j) { std::cout << j <<','<<i<<'\n'; }); return 0; } Now the problem is that I want to make this function generic

Constructor for nested initializer lists

核能气质少年 提交于 2019-12-05 01:43:03
问题 Is it possible to have a generic constructor that takes any type of initializer list, even if this has nested lists within? Say you have the following partial template specialization for a class that takes in its constructor nested initializer lists: template class ClassA; template <> class ClassA<4> { typedef std::initializer_list<double> list_type; typedef std::initializer_list<list_type> llist_type; typedef std::initializer_list<llist_type> lllist_type; typedef std::initializer_list<lllist

Extracting an Id with GHC.Generics

烂漫一生 提交于 2019-12-04 20:02:15
How to extract an identifier (in this case an integer) from a structure using GHC.Generics ? I have an Id type: newtype Id = Id { _id :: Int } and a lot of types that use that type: data VarId = VarId Name Id SortId data FuncId = FuncId Name Id [SortId] SortId data SortId = Name Id -- ... and 20 more of these things On top of that there are other types that wrap the types above, for instance, identifiers: data Identifier = IdVar VarId | IdFunc FuncId | IdSort SortId -- ... and 20 more of these things Now I need to extract the _id field from any of these types containing an Id value. Currently