Tuesday, February 28, 2017

Scala : Higher order functions , currying , anonymous in functional programming






package com.salpe.scala

object HigherOrderFunctions {

  // define functions like field 
  def identity(x: Int) = x

  def main(args: Array[String]): Unit = {

    // passing declared function as argument 
    println(sumInts(identity, 0, 10))

    // passing anonymous function as argument 
    println(sumInts(x => x, 0, 10))
    println(sumInts(x => x * x, 0, 10))
    println(sumInts(x => x * x * x, 0, 10))

    // Currying 
    println(summationOf(cube)(0, 10))

    println(sumOfCubeOfInts(0, 10))

  }

  // defining functions as local variables  
  def cube(x: Int): Int = x * x * x

  // Passing function as argument 
  def sumInts(f: Int => Int, a: Int, b: Int): Int = {
    if (a > b) 0 else f(a) + sumInts(f, a + 1, b)
  }

  // Defining function inside function 
  def sumOfCubeOfInts(a: Int, b: Int): Int = {
    def myCube(x: Int): Int = x * x * x
    if (a > b) 0 else myCube(a) + sumOfCubeOfInts(a + 1, b)
  }
  // Returning function as argument  
  def summationOf(f: Int => Int)(a: Int, b: Int): Int = {

    def sum(a: Int, b: Int): Int = {
      if (a > b) 0 else f(a) + sum(a + 1, b)
    }

    sum(a, b)
  }


}

Square Root of Number using Newtons method in Scala




 package com.salpe.scala  
 object NewtonSquareRoot {  
  def main(args: Array[String]): Unit = {  
   print(squareRoot(3))  
  }  
  def squareRoot(x: Double): Double = {  
   def squareItr(guess: Double, x: Double): Double = {  
    def isConverged(guess: Double, x: Double): Boolean = {  
     abs(guess * guess - x) / x < 0.001  
    }  
    def improve(guess: Double, x: Double): Double = {  
     (guess + x / guess) / 2  
    }  
    if (isConverged(guess, x)) guess  
    else squareItr(improve(guess, x), x)  
   }  
   squareItr(1.0, x)  
  }  
  def abs(x: Double) = {  
   if (x < 0) -x else x  
  }  
 }  

Saturday, February 25, 2017

MicroServices Aspects


Microservices gives to ability to develop and deploy modules independent of each other with features like fault tolerance , availability  and auto-scaling .

Mostly is manageable  module deployed behind web services which has its own separate storage and compute.
 Most of time these are deployed as cluster behind load balancer like NGINX reverse proxy .
Nodes or containers could come and go as per load on system .

Two services should talk to each other through contracts like  REST/SOAP APIs. As long as they are providing required fields for request it does not matter how it is implemented .

For time taking process they may have their own queue or message broker for different nodes of same service .

Typical micro-services have or they use following modules

  1. Web service APIs
  2. Authentication and Authorization / Identity  (HTTP Basic , Outh)
  3. Configuration/Credentials /Tokens etc .
  4. Message Broker or Queue 
  5. Caching 
  6. Storage (SQL/NOSQL/File) 
  7. Validator 
  8. Logging , Audit Trails , Performance Metrics 
Infrastructure

  1. Deployment could be in containers like dockers or kubernete. 
  2. Auto-Scaling  
  3. Rate Limiting