Skip to content

Instantly share code, notes, and snippets.

View joshiraez's full-sized avatar

José Ráez Rodríguez joshiraez

  • Red Hat
  • Seville, Spain
View GitHub Profile
@joshiraez
joshiraez / Main.java
Created January 23, 2022 21:41
Toy compresser
package com.company;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
record SymbolPair(Short symbol1, Short symbol2) {}
record Compressed(List<Character> symbolToText, List<SymbolPair> symbolDictionary, List<Short> compressed) {}
//Ideally, you can save the compressed in separate binary files with a given word size to later deserialize every value,
import java.util.function.UnaryOperator;
public class Main {
//Top level layer
public static void main(String[] args) {
sayHi(SHOUTING, "joshi");
sayGoodbye(AFABLE, "readers");
}
@joshiraez
joshiraez / OperationWithOperationData.java
Created January 1, 2020 04:52
How would you create classic instances using this aproach, creating a factory. "Rethinking interfaces"
class OperationWithOperationData {
Predicate<String> isOperation;
IntBinaryOperator reductionOperation;
List<Integer> toOperateOn;
private OperationWithOperationData() {};
public static Function<
List<Integer>,
@joshiraez
joshiraez / CharacterData.java
Created January 1, 2020 04:31
Creating Data classes interfaces. Rethinking interfaces in medium
class CharacterData {
int HP;
int attack;
int defense;
//Implement all the boilerplate :D
}
@joshiraez
joshiraez / OperationInterfaceAsClass.java
Created January 1, 2020 04:22
Transforming it to an abstract approach instead of interface approach (having some part of the code already implemented).
class OperationAbstractAsClass implements Operation {
Predicate<String> isOperation;
IntBinaryOperator reductionOperation;
private OperationAbstractAsClass(){};
public static OperationAbstractAsClass of(
Predicate<String> isOperation,
IntBinaryOperator reductionOperation
@joshiraez
joshiraez / OperationInterfaceAsClass.java
Created January 1, 2020 03:55
Third example for "Rethinking interfaces". Now making totally equivalent our "interface class" to implement our interface.
class OperationInterfaceAsClass implements Operation {
Predicate<String> isOperation;
Function<List<Integer>, Integer> operate;
private OperationInterfaceAsClass(){};
public static OperationInterfaceAsClass of(
Predicate<String> isOperation,
Function<List<Integer>, Integer> operate
@joshiraez
joshiraez / OperationInterfaceAsClass.java
Created January 1, 2020 03:48
Second example for "Rethinking interfaces"
class OperationInterfaceAsClass {
Predicate<String> isOperation;
Function<List<Integer>, Integer> operate;
private OperationInterfaceAsClass(){};
public static OperationInterfaceAsClass of(
Predicate<String> isOperation,
Function<List<Integer>, Integer> operate
@joshiraez
joshiraez / Operator.java
Created January 1, 2020 03:30
Example Operator interface for "Rethinking Interfaces" in Medium
import java.util.List;
interface Operation {
boolean isOperation (String input);
int operate (List<Integer> numbers);
}
@joshiraez
joshiraez / allMatch.java
Last active December 16, 2019 16:42
How to assert on a list of boolean functions/predicates/flags that all match without making it messy as hell.
//Is common to use all matching functions. Whichever you need to check if a list of flags are set,
//if some conditions meet, even if all the fields are equal in a equals function, you need to do an all match.
//Many people would do this, but, please, do not do this. Specially if the set of conditions is large.
if (conditionA()) {
if (conditionB()) {
if (conditionC()) {
return true;
}
@joshiraez
joshiraez / EnumCalculator.java
Created June 9, 2019 13:21
Calculator done with enums for the operation
public class EnumCalculator {
//Notice how concise the code has become in the logic
public int calculate(char op, int a, int b) {
return OperationEnum.of(op).operate(a,b);
}
}