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
27 votes
3 answers
2k views

A Failable<T> that allows safe returning of exceptions

This was inspired by a conversation in chat, that started with the discussion of C#7.0 tuples and out parameter declarations, which led to the idea that there is no 'good1' way to return an error ...
Der Kommissar's user avatar
5 votes
2 answers
263 views

Genericizing PropertyValues

This should be the last iteration for this code. Does this approach make sense or am I heading down the wrong path? The only thing I can see to make this better is to genericize the ...
Gabriel W's user avatar
  • 371
9 votes
2 answers
524 views

ConvertAll Methods Implementation for Multidimensional Array in C#

I am trying to perform some conversions with multidimensional array in C# and I have checked the discussion ConvertAll and 2-dimensional arrays. I found that ...
JimmyHu's user avatar
  • 3,918
8 votes
2 answers
2k views

Recursive flattening of Swift sequences

In Flatten to get all child controls of certain type in a UIView, methods were discussed to recursively flatten a tree-like structure in Swift, resulting in an array of all elements. Motivated by ...
Martin R's user avatar
  • 23k
7 votes
1 answer
341 views

Model simulation using Java annotations

A couple of days ago I posted my code that models systems made up of moving parts. I got several great tips and ideas and here is my latest version. My actions are now methods annotated with @Action (...
uros calakovic's user avatar
7 votes
1 answer
168 views

HTML-rendering framework for emails

Sometimes I need to auto-generate html-emails. To make this task a little bit easier I created a simple framework that takes care of rendering HTML. Because I'm mainly interested in generating HTML ...
t3chb0t's user avatar
  • 44.1k
6 votes
3 answers
177 views

Generic Two Dimensional Data Plane with Manipulation Methods For C#

I know that there is a generic data structure System.Array could be used to create customized type array. I want to focus on the two dimensional data structure that ...
JimmyHu's user avatar
  • 3,918
3 votes
2 answers
120 views

A SubPlane Method for Generic Two Dimensional Data Plane in C#

This is a follow-up question for Generic Two Dimensional Data Plane with Manipulation Methods For C#. Thanks to cliesens's and Rick Davin's detailed answer. Moreover, the mentioned ...
JimmyHu's user avatar
  • 3,918
2 votes
2 answers
97 views

Creating keys/names from interfaces to avoid strings

There are many APIs that require some kind of a string key/name. I usually try to avoid using raw strings and prefer to use ...
t3chb0t's user avatar
  • 44.1k
14 votes
6 answers
11k views

Calculate fingerprint for an object

I need to store serialized objects in a database and I want to avoid duplicates. In order to be able to tell wheter an object is already stored, I have to compare them somehow. My idea was to ...
t3chb0t's user avatar
  • 44.1k
14 votes
7 answers
1k views

Binary Heap where a comparison delegate is used

I currently have a Generic Implementation of a BinaryHeap. It must be able to maintain it's integrity with elements that may or may not implement ...
BarelyTilted's user avatar
13 votes
3 answers
7k views

Structure to ByteArray Extension

I have a need to turn various structures into byte arrays to be sent over serial port to another machine. I created generic extensions to turn any structure into a byte array and from a byte array ...
RubberDuck's user avatar
  • 30.8k
12 votes
2 answers
2k views

Instantiating a run-time implementation of an interface

Below is my code that was written for the following task (based on an SO question): Given a Java Collection (of N elements) create an ...
PM 77-1's user avatar
  • 597
12 votes
3 answers
16k views

Extension method replacing elements from an IEnumerable<T> collection

Today I needed a method to replace all 'x' values in a said collection. Since there is no such method by default in .NET I wrote my own: ...
Denis's user avatar
  • 8,438
9 votes
2 answers
234 views

Reasonable way to implement "safe" math operations using _Generic?

I've been thinking about a way to make it easier to safely use math operations with C's basic datatypes (e.g. using the CERT C coding standard). So far, I've come up with something like this: ...
acarlow's user avatar
  • 193
9 votes
5 answers
646 views

Declarative type comparer

Writing comparers by either implementing the IEqualityComparer<T> or the IEquatable<T> interface is another boring ...
t3chb0t's user avatar
  • 44.1k
9 votes
1 answer
3k views

Database abstraction layer for multiple providers

There's been a lot of questions lately about database provider and repository design especially without entity framework or alike. I thought I try myself to create a reusable framework for this kind ...
t3chb0t's user avatar
  • 44.1k
9 votes
2 answers
2k views

Optimize a generic foreach method that converts Datatable to my object using Reflection

I need to optimize this code block below, this method converts a Datatable object to the Object that I am passing by parameter, in another words, this serializes the object, however I have to run this ...
Roger Oliveira's user avatar
8 votes
5 answers
6k views

Removing exact instances of elements in one list from another

Basically, here's the problem statement: Given an IEnumerable<T> source and an IEnumerable<T> exceptions, return ...
Der Kommissar's user avatar
8 votes
1 answer
5k views

Generic Java task-scheduler

I wrote this generic task scheduler for executing tasks in fixed-delay intervals. Can you find anything wrong with it, or issues that may arise from using it for sending something like queued mails in ...
epoch's user avatar
  • 183
8 votes
2 answers
1k views

Implement a generic Fibonacci sequence in Rust without using Copy trait

I'm trying to learn Rust and am a beginner. How does one go about implementing a generic version of the Fibonacci sequence without using Copy trait in Rust? My code ...
skanur's user avatar
  • 83
8 votes
3 answers
1k views

Automatic EqualityComparer<T> tests

Writing tests is sometimes a really boring task especially if you need to write the same test for the n-th time like when you are testing another custom ...
t3chb0t's user avatar
  • 44.1k
7 votes
2 answers
946 views

Parsing small data into tuples

I'd like to make parsing small samples of data more efficient (from the developer point of view). This means, instead of writing the parsing logic each time from scratch when I encounter something as ...
t3chb0t's user avatar
  • 44.1k
7 votes
2 answers
2k views

Extendable REST-Client

I've been working on a REST-client. The client itself provides only very basic and generic functionality. Everything else is done with extensions. Its raw usage does not need to be convenient but ...
t3chb0t's user avatar
  • 44.1k
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
7 votes
2 answers
3k views

Generic NSMapTable replacement written in Swift

This is my attempt at writing a generic NSMapTable with weak keys and strong values (after your feedback I'll be trying to write Strong-Key/Weak-Value and Weak-Key/...
Alex's user avatar
  • 561
7 votes
3 answers
924 views

Custom collection implementing IList<T> saving current, next and previous element

I'm developing a Tic Tac Toe game where you can change your pawn look Now as You can see it has 2 buttons one for next one for previous, and You can also pick if you want to edit the O or the X ...
Denis's user avatar
  • 8,438
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
6 votes
3 answers
1k views

Factory for classes unknown at compile time

I have a class called Machine. It contains a list of parts and a list of actions. The list of parts will contain instances of the ...
uros calakovic's user avatar
6 votes
2 answers
355 views

A monad in Java (FriendSpace and People)

After asking a similar question on Stack Overflow, I'm picking up on the answer there to improve my monad. I'm not trying to solve the general case, just come up with one to see how it works. If I'm ...
sea-rob's user avatar
  • 467
6 votes
2 answers
1k views

Generic immutable object builder

I've made an object builder which can create all sorts of classes, it can also create immutable objects. There are 2 requirements that must be met in order for this pattern to work with fully ...
Denis's user avatar
  • 8,438
6 votes
3 answers
362 views

Calculating time windows for entities

I have made some code that operates on multiple entities and creates 'time windows' for them. Basically, the entities will contain states, usually in boolean or int. They will be timestamped and in ...
Mark Cilia Vincenti's user avatar
6 votes
1 answer
130 views

Generic Timing Class

I have written a class which can time functions, and I'd like to have it reviewed. I'm interested in everything (better naming and commenting, accuracy of measurement, usability, structure, use of ...
tim's user avatar
  • 25.1k
5 votes
1 answer
17k views

Polling loop to run in a background thread

I came up with the idea of a small utility class that will poll some delegate until the response received meets some condition, upon which it will notify the main thread which can take the appropriate ...
Alain's user avatar
  • 472
5 votes
1 answer
144 views

Double LinkedList Deep Copy in Kotlin with Generics and Thread Safety

Goal Return a deep copy of a double LinkedList. Each node also contains an additional random pointer, potentially to any node or null. Code to start ...
AdamHurwitz's user avatar
5 votes
0 answers
331 views

Extendable REST-Client - follow-up

My previous RestClient wasn't disposing a lot of resources. I've fixed it (hopefully without introducing new issues) and because a few new patterns emerged I did a ...
t3chb0t's user avatar
  • 44.1k
5 votes
2 answers
346 views

Generic equality checker

I use this method to check if two reference types are equal ...
IEatBagels's user avatar
  • 12.4k
4 votes
1 answer
4k views

Library for parsing strings to java types, generic types and collections/arrays

I've done a library that can parse strings to different java types and to List<...>, Set<...>, ...
drapostolos's user avatar
4 votes
0 answers
174 views

Heap update generic algorithms

In the standard library, there are no algorithms for element updates. This makes it unsuitable as a queue for a Dijkstra's algorithm, for example. Thus I implemented generic heap update functions with ...
Ilya Popov's user avatar
4 votes
2 answers
7k views

java.util.Observable but with generics to avoid casts

This is an Observable class similar to java.util.Observable. The difference is that it uses generics to avoid casts. The ...
hkoosha's user avatar
  • 153
4 votes
3 answers
446 views

Type-independent vector in C

I wanted to create a vector that can be used for different types, as generics do not exist in C. I came up with an implementation that maintains a buffer on the heap and reallocs itself when the ...
PhilippHochmann's user avatar
4 votes
1 answer
537 views

Functional Html builder

This is my third attempt to create a easy to use html builder because I wasn't really satisfied with the first one that wasn't extendable at all and even the one using dynamics wasn't much better. It ...
t3chb0t's user avatar
  • 44.1k
3 votes
1 answer
8k views

Cached-object Store c# with Redis client for persistent storage

I have written a Cached-Object store with a Redis Client for persistent storage. The application that is going to use this is a heavy read application with the occasional write. I assume that entire ...
Joachim's user avatar
  • 205
3 votes
1 answer
4k views

A Java class for calculating size of iterable/array

Another exercise of Java generics: ...
qed's user avatar
  • 1,429
3 votes
2 answers
1k views

Batch save with progress reporting

I reviewed this question that in my opinion uses a not so pretty batch-save so I tried improve it and this is what I came up with. In this more generic solution that allows to reuse it I wanted to ...
t3chb0t's user avatar
  • 44.1k
3 votes
0 answers
289 views

Object pooling in WPF

In a WPF project I'm working on, often multiple objects reuse the same AnimationTimeline, to smooth that out I made an ...
Denis's user avatar
  • 8,438
3 votes
1 answer
1k views

Making Dependency Property registration strongly typed

I've been working on another builder, this time for the DependencyProperty because it's object-based. This means that every ...
t3chb0t's user avatar
  • 44.1k
3 votes
1 answer
287 views

Recursive flattening of Swift sequences - an overly complicated approach

I recently read and answered Martin R's Recursive flattening of Swift sequences and continued to play around with the code until I arrived at something that was both pretty cool and possibly an ...
overactor's user avatar
  • 654
3 votes
2 answers
5k views

Tips on multiple key Map-wrapper

I'm creating a simple generic Map-wrapper with multiple keyed values. I'm intending to use it with storing edges in a graph, where an edge goes from one vertex to ...
mfaerevaag's user avatar
3 votes
2 answers
117 views

Sequence generators not only for potatoes but also for apples and oranges

The last question about sequence generators Growing potatoes in delayed sequences was only about potatoes. I thougt why not make it work with apples and oranges too so I made it generic. The base ...
t3chb0t's user avatar
  • 44.1k