FP – Relationship between Applicative Functor and Monad

Applicative Functor has the following two properties.

Pure: create a functor from value.
Apply: a function taking arguments which are a Functor having a function and a Pure. This function will return the Functor after applying the function inside the first Functor with a value from a Pure by unwrapping.

Example)


Optional<int> apply(Optional<function<int(std::string)>> f, Optional<std::string> v) {
   if (f & v) {
      return Optional<int>(*f (*v));
   }

   return Optional(nullptr);
}

Java example)


Optional<Integer> apply(Optional<Function<String, Integer>> f, Optional<String> v) {
   return f.flatMap(a -> v.map(a(v))
}

Monad is the Applicative Functor with joining to flat the returning Functor.
So Monad is Applicative Functor with a join.
Join: flattening the Functor

Join Example)

Optional<int> join(Optional<Optional<int>> v) {
   if (v) {
      return *v;
   }

   return Optional<int>(nullptr);
}

Monad C++ definition:

template<typename T, typename U>
Monad<U> bind(Monad<T> m, std::function<Monad<U>(T)> f) {
   return join(apply(pure(f), m));
}
Advertisements
Posted in Functional Programming, Programming | Leave a comment

Java – Open addressing hash implementation in Java

IdentityHashMap class

Map<String,String> iMap = new IdentityHashMap<String,String>();
Posted in Java, Programming | Leave a comment

Java – headMap, tailMap and subMap in NavigableMap

NavigableMap collection on Java provide methods to return the view of Map on sorted order of key. The most common methods are headMap, tailMap and subMap.

headMap example.

        NavigableMap<String, String> navigableMap = new TreeMap<>();

        navigableMap.put("1", "1");
        navigableMap.put("2", "2");
        navigableMap.put("3", "3");
        navigableMap.put("4", "4");

        SortedMap<String, String> resultViewMap = navigableMap.headMap("3");

        resultViewMap.entrySet().stream().forEach(entry ->
                System.out.println(String.format("key: %s, value: %s", entry.getKey(), entry.getValue())));

output

key: 1, value: 1
key: 2, value: 2

If inclusive is true on calling headMap(“3”, true), it will be

key: 1, value: 1
key: 2, value: 2
key: 3, value: 3

tailMap example.

        NavigableMap<String, String> navigableMap = new TreeMap<>();

        navigableMap.put("1", "1");
        navigableMap.put("2", "2");
        navigableMap.put("3", "3");
        navigableMap.put("4", "4");

        SortedMap<String, String> resultViewMap = navigableMap.tailMap("3");

        resultViewMap.entrySet().stream().forEach(entry ->
                System.out.println(String.format("key: %s, value: %s", entry.getKey(), entry.getValue())));

output

key: 3, value: 3
key: 4, value: 4

By default, inclusive is true on calling tailMap(“3”, true).

subMap example

        NavigableMap<String, String> navigableMap = new TreeMap<>();

        navigableMap.put("1", "1");
        navigableMap.put("2", "2");
        navigableMap.put("3", "3");
        navigableMap.put("4", "4");

        SortedMap<String, String> resultViewMap = navigableMap.subMap("2", "4");

        resultViewMap.entrySet().stream().forEach(entry ->
                System.out.println(String.format("key: %s, value: %s", entry.getKey(), entry.getValue())));

output

key: 2, value: 2
key: 3, value: 3

By default, inclusive is true for fromKey, and inclusive is false for toKey on calling subMap(“2”, true, “4”, false)

There are more available methods in NavigableMap

descendingKeySet() and descendingMap()

ceilingKey(), floorKey(), higherKey() and lowerKey()

celingEntry(), floorEntry(), higherEntry(), lowerEntry()

pollFirstEntry() and pollLastEntry()
Posted in Java, Programming | Leave a comment

Java – Java 8 Comparator to replace Guava compound comparing

Guava example

Collection.sort(list, 
    Ordering.from((Comparator<Item>) Comparator.comparingInt(item -> item.getId()))
    .compound(Comparator.comparing(Item::getName)))

Java 8 example

Comparator<Item> compoundComparator
      = Comparator.comparingInt(Item::getId)
        .thenComparing(Item::getName);

Collection.sort(list, compoundComparator)
Posted in Java, Programming | Leave a comment

Java – Guava API to implement compareTo with considering sorting ordering

Example of a chained comparison statement.

   public int compareTo(Foo that) {
     return ComparisonChain.start()
         .compare(this.aString, that.aString)
         .compare(this.anInt, that.anInt)
         .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
         .result();
   }
Posted in Java, Programming | Leave a comment

Java – The article of Stream API under the hood

The following article is explaining the under the hood Java 8 Stream API.

https://www.ibm.com/developerworks/library/j-java-streams-3-brian-goetz/index.html

Posted in Java, Programming | Leave a comment

Java – Converting Cassandra ResultSet to Java 8 Stream

Statement stmt = QueryBuilder().select().all().from('table');
ResultSet rs = session.execute(stmt);

StreamSupport.stream(
    Spliterators.spliteratorUnknownSize(
                rs.iterator(), Spliterator.ORDERED), false)
.forEach(row -> {
   ...
});

As input stream is sorted by partition key and input order is important, the following example can be used.

Statement stmt = QueryBuilder().select().all().from('table');
ResultSet rs = session.execute(stmt);

StreamSupport.stream(
    Spliterators.spliteratorUnknownSize(
                rs.iterator(), Spliterator.ORDERED | Spliterator.SORTED), false)
.forEach(row -> {
   ...
});
Posted in cassandra, Java, Programming | Leave a comment