FP – Gang of four failed me on designing multi threaded and concurrent aware software

Modern hardware force us to design softwares with multi-threaded and concurrent aware code.

But Gang of four design principal didn’t show me how to design scalable software by avoiding racing condition and thread locking because Gang of four design pattern encourage us to hide details and abstract operations but this make more problems on dealing with concurrent processing. In order to make a correct software, I had to introduce the locking code to avoid the race condition. This code degraded the performance of application by wasting time to finish the task in different thread or serializing the concurrent operations.

Fortunately, I have found a way to solve this problem after understanding the functional programming and concepts such as Monoid, Functor, Applicative Functor and Monads.

Functional programming don’t hide details so that I can reason about all logic in code very quickly. Above all, immutable data structure and Monoid make me maximize the utilization of multiple cores without worrying about race condition and locking. Now I can see how to design a highly scalable software in a correct way.

Advertisements
Posted in Functional Programming, Programming | Leave a comment

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));
}
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