Skip to content

Instantly share code, notes, and snippets.

View asiryk's full-sized avatar
🌿

Alexander Siryk asiryk

🌿
View GitHub Profile
@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;
}
@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 / 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 / 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 / node_nginx_ssl.md
Created September 25, 2021 11:16 — forked from bradtraversy/node_nginx_ssl.md
Node app deploy with nginx & SSL

Node.js Deployment

Steps to deploy a Node.js app to DigitalOcean using PM2, NGINX as a reverse proxy and an SSL from LetsEncrypt

1. Sign up for Digital Ocean

If you use the referal link below, you get $10 free (1 or 2 months) https://m.do.co/c/5424d440c63a

2. Create a droplet and log in via ssh

I will be using the root user, but would suggest creating a new user

@asiryk
asiryk / compareShiftedArrays.ts
Created October 29, 2021 21:27
The function, that compares arrays with shifted values.
/**
* Compares arrays with shifted values.
*
* @example
* // returns 3
* compareShifted([1, 2, 3, 4], [4, 1, 2, 3]);
* @example
* // returns -1
* compareShifted([1, 2, 3, 4], [1, 2, 4, 3]);
* @example
@asiryk
asiryk / ascii-shift-decryption.java
Created February 18, 2022 09:31
Java files, regex, chars examples
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collector;
public class H13_5 {
@asiryk
asiryk / nvim-better-defaults.lua
Created February 20, 2022 12:50
Must have neovim remaps
local opts = { noremap = true, silent = true }
local expr = { noremap = true, silent = true, expr = true }
-- Center screen on the "next" and "previos" search jumps
vim.api.nvim_set_keymap("n", "n", "nzzzv", opts)
vim.api.nvim_set_keymap("n", "N", "Nzzzv", opts)
-- Move one or more selected lines up and down (in Visual mode)
vim.api.nvim_set_keymap("v", "J", ":m '>+1<CR>gv=gv", opts)
vim.api.nvim_set_keymap("v", "K", ":m '<-2<CR>gv=gv", opts)
@asiryk
asiryk / match.java
Created July 12, 2022 09:15
Pattern matching for Java
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
/**
* Usage example:
* {@code (int) new Matcher(5).matchCase(1, () -> 1).defaultCase(() -> 5)}
*/
public class Matcher<V, R> {
@asiryk
asiryk / typescript_is_broken.ts
Last active December 4, 2022 07:51
Typescript is broken
// If A extends B, does it mean that Type<A> extends Type<B>?
//
// An example that shows broken type variance in TypeScript.
// Following code compiles, but it shouldn't.
// It also doesn't fail at runtime because there are example classes
// with no methos, but real-world classes would use wrong methos and fail at runtime.
// Let's pretend there are unique methos for each class
abstract class Animal {}