Tuesday, July 21, 2020

Scala : Generic Types in Scala


Generic Types in Scala

a) def func1[T](a : T) : T = a

Explanation :

def listOfDuplicates[A](x: A, length: Int): List[A] = {
  if (length < 1)
    Nil
  else
    x :: listOfDuplicates(x, length - 1)
}

println(listOfDuplicates[Int](3, 4))  // List(3, 3, 3, 3)----1
println(listOfDuplicates("La", 8))  // List(La, La, La, La, La, La, La, La)---2

we explicitly provide the type parameter by writing [Int]. Therefore the first argument must be an Int and the return type will be List[Int].
You don’t always need to explicitly provide the type parameter. The compiler can often infer it based on context or on the types of the value arguments. In this example, "La" is a String so the compiler knows A must be String.


b) 

class Stack[A] {

  private var elements: List[A] = Nil
  def push(x: A) { elements = x :: elements }
  def peek: A = elements.head
  }
}

Explanation:

This implementation of a Stack class takes any type A as a parameter. 
This means the underlying list, var elements: List[A] = Nil, can only store elements of type A. 
The procedure def push only accepts objects of type A (note: elements = x :: elements prepending x to the current elements).
Nil here is an empty List and is not to be confused with null.

example 1:

val stack = new Stack[Int]
stack.push(1)
stack.push(2)
println(stack.peek)  // prints 2

example 2:

class Fruit
class Apple extends Fruit
class Banana extends Fruit

val stack = new Stack[Fruit]
val apple = new Apple
val banana = new Banana

stack.push(apple)
stack.push(banana)

Sunday, July 12, 2020

Test Drive Development (TDD)


Test Drive Development  (TDD)


 

What is TDD :
Write Failing test First and later test should pass as the development progress


Properties of TDD
  1. Red-Green Refractor ie., Write Failing test First and later test should pass as the development progress
  2. Canary Test = Blank test to validate if the environment is working
  3. Description / Console output / Naming should read like a book
  4. Pending tests ie., Only declared but not implemented
  5. Error message from the Unit test should drive the development
  6. Source Code should be separate from the Unit Test (As it does not need to go to production. It can be in the same repo not the same file)
  7. The method under test initially should return below as the development progress : 
    1.   "NULL"
    2.   Change "NULL"  >  Constant
    3.   Constant > "Complex Expression depending on the requirement"
Eg :
assert (mtsToKm(mtrs) == NULL)
def dev (mtrs) : return NULL

assert (mtsToKm(1000) == 1)
def dev (mtrs) : return 1

assert (mtsToKm(1000) == 1)
def dev (mtrs) : mtrs/1000 

Rules of Simple Design:
  1. Pass the tests
  2. Reveal Intention
  3. No Duplication  
  4. Fewest number of elements
Pass the tests :
  1. Always Unit test should come first than we start the actual code.
  2. List all the test initially with only declaration ie.,(only describe and it()) these will be considered as pending tests or todo list.
  3. 1st test in canary test (No test , pass by default)
Reveal Intention.
  1. Meaningful variable and methods names which reveal intention in tests /Actual Code.
  2. Comments are unnecessary if you have meaningful names.
No Duplication
  1. Code should not be duplicated 
  2.  should be < 1  
  3. use functions /methods for repetitions. 
Transformation premises

Pre-Req : 
  • Make Sure all the tests are declared initially
  • And only Canary Test has test definition.

Cyele 1 :
  1. Write a Unit test which sends null / blank as input to the function under test.
  2. Modify the Actual code to handle null / blank
  3. Run the test it should pass.
Cyele 2:
  1. Write a test / Add definition to empty test declaration which sends a simple constant as input to Actual Code
  2. Modify the Actual code to handle that constant either by returning it directly / other means and make sure both the test passes 
  3. Run the test it should pass both the tests.
Cyele 3:
  1. Add definition to next empty test declaration to send little bit complex input.
  2. Modify the Actual code to handle that constant either by using if statements
  3. Run the test it should pass all 3 tests.
Cyele 4:
  1. Add definition to next empty test declaration to send even more  complex input to actual code 
  2. Modify the Actual code to handle that constant either by using better logic and so on.
  3. .....

Friday, July 10, 2020

Dataframe : agg

Spark Dataframe : agg()


To perform column related operations

  • ds.agg(...) = ds.groupBy().agg(...)
  • agg() is a DataFrame method that accepts those aggregate functions as arguments.
Example :

val df3: DataFrame =Seq(1.6,7,42).toDF("CURRENCY")
.withColumn("c2", lit(8.6).cast(DoubleType))
.withColumn("c3", lit(1).cast(DoubleType))
+--------+---+---+
|CURRENCY| c2| c3| +--------+---+---+ | 1.6|8.6|1.0| | 7.0|8.6|1.0| | 42.0|8.6|1.0| +--------+---+---+
df3.agg(max("CURRENCY")).show()
+-------------+ |max(CURRENCY)| +-------------+
| 42.0|
+-------------+
println(df3.agg(max("CURRENCY")).collect()(0))
[42.0]
println(df3.agg(sum("CURRENCY")).collect()(0))
[50.6]
df3.agg(sum("CURRENCY"),sum("c2")).show()
+-------------+------------------+
|sum(CURRENCY)| sum(c2)|
+-------------+------------------+
| 50.6|25.799999999999997|
+-------------+------------------+
In the above example In order write .sum this method has to exist. It is hardcoded on the API. Using .agg you can provide other aggregating functions, sum("column") is just one of them.


Same as :
df3.groupBy().max("CURRENCY")
[42.0]



Sunday, July 5, 2020

POSTMAN:Extraction and parameterization

POSTMAN : Extraction and parameterization

Extraction:
  • Click on + symbol and Create a new Environment
  • Once Environmanr is created , add a varaible example "token" in the environment and keep other fields blank
  • Create a request which gives a response
  • Click on test tab and add below lines of code (Javascript)
var jsonData = pm.response.json();
pm.environment.set("token", jsonData);
  • once you run the request postman should be able to capture the token. You can verify by clicking on the "eye" button . 
  • to use the token , you need to use  {{variable}} Eg : {{token}}


Parametrize url:
  1. Select the environment under use
  2. Add a new varaible say "url"
  3. Set current value as url which is paramaterize as shown in the above picture
  4. Now u can use {{url}}/resourcename inplace of full url (which turns orange)
  5. You can hover over orange variable name to verify if is displaying the correct Endpoint.