Questions tagged [generics]

Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters.

Filter by
Sorted by
Tagged with
2 votes
4 answers
240 views

Generic type data storage

We have a module which can be used by user to store and load value of variables. Every variable has an index associated with it (in the shown code cample this is ommitted and index is instead the data ...
4 votes
2 answers
407 views

Generic matrix library in Java

I have this repository. The most important source files follow. com.github.coderodde.math.linear.matrix.AbstractMatrix.java: ...
2 votes
1 answer
140 views

Using Ada's access type and generics to solve an ODE

This post is linked to Ada: Convert float to decimal I have produced Ada codes which uses the Euler algorithm Euler method to solve an ordinary differential equation. The codes given below are working ...
5 votes
1 answer
100 views

Instance specific code generation

Disclaimer: I've asked this question before on Stack overflow and got a response that this place would be a better fit so I am copy pasting the question here. I've come up with two different ...
2 votes
1 answer
38 views

Python Module for representation, calculus and rich comparisons between temperature scale instances

ToTemp is a temperature conversion package with Celsius, Delisle, Fahrenheit, Kelvin, Rankine, Réaumur, Newton and Rømer scales. With a documentation and already in PyPI. The source code for the main ...
1 vote
0 answers
134 views

Generic[type[Enum], Protocol[DataFrame]] Dataset with mapped to enum types

Below is my solution for managing multiple DataFrames, in an abstract enough way that it may apply to objects outside of a pandas.DataFrame hence the ...
3 votes
1 answer
425 views

A predicated version of std::for_each

For reasons, I want to implement a predicated version of std::for_each. I know that, in C++20, this is made somewhat redundant or less useful, as we can use ...
5 votes
0 answers
173 views

@lazylist - Converts a Python Generator to a List without evaluating every element

Inspired by a blog I read, I made a LazyList class that can wrap an iterable (list, generator etc.) and turn it into a ...
3 votes
1 answer
237 views

Generic Integer Square Root

I have implemented an integer square root function that is branch-free and runs in constant time, using the first variant found in this answer as a base. All possible values for the types ...
2 votes
1 answer
140 views

Generic Interpolation for many types in C#

I made a generic interpolator for Unity objects, but the way it functions makes me question if there's a better way to approach this problem. I want the logic inside of the ...
2 votes
1 answer
54 views

Small typesafe assertion library, is()

I need to write a function with the following requirements (Programming TypeScript book by B. Cherny, Chapter 4, exercise 5): Implement a small typesafe assertion library, ...
6 votes
2 answers
754 views

Classes representing 2D points and pixels

How can I improve this code or make it tidier? ...
3 votes
1 answer
94 views

rust: idiomatic use of generics when using a filetype-dependent writer

Simplified from https://github.com/132nd-vWing/tacview-splitter. External dependencies: zip crate The code would do the following: read a file from disk (...
5 votes
3 answers
1k views

C++ Generic Callback class with removable listeners by unique id

I'm quite new to the STL. Does this make sense? Is there a better way of removing the listeners instead of using shared_ptr while keeping the code short and simple? Is there something in the STL to ...
1 vote
1 answer
91 views

Correct usage of Java Generics for a simple cache holding objects of any particular type that contain an identifier of any particular type

The Stack Overflow question, I'm trying to create an inMemory database using Collections and generics, presents some code that attempts to use Java Generics for an in-memory cache of objects of any ...
0 votes
1 answer
111 views

Singleton generic implementation of simple LinkedList

I would like to ask if the implementation is ok, and what I can improve. Although I'm not sure if the Singleton is working properly. ...
2 votes
1 answer
395 views

A database trait in Rust. How can I make all these generic parameters easier to use?

I am trying to hide the implementation details of accessing a Postgres database. To do this, I want to create "database" and "transaction" traits for use in a repository struct. ...
1 vote
1 answer
260 views

Suggestions for improvement for generic array and map functions in Go 1.18

Yesterday I build some generic functions for the new official Golang 1.18 release. What do you think about it and what could be added and improved? ...
2 votes
1 answer
723 views

MongoDB abstraction in Go with generics

I'm working on a new project with go. So I figured I would give this new feature a run (it's fun!). Although, I'm not sure I'm implementing this the way it's suposed to be. I've read the following ...
-1 votes
1 answer
1k views

Making a generic web API controller when using multiple tables with similar fields

I have been trying to refactor my code since I found I've been doing a lot of copying and pasting to implement the logic of the web API controllers. Here's how the controllers looked like before ...
2 votes
1 answer
391 views

JSON save system for Unity

I made a Json save system for my Unity game. I wanted to make something as generic as possible to keep the code DRY. I couldn't find much information online on how to make something generic. Could you ...
2 votes
1 answer
685 views

Generic Pairing Heap Performance

