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 ...
user1806687's user avatar
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: ...
coderodde's user avatar
  • 27k
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 ...
user1806687's user avatar
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 ...
Adaenthusiast's user avatar
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 ...
Jason Leaver's user avatar
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 ...
eddyxide's user avatar
  • 141
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 ...
Kittoes0124's user avatar
  • 1,930
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 ...
Thane Brimhall's user avatar
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, ...
user3132457's user avatar
6 votes
2 answers
754 views

Classes representing 2D points and pixels

How can I improve this code or make it tidier? ...
user366312's user avatar
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 (...
sarema's user avatar
  • 449
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. ...
John's user avatar
  • 1
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. ...
haz's user avatar
  • 203
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 ...
Basil Bourque's user avatar
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 ...
Russo's user avatar
  • 123
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? ...
Erik Bent's user avatar
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 ...
willywinelover's user avatar
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 ...
Robin Carter's user avatar
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 ...
Oliver Schönrock's user avatar
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 ...
Emil Holmsten's user avatar
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 ...
Moshe Katz's user avatar
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 ...
t3chb0t's user avatar
  • 44.1k
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 ...
Julius's user avatar
  • 171
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 ...
coderodde's user avatar
  • 27k
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 ...
Greedo's user avatar
  • 2,345
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 ...
Ben beri's user avatar
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 ...
oneManArmin's user avatar
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: ...
Erick Santander's user avatar
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 ...
TimeToCode's user avatar
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, ...
user avatar
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. ...
Ankush Tripathi's user avatar
3 votes
1 answer
424 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 ...
einpoklum's user avatar
  • 1,985
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 ...
BugSquanch's user avatar
4 votes
3 answers
581 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 ...
deutschistschwer's user avatar
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 ...
leaf_yakitori's user avatar
4 votes
0 answers
448 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 ...
JiaHao Xu's user avatar
  • 141
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 ...
Andy Lin's user avatar
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 ...
Tim Hunter's user avatar
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: ...
geoffrey's user avatar
  • 121
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 ...
fededevi's user avatar
  • 300
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 ...
KlemenPl's user avatar
  • 185
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: ...
xilpex's user avatar
  • 289
-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 ...
Youssef Ashraf's user avatar
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 ...
JimmyHu's user avatar
  • 3,918
0 votes
0 answers
124 views

Various type Tuple flatten tool implementation

For some reason, I am trying to implement a various type tuple flatten tool with the following requirements: The nested Tuple object ...
JimmyHu's user avatar
  • 3,918
2 votes
1 answer
583 views

Two Dimensional Array to Markdown Table Converter Implementation in C#

I am attempting to implement a converter which can convert two dimensional array (such as string[,]) into markdown table. The experimental implementation The ...
JimmyHu's user avatar
  • 3,918
1 vote
1 answer
144 views

Generic Class for a GSON LinkedHashMap

I originally posted this on Stack overflow and was told to post it here, so here it is. Here's the stack overflow question: https://stackoverflow.com/questions/66528393/generic-class-for-a-gson-...
FireController1847's user avatar
1 vote
1 answer
111 views

ForEach Methods Implementation for Multidimensional Array in C#

I am working with System.Array and I found that the input parameter of ForEach method ...
JimmyHu's user avatar
  • 3,918
0 votes
1 answer
68 views

Divide a 3D matrix into multiple smaller 3D matrix

I'm trying to build an extension method that will allow me to subdivide a huge array into much smaller ones. My current code is the following but it still takes about 2s to generate with an array of ...
Kamigaku's user avatar
5 votes
1 answer
500 views

Generic Queue in C

I'm implementing a generic (ie. void *) queue in C. I believe I have a working version but I'm looking for two things: Do any subtle bugs pop out to the ...
sdasdadas's user avatar
  • 557

1
2 3 4 5
10