Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View sviperll's full-sized avatar

Victor Nazarov sviperll

  • München, Germany
  • 09:03 (UTC +02:00)
View GitHub Profile
Map<String, Integer> map = // ...
int limit = 50;
PriorityQueue<Map.Entry<String, Intger>> queue = new PriorityQueue<>(limit, Comparator.comparing(Map.Entry::getValue).reversed());
for (var entry: map.entrySet()) {
if (queue.size() == limit) {
queue.remove(); // Remove MAXIMAL element
}
queue.add(entry);
}
@sviperll
sviperll / Try.java
Created October 2, 2018 13:50
Try (aka Exception-monad) for Java
package com.github.sviperll;
import com.github.sviperll.exception.ExceptionfulConsumer;
import com.github.sviperll.exception.ExceptionfulFunction;
import com.github.sviperll.exception.ExceptionfulRunnable;
import com.github.sviperll.exception.ExceptionfulSupplier;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Optional;
import java.util.function.Function;
@sviperll
sviperll / anti-if-case.markdown
Last active June 20, 2016 15:30
On layered architecure and naturalness of internal data-types

On layered architecure and naturalness of internal data-types

After reading Make Everything The Same post by Sandi Metz I've started thinking about one of my own experiences with software design. During the evolution of one system I've faced a challenge of extending billing subsystem. I've actually spent couple of days thinking about the problem and this process in foresight seems very similar to one described be Sandi. At first everything seemed clear about problem, but I hated conditionals that have sprung into life. This raise of conditionals seemed both inevitable, but somehow artificially inflicted.

@sviperll
sviperll / GADT.java
Last active August 29, 2015 14:15
GADT with ADT4J
/**
*
* @author Victor Nazarov <asviraspossible@gmail.com>
*/
public class GADT<T> extends GADTBase<GADT<Integer>, GADT<Boolean>, GADT<T>> {
public static GADT<Integer> literal(int i) {
return new GADT<Integer>(GADTBase.<GADT<Integer>, GADT<Boolean>, GADT<Integer>>_literal(i));
}
@sviperll
sviperll / gist:3734802
Created September 16, 2012 23:21
More thoughts on new lazy pure functional programming language to run on JVM
class List implements Monad {
(:), nil, (++), null public;
(:), nil constructor;
(:) :: forall a. a -> this a -> this a;
nil :: forall a. this a;
(++) :: forall a. List a -> List a -> List a;
(x:?xs) ++ ys = x : (xs ++ ys);
nil? ++ ys = ys;
@sviperll
sviperll / gist:2762734
Created May 21, 2012 14:50
My thoughts on new programming language

I have been thinking a lot about implementing functional lazy language targeting JVM with syntax like:

class Maybe a = Just a | Nothing
  implements Monad, Eq when a implements Eq, Ord when a implements Ord
  where {
    derive Ord;
    derive Eq;

override {