I have made a generic pairing heap library in C. Pairing heaps are one of the several heap variants with better asymptotic running times than standard binary heaps (others include Fibonacci heaps and ...
3 votes
0 answers
95 views

Build the Mountain Car Environment using coupled inheritance and type annotations

This code builds the environment for the standard Mountain Car Problem of reinforcement learning. I'm using Python 3.9 with PyCharm and I'm wishing to use type annotations throughout. I'm trying to ...
1 vote
1 answer
62 views

Stream that computes running median value

I was solving a DSA question, running median in a stream. And I decided to design my own stream class with additional features. ...
6 votes
3 answers
538 views

generic implementation of median

Below is a generic implementation of the summary statistics function Median. Discussion of efficient use of std::nth_element and ...
3 votes
2 answers
438 views

Generic Observer/Listener implementation in Java

I am working on a personal project in university where I am currently trying to make my own implementation of a generic observer pattern that I can use throughout my project. I would appreciate any ...
2 votes
1 answer
962 views

Getting a single result from multiple LiveData objects

I have an Android viewmodel for a Fragment that requires the user to perform multiple tasks. In addition to a LiveData object to track the status of each task, I ...
1 vote
1 answer
42 views

interface different usages issue for two or more different implementations

I am writing a small micro-service in typescript with NestJS. This microservice is responsible of giving a user access to one of our company's systems trial access. At first I used hubspot integration ...
3 votes
0 answers
88 views

Handling boilerplate tasks via service pipelines

Here's a small experiment about handling boilerplate tasks via service pipelines. It's similar to the HTML pipeline but simpler and multipurpose. I've updated the question and this is the 2nd version ...
3 votes
1 answer
81 views

C Typed Vector that implements type-safety, encapsulation and lazy initialization

C Typed Vector I wasn't happy with the various dynamic array / vector implementations in C, so I made my own. In general, I'm not the biggest fan of C-preprocessor madness, but alas for this ...
7 votes
5 answers
972 views

Checking whether a string is a permutation of a palindrome in C++20 - follow-up

This post is the follow-up of Checking whether a string is a permutation of a palindrome in C++20. So, what's new? Well, nothing else except that the procedure is now generic and accepts all ...
5 votes
2 answers
15k views

Is there a better way of defining generic entity classes?

I have some entities in use in my project, and to make things easier, I would like to have the type of the key for that entity defined via a generic. E.g.: ...
0 votes
1 answer
409 views

Generic Base Repository implementation with Unit of Work in C#

I have seen a lot of Repository pattern implementations with Unit of Work. The simplest way of implementing this I came across is using hard-coded repos (such as this one), while the way recommended ...
1 vote
2 answers
727 views

Retrying a RestRequest

I wrote a method that lets me retry RestSharp requests when a specific exception is thrown. Here's the code for that method: ...
4 votes
2 answers
820 views

Method for create a copy of List<T>

I created a method to return a copy of List<T>, basically I convert the List<T> into an array ...
1 vote
1 answer
211 views

Generic doubly linked list in C

I have implemented generic doubly linked list in C language in which you can store any data type you want. Just like in C++ and Java, where a list can store any data type, like - string, int, long, ...
14 votes
2 answers
1k views

Is this C# 'hack' a bad idea?

As C# doesn't have generic specialisation like C++ template specialisation and I wanted a way to be able to do it, I came up with a little hack that goes a bit like this: ...
3 votes
1 answer
823 views

Quicksort using generics in java

The goal is to have a function that can sort a series of all object types that implement the comparable interface. I'm not trying to improve the performance of the sort. I'm only trying to optimize my ...
12 votes
5 answers
14k views

Convert a boxed integer to an nullable integer with potentially different type

Given a boxed integer (with a boxed short, int, ...) I want to convert it to an unboxed, nullable type instead. It should be ...
4 votes
3 answers
582 views

FIFO array/queue

I'm new to programming and was tasked with programming a generic circular FIFO queue, without using anything but an underlying array and self-programmed methods. I still don't know how to approach ...
4 votes
2 answers
183 views

CGI script that reads or writes its height and weight configuration

UPDATES: I'm looking for an answer that can answer the questions listed in the post. Moreover, this answer will not only continue my way of thinking, but also give feedback based on it. It's just like ...
0 votes
1 answer
79 views

How to save/get different type of data with less duplicated code

description: I'm using thrift to save and get data by filter. The data have many types, and the types may increase in the future. So I use ...
4 votes
0 answers
449 views

A safe type-erased Fn Pointer in rust that can be used to call associated and regular function

I am implementing a callback method to replace the use of generics. For maximum performance, it is not allowed to use Box and I also don’t want to use ...
2 votes
2 answers
557 views

Generic Object mapping function

I implemented as an exercise a function which maps over object values in Typescript, and I am truly horrified by my type annotations. The function: ...
3 votes
1 answer
721 views

Generic Scanner(System.in) Parsing

Often end up using a Scanner to grab some simple input and often when dealing with Number input it can result in a lot of little ...
4 votes
2 answers
130 views

Related classes around generic types

I'm wondering if this code can be simplified. I'm less than thrilled about the fact that I repeat the Draw method in both CatModel and DogModel. For that matter, I'm not real happy that that method is ...
2 votes
1 answer
318 views

C - Generic vector library

I'm quite new to C, and I decided to create a generic vector library. Here is the code: ...
2 votes
1 answer
354 views

Dynamic generic array list implementation in C99

As an exercise to get to know C a bit more, I decided to implement a dynamic array implementation in C99 with generic support. Everything is implemented with series of macros. The array_list struct ...
9 votes
2 answers
1k views

Generic queries and handlers without reflection

I'm trying to implement generic queries and handlers so that I can make requests with simple syntax, like this: ...
0 votes
1 answer
1k views

Exists Method Implementation for Multidimensional Array in C#

To determine whether the specified array contains specific element or not, Array.Exists can be used if the given array is one dimensional. I am attempting to implement ...

1
2 3 4 5
10