Skip to content

Instantly share code, notes, and snippets.

@VAlux
VAlux / ThreadSafeMap.java
Last active April 29, 2022 09:38
Thread Safe map wrapper scratch
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.IntStream;
@VAlux
VAlux / SlidingWindowSpliterator.java
Created October 28, 2021 17:23
Java Spliterator implementation for a sliding window stream handling
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.Queue;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@VAlux
VAlux / JavaFP.java
Created June 5, 2021 08:44
Java collections/streams handy operations
/**
* Basic folding of the specified iterable source seeded with initial value and accumulated by the
* provided operation
*/
public static <T, R> R fold(final Iterable<T> source,
final R initial,
final BiFunction<R, T, R> operation) {
var accumulator = initial;
for (T element : source) {
@VAlux
VAlux / Extractors.java
Created April 7, 2021 09:07
Java nested data structure extractors
/**
* Will extract nested property from value, if it is not null
*
* @param value source value
* @param extractor extractor function for getting desired nested property from value.
* @param <A> type of the source value
* @param <B> type of the target nested property
* @return target nested property from value or null.
*/
public static <A, B> B getOrNull(A value, Function<A, B> extractor) {

Keybase proof

I hereby claim:

  • I am valux on github.
  • I am alv0 (https://keybase.io/alv0) on keybase.
  • I have a public key ASCV5ZTcLFXDeVXVknrSseuZP24VD-YPaXw2WEEbxYQcbAo

To claim this, I am signing this object:

@VAlux
VAlux / Either.java
Last active November 28, 2019 11:42
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Represents a value of one of two possible types (a disjoint union.)
* An instance of Either is either an instance of <code>left</code> or <code>right</code>.
* e.g.: Can contain the value of type L OR R in the same time.
*
* @param <L> Type parameter of first(LEFT) value.
@VAlux
VAlux / StackCalc.scala
Created December 6, 2018 14:42
Attempt to write clean stack calculator in scala.
object Test extends App {
val processCommand: String => List[Int] => List[Int] = {
case "add" => {
case a :: b :: tail => a + b :: tail
case _ => Nil
}
case "sub" => {
case a :: b :: tail => a - b :: tail
case _ => Nil
@VAlux
VAlux / AnagramTest.java
Created July 9, 2018 13:41
Anagram search in java
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
public class AnagramTest {
private final HashMap<String, Integer> dictionary =
@VAlux
VAlux / PromptDialog.java
Last active January 1, 2016 06:39
Android prompt dialog using alert builder
final EditText txt = new EditText(this);
new AlertDialog.Builder(this)
.setTitle("Title")
.setView(txt)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String url = txtUrl.getText().toString();
//do smth. with it.
}
})