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
1 answer
85 views

C# Implementing a list indexed by generic enums [closed]

I've seen a lot of code bases use Dictionaries with strings as their key. This can be redundant and expensive, so I'm trying to make a class that extends ...
user3337705's user avatar
1 vote
1 answer
67 views

Find Method Implementation for Multidimensional Array in C#

I found that Array.Find(T[], Predicate) Method is only support one dimensional array. I am trying to generalize it to multi-dimensional array in this post. The experimental implementation The ...
JimmyHu's user avatar
  • 4,708
2 votes
2 answers
91 views

Generic stack implementation (revision)

The below post is a follow-up of Generic stack implementation. Below follows a header-only implementation of a generic stack (inspired by stb-libraries, following these guidelines: stb-howto.txt). ...
Harith's user avatar
  • 5,834
3 votes
1 answer
204 views

Generic stack implementation

The provided code (a header-only library inspired by stb libraries] defines functions for creating a stack, pushing elements onto the stack, popping elements from the stack, peeking at the top element ...
Harith's user avatar
  • 5,834
2 votes
0 answers
59 views

Implementing Generic, General, Specific and Portable Bitwise Operations

First my appologies. My mental faculties currently leave rather a lot to be desired and I have thus spend an inordinary amount of time on this pet project of mine, testing myself if you will. I find ...
Zacariaz's user avatar
  • 373
6 votes
2 answers
147 views

Generic map-like function

This function mimics Python's map() function (No, it doesn't support generic return types for the function or multiple iterables) by applying the provided function <...
Harith's user avatar
  • 5,834
1 vote
2 answers
139 views

Fully generic, very efficient bidirectional Dijkstra's algorithm in Java

After finding out that my previous implementations are incorrect, I decided to give it another try. I relied on this post. (The entire project resides in this GitHub repository. Contains some unit ...
coderodde's user avatar
  • 28.2k
3 votes
0 answers
65 views

Custom AutoComplete component with Typescript Generics

I've created my own AutoComplete form element based on @nextui-org/autocomplete to be able to use it along with react-hook-form. ...
kursat's user avatar
  • 131
4 votes
1 answer
66 views

Ringbuffer over a const generic Array in Rust

I implemented this generic ringbuffer over a const generic array with the usage of the MaybeUninit type. Is there something which is maybe unsound (having the lines ...
Lockna's user avatar
  • 143
3 votes
1 answer
92 views

Sequentially find the indexes of an element into a collection

Util class to find into a collection the indexes of a given element with multiple occurrences from the first index or relative to a given index. ...
user avatar
2 votes
4 answers
275 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
5 votes
2 answers
499 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
  • 28.2k
5 votes
1 answer
110 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
156 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
209 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
39 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
325 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,940
2 votes
1 answer
213 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
63 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
938 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
100 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
138 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
596 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
  • 255
1 vote
1 answer
106 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
943 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
297 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
528 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
101 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
655 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
637 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
3 votes
1 answer
1k 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
95 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.3k
3 votes
1 answer
87 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
984 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
  • 28.2k
5 votes
0 answers
195 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,385
1 vote
1 answer
51 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
479 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
991 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
851 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
224 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
70 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
736 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
  • 2,015
3 votes
1 answer
1k 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
754 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
81 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
517 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
186 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
898 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
672 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
7 votes
3 answers
2k 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
  • 310

1
2 3 4 5
10