Pages

Showing posts with label functional programming. Show all posts
Showing posts with label functional programming. Show all posts

Sunday, June 16, 2019

Enhance your functional java experience with VAVR

In this video I briefly show some features that Vavr has and Java-8 doesn't.




Code examples: https://github.com/SFRJ/vavrExamples

Saturday, December 8, 2018

Java 8 Refactoring Part 1: Extracting Anonymous function to it's own class

In this video I mimic the first of the design patterns presented by Victor Rentea in the Devoxx conference in London in 2018.


Perhaps worth mentioning that the advantages of extracting anonymous functions into separate classes are:

- better readability of the code
- code easier to test
- more maintainable code

Tuesday, April 24, 2018

VAVR - Using map(), flatMap(), Option and Try to get different return types

Simple map() operation in vavr.io takes as argument a function that has as a parameter the type of element contained in the list. The return type of the method in the function can be anything we want. The purpose of map is to transform from one type to another.

public List<BigDecimal> simpleMap(List<Integer> numbers) {
      return numbers.map(n -> m1(n));
   }

    private BigDecimal m1(Integer i) {
     return new BigDecimal(i);
   }


flatMap() uses the same mechanics as map but the only difference is that it will remove the duplication by collapsing the duplicates into a single entry. e.g 1,2,2,2,3 flatMapped will become 1,2,3
public List<BigDecimal> flatMapping(List<Integer> numbers) {
    return numbers.flatMap(n -> m2(n));
   }

   private List<BigDecimal> m2(Integer i) {
     return List.of(new BigDecimal(i));
   }

Sometimes a function can return List<Try<Option<?>>>> that is fine but perhaps Option is sometimes redundant. Notice that this method uses Try<Option>, that looks a bit overkill
public List<Try<Option<String>>> returningARedundantOption(List<Integer> numbers) {
        return numbers.map(n -> m3(n));
    }

    private Try<Option<String>> m3(Integer i) {
        //Imagine this option is the result of intereacting with other code
        // e.g some dao object
        return Try.success(Option.some(""));
    }

To solve the redundancy shown in the example above, we can perform an additional flatMap() so that we get rid of the Option by mapping it to a Try using the toTry() method inside Option. This way we get a List<Try<String>>.
public List<Try<String>> removingRedundancy(List<Integer> numbers) {
        return numbers.map(n -> {
            return m3(n).flatMap(Option::toTry);
        });
    }
    //Same as above
    public List<Try<String>> removingRedundancy(List<Integer> numbers) {
        return numbers.map(n -> m3(n).flatMap(Option::toTry));
    }

    private Try<Option<String>> m3(Integer i) {
        return Try.success(Option.some(""));
    }

In this final example we map a set of integers to a Try<Option<String>> and then we flatMap the result to Set<Try<String>> in order to transform that Set<Try<String>> into a Try<List<String>> we pass the result to Try.sequence() and we map the outcome to list.
public Try<List<String>> usingSequence(Set<Integer> ids) {
        Set<Try<String>> result = ids.map(id -> m4(id).flatMap(Option::toTry));
        return Try.sequence(result).map(Seq::toList);
    }

    //Same as above
    public Try<List<String>> spike2(Set<Integer> ids) {
        return Try.sequence(ids.map(id -> m4(id).flatMap(Option::toTry))).map(Seq::toList);
    }

    private Try<Option<String>>  m4(Integer id) {
        Try.success(Option.of("something" + id));
    }

For more information about the vavr.io framework: http://www.vavr.io/

Thursday, September 24, 2015

First class functions Vs Higher order functions

First class functions means the ability to treat a function as a value, assigning it to a variable, or passing it as an argument to another function.

 //example 1 assignment  
   Function<String,String> postfixFunction = s -> s + "-postfix";  
 //example 2 passing as parameter  
   public void someMethod() {  
     stringManipulator(postfixFunction, asList("A","B"));  
   }  

A higher order function is a function that accepts other functions as arguments, or returns another function.

  //example 1 accepting function as arguments  
   public List<String> stringManipulator(Function<String, String> manipulator, List<String> data) {  
     return data.stream().map(manipulator).collect(toList());  
   }  
   //example 2 returning a function  
   public Function<String, String> postfixFunction() {  
     return s -> s + "-postfix";  
   }   

Share with your friends