Posts

Scala Functions

Local Functions Local functions are the functions defined inside other functions. Therefore , local functions can access the parameters of their enclosing function. First-class Functions Scala supports first-class functions which can be user defined as well as anonymous literal value, for example (x:Int) => x + 1 parameters and the function body separated by the "=>". In case if the type can be inference, then no need to define type, this is called target typing . In the partially applied function , no need to provide all the necessary parameters, but partially. For example, def sum(a:Int, b:Int):Int = a + b //> sum: (a: Int, b: Int)Int val a = sum _ //> a : (Int, Int) => Int = ex3$$$Lambd a.apply(1,2) //> res1: Int = 3 In the second line, you don't need to give parameters. The underscore can be given to replace one or more parameters: val f: (Int, I...

Java 8: Remedies for What cannot be done in Generics

Image
Generic programming the way to reuse code for Objects of many different types. The unbounded wild card is "?" (which is actually "? extends Object"). You can read from it, but you cannot write. If your type variable is T then you can use only extends (never super ) with T then, the upper-bounded wildcard is "? extends T". you can define read from but can not add (only super support with wildcards) The term covariant preserves the ordering of types from more specific to more general. Collections are covariant when they use extends wildcard. The lower-bounded wildcard is "? super T". Collections are contravariant when they use super with a wildcard. The term contravariant preserves the ordering of types from more general to more specific. Here supplied element must be itself or above of the T . The rule of thumb is "producer => extends and consumer => super": PECS.  As shown in the above, you can copy the eleme...

Java 8: Exception Handling

Image
According to Oracle 1 , Java SE 8 class called java.util.Optional<T> that is inspired from the ideas of Haskell and Scala. In the functional programming, exceptions are side effects and need to avoid. Optional in a Nutshell The Optional class includes methods to explicitly deal with the cases where a value is present or absent. You can create variable Optional<Something> sc = Optional.empty() or Optional<Something> sc = Optional.ofNullable(Something); for null object. Otherwise the Optional<Something> sc = Optional.of(Something) create the Something object. Patterns of using Optional This blog is the summary of the video 2 I went through. The problem is very simply demonstrated in the following code: package au.com.blogspot.ojitha.trainings.exception; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ExExample { public static void main(String[] args) { Stream.of("a.txt"...

Learn to Learn

Image
Recently I have been through the 1 and 2 . Here is some notable things to remember as an effective learner. The idea is to study more is not necessarily effective. Instead, it may worst the performance. One of the underlying theory is that things are reinforced when we tend to do more off. Things that are punished or ignored when manage to do less off.  Rules It is worth to note down few rules 1. Rule the moment you started to slide, you're shovelling against the tide.: take a break. Pomodoro technique there are two modes in your brain _focus mode_: tight pattern of Knowledge control your thinking. There is very less opportunity to grasp the new things or being creative. _diffuse mode_: the relax mode. Models of knowledge are weakened. Ready for the new concepts.  This technique needs to be trained. However, most difficult is getting to the focus mode again. But it is important to switch between these modes as shown as above. 2. Rule Create a stu...

Intellij Idea Gradle project

Image
This is the continuation fo the post Introduction to Gradle . Sometime back, I wrote a blog How To: GitHub projects in Spring Tool Suite . At that time, my development environment is mostly Windows and Linux and favorite Java IDE was Eclipse becasue heavy use of Wizards. Later Intellij Idea came with better wizards. Not only that, the quality of the Idea deviated me from the Eclipse. My current environment is macOS. Most of the tools I install using homebrew : sublime, pipenv and so on. Another great installation tool is SDKMAN which I used to install the JDK, gradle and maven. As a Java Developer I need to create a gradle project in the Intellij Idea. But I want to avoid all the fancy wizards and open the project direcly in the Idea. In this example I use Gradle Idea Plugin . Here is my workflow as shown in the diagram,  It is necessary to create directories for the source sets: Java mkdir -p src/main/java/<package> mkdir -p src/main/resources/<package> Test...

Packer to Create Docker

Image
It is simple to create Docker from Packer. In this example I will show you how to create open JDK 8 installed Docker image. First you have to create a packer.json file: { "builders": [ { "name": "docker", "type": "docker", "image": "centos:7", "commit": true } ], "provisioners": [ { "type": "ansible", "playbook_file": "playbook.yaml" } ], "post-processors": [ { "type": "docker-tag", "repository": "ojitha/java8", "tag": "0.0.1" } ] } Here the playbook.yaml which will provision the docker instance: --- - hosts: all tasks: - name: message debug: msg="Container - {{ inventory_hostname }} running {{ ansible_distribution }}" - name: install the latest version of open jdk...

Hands dirty with Kubernetes

Image
The smallest entity Kubernetes can manage is pod: specification of how to run the containers. The pod have network and the storage. Identical Pods can be in a collection called replicas set. Replicaset guarantee the availability of the deployment. However, Kubernetes cannot handle services as well as deployment. Through the services, deployment is exposed. Same Kubernetes host, there can be number of Pods, the namespace define the group of pods belongs to each other: guarantee the secure environment. Architecture . Following are running in the very well defined processes Head Node Worker Node Head Node API server scheduler- Place the containers where need to go Controller Manager- state of the system Etcd-Data store to store the state Kubelet docker Worker Node Kubelet is Kubenetes agent runs on all the cluster nodes Talks to API and local docker demon to manage the docker containers. Kube-proxy manage the trafie of the PODS and the node docker Install ...