generic-programming

Container for pointers to member functions with different arguments

我只是一个虾纸丫 提交于 2019-12-19 11:54:24
问题 I am looking everywhere (Modern C++ design & co) but I can't find a nice way to store a set of callbacks that accept different arguments and operate on different classes. I need this because I would like every object of my application to have the possibility to defer the execution of one of its methods to a master Clock object that, keeping track of the current time, can call this methods in the right moment. The code I am aiming for is something along the lines of: In the void

Transposing arbitrary collection-of-collections in Scala

独自空忆成欢 提交于 2019-12-19 06:02:44
问题 I have to often transpose a "rectangular" collection-of-collections in Scala, e.g.: a list of maps, a map of lists, a map of maps, a set of lists, a map of sets etc. Since collections can be uniformly viewed as a mapping from a specific domain to a co-domain (e.g.: a List[A]/Array[A] is a mapping from the Int domain to the A co-domain, Set[A]is a mapping from the A domain to the Boolean co-domain etc.), I'd like to write a clean, generic function to do a transpose operation (e.g.: turn a map

What are concepts?

北城余情 提交于 2019-12-18 11:45:01
问题 I've heard all this new (on /.) about C++0x not having concepts anymore, but I have no idea what they are? Can someone explain to me? 回答1: Concepts are a generic programming feature which allow someone writing templated code to specify requirements which the type parameters need to meet. For example, some collection types need for the type parameter for the collection to define the < operator. So the programmer might define a concept called LessThanComparable which tells the compiler that the

Generic Programming in Scala

£可爱£侵袭症+ 提交于 2019-12-18 09:34:21
问题 Hi all I am fairly new to Scala coming from C#. I am attempting to write my own version of accumulate ( fold) I am wondering why I am running into some issues with the following: def accumulate[T](list : List[T], initial: T, f: (T, T) => T) : T = { @tailrec def loop[T](list: List[T], accum: T) : T = if(list.length == 0) accum else{ val head : T = list.head val res : T = f(accum,head) loop[T](list.tail, res) } loop(list,initial) } I am getting the following error: type mismatch; found : accum

How to make some generic programming in fortran 90/95 working with intrinsic types

a 夏天 提交于 2019-12-18 06:55:04
问题 I would like to program some procedure that will work with different types. I am planning to use the "include" method used in flibs described here and here. I give here a simple exemple. ! -------------------------------------------------------------- ! module data_type type ivalue integer :: v end type type rvalue real(8) :: v end type end module data_type ! -------------------------------------------------------------- ! module imod use data_type, only: T => ivalue include "template.f90"

Generic Types Collection

我的梦境 提交于 2019-12-18 05:23:10
问题 Building on previous question which got resolved, but it led to another problem. If protocol/class types are stored in a collection, retrieving and instantiating them back throws an error. a hypothetical example is below. The paradigm is based on "Program to Interface not an implementation" What does it mean to "program to an interface"? instantiate from protocol.Type reference dynamically at runtime public protocol ISpeakable { init() func speak() } class Cat : ISpeakable { required init() {

Scrap Your Boilerplate in f#

断了今生、忘了曾经 提交于 2019-12-17 22:44:27
问题 I've used the Scrap Your Boilerplate and Uniplate libraries in the Haskell programming language, and I would find that form of generic programming over discriminated unions to be really useful. Is there an equivalent library in the f# programming language? 回答1: Not that I know of; without support built-in to the language/compiler, I expect the only alternative is a reflection-based version. (I don't know how Uniplate is implemented - do you?) Here's the code for a reflection-based version

Trait for numeric functionality in Rust

試著忘記壹切 提交于 2019-12-17 07:52:14
问题 Is there any trait that specifies some numeric functionality? I'd like to use it for bounding a generic type, like this hypothetical HasSQRT : fn some_generic_function<T>(input: &T) where T: HasSQRT { // ... input.sqrt() // ... } 回答1: You can use num or num-traits crates and bound your generic function type with num::Float, num::Integer or whatever relevant trait: extern crate num; use num::Float; fn main() { let f1: f32 = 2.0; let f2: f64 = 3.0; let i1: i32 = 3; println!("{:?}", sqrt(f1));

How can I write function to convert generic type to Tag-shaped type for use with DSum?

好久不见. 提交于 2019-12-14 02:02:18
问题 How can I implement this toDSum function? I've managed to get the base case to compile, but I don't know how to carry all the type information across a recursive call. Do I have to strip off the Code from the type before trying to recurse? (this is a followup to How can I write this GEq instance?) {-# LANGUAGE GADTs #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} module Foo where

Generic Programming in Go. Avoiding hard coded type assertion

心不动则不痛 提交于 2019-12-13 09:40:14
问题 I'm programming a generic cache mechanism and i need to set some attributes in a struct knowing only their reflect.Type, attribute name and reflect.Value to be setted in the attribute, but i can't avoid the type assertion, that makes my code not generic... func main() { addressNew := Address{"New Address description!"} // In the real problem, i know the reflect.Type of value, but // the struct came to me as a interface{}, just like this method // Return many kinds of values from redis as