Skip to content

Instantly share code, notes, and snippets.

View asiryk's full-sized avatar
🌿

Alexander Siryk asiryk

🌿
View GitHub Profile
@asiryk
asiryk / withCounter.java
Created July 26, 2021 11:38
Transform Consumer to the Consumer with counter (can be useful for Stream.forEach(withCounter((element, index) -> {})))
public static <T> Consumer<T> withCounter(ObjIntConsumer<T> consumer) {
AtomicInteger counter = new AtomicInteger(0);
return item -> consumer.accept(item, counter.getAndIncrement());
}
@asiryk
asiryk / invokePrivate.java
Last active July 23, 2021 06:42
Method to invoke private methods of other objects via reflection
/**
* Method to invoke private methods of other objects via reflection.
*
* <pre>{@code
* BigDecimal object = BigDecimal.ZERO;
*
* int result = invokePrivate(
* object, "adjustScale",
* Arrays.asList(10, 2));
*
@asiryk
asiryk / throttle.ts
Last active January 28, 2021 09:20
Generic throttle implementation
/**
* Limits the maximum number of times a given callback can be called over the time
* @param func - callback to throttle
* @param ms - interval in milliseconds to throttle a callback
*/
function throttle<T extends (...args: any[]) => any>(func: T, ms: number): T {
let isThrottled = false;
return <T>function () {
if (isThrottled) {
@asiryk
asiryk / list.ts
Last active October 3, 2022 13:17
Doubly linked list (TypeScript). Iterable contract.
class ListNode<T> {
value: T;
prev: ListNode<T> | null;
next: ListNode<T> | null;
constructor(value: T) {
this.value = value;
this.prev = null;
this.next = null;
}