Posts

Showing posts from 2018

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

Introduction To Gradle

The Gradle is a groovy based build tool. I used to install sdkman to setup Gradle as well as maven in my machine because sdkman allow you to install number of version and use any version of the tool at the time it necessary. This is a great feature I was expecting as a developer. So exciting with sdkman. In the following example, I used Gradle version 3.4.1 to setup the multi-moudle project. Here the parent gradle.build file: task wrapper (type: Wrapper){ gradleVersion = '3.4.1' distributionType = Wrapper.DistributionType.ALL } subprojects{ apply plugin: 'java' dependencies { // https://mvnrepository.com/artifact/org.slf4j/slf4j-api compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' } repositories { mavenCentral() } jar { baseName = 'example1' version = '0.0.1-SNAPSHOT' } } As shown in the above, If you need to use wrapper (which

Scala Tips - separating common from varying

It is a best practice to separating common from varying in the Scala. Common parts are implemented as functions and the varying parts will be arguments: def createSeqOnCriteria(start:Int, end:Int, // createSeqOnCriteria: createSeqOnCriteria[](val start: Int,val end: Int,val criteria: Int => Boolean) => scala.collection.immutable.IndexedSeq[Int] criteria: Int => Boolean) = { // for (i <- start to end if criteria(i)) yield i // } //

Scala Tips: Functions and Closures

Image
In the Scala functions are first class. For example function literal can be possible to assign to object as follows: val f = (x:Int ) => x + x //f: Int => Int = <function1> f(2) // res0: Int = 4 The different between function literal and function value is that literal exists in source code and the value exists in the runtime. Compiler do the target typing if you don't specify the type for the x in the above source. To more precise, you can use the _ as a placeholder instead of x bound variable in the above code because x is a parameter to the function. For simplicity : //collection supports val l1 = for (i <- 1 to 10) yield i //l1: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) l1.filter(_ > 3 ) //res1: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 5, 6, 7, 8, 9, 10) In the above source, when _ used that is called partially applied function because don't supply all of the arguments needed by the expre

Python Mocking Examples

Here the first example using decorator in python 2.7 import unittest import random import mock def myrandom(p): return random.random() > p class Test(unittest.TestCase): @mock.patch('random.random') def test_myrandom(self, mock_random): mock_random.return_value = 0.1 val = myrandom(0.0) assert val > 0 assert mock_random.call_count == 1 if __name__ == '__main__': unittest.main() Here the example for assert_callled_with() function import unittest import mock import example class Test(unittest.TestCase): @mock.patch('example.hello') def test1(self,mock_hello): x = 'Oj' example.hello(x) # Uses patched example.func mock_hello.assert_called_with(x) if __name__ == '__main__': unittest.main() Above test can be ran using context manager: import unittest import mock import example class Test(unittest.TestCase): def test1(self): x = &q

JavaScript Tips: Module Pattern

Coming from the Java background, my main problem with JavaScript is encapsulation: global space is not good. I love OO because object is the best place to encapsulate the data. Here are the patterns I would like to follow always. In the JavaScript, function can be used as : function method constructors classes Module As a first example, will start with the Module. Module Pattern In this pattern, you can define number of privilege method which have access to the secret information such as private variables and methods . For example, setFirstName method can set the name local variable as well as the function getUserFullName has access to the private method getFullName as shown int he following code: var person = (function (firstName, lastName, age){ //properties var a = age; var fname = firstName; var lname = lastName; //common functions function getFullName(f,l){ return fname+" "+lname; } return { //open to outside getFirstName:

Python Algorithm: create Object from JSON

As shown in the following example, you can use the @wrap (which return another wrapper) to to transfer JSON to object in the python. This blog written conjunction with the Python Algorithm to flattening JSON 1 . from functools import wraps def json_to_object(func): @wraps(func) def wrapper(self, d): for name, value in d.iteritems(): setattr(self, name,value) return func(self, d) return wrapper class Person(object): @json_to_object def __init__(self, d): pass a = Person({'firstName':'Tom', 'lastName':'Hanks', 'age':50}) a.firstName a.lastName a.age More advanced version from functools import wraps def json_to_object(func): @wraps(func) def wrapper(self, d): for name, value in d.iteritems(): if type(value) == dict: print(value) setattr(self,name,Person.fromJson(value)) else: setattr(self, nam

Python Algorithm to flattening JSON

Algorithm to flatten the deep JSON structure: def flat(root, **args): d = {} for k, v in args.iteritems(): if type(v) == dict: d.update(flat((k if root == None else '{}.{}'.format(root,k)),**v)) elif type(v) == list: for idx, item in enumerate(v): d.update(flat('{}.{}'.format(k if root == None else '{}.{}'.format(root,k),idx), **item)) else: if root == None: d['{}'.format(k)] = v else: d['{}.{}'.format(root,k)] = v #print ('key: {}, val: {}'.format(k,v)) return d for example, if you flatten the following JSON structure: tt ={'name':{'firstName':'Tom', 'lastName':'Hanks'}, 'orderlineitems':[{'rice':{'qty':2,'price':10}},{'bread':{'qty':1,'price':2}}], 'age':20, 'location'

Scala Tips: Matching

Scala matching functionality is compelling. This feature is very important for big data programming. This blog tries to consolidate matching example as much as possible. Basic use of Match Boolean operator Use in the exceptions With Constants With Regex With Vector With Map As an Iterator Case with guard conditions With Option With Tuples With List With Seq Wildcards Case classes Reference Basic use of Match The simplest matching var i = 2 // i: Int = 2 // i match { // res0: String = two case 1 => "one" // case 2 => "two" // } // You can match anything as follows: def getClassAsString(x: Any):String = x match { // getClassAsString: (x: Any)String case s: String => s + " is a string" // case i: Int => i +" is Int"