package com.example.lambda;

interface NumericFun {
	int fun(int n);
}

public class LambdaExample2 {

	public static void main(String[] args) {
		// TRY THIS
		// write implementation of NumericFun interface
		// that will return square value of n
		// use lambda expression
		NumericFun square = n -> n * n;

		System.out.println("Square of 3 is " + square.fun(3));
		System.out.println("Square of 5 is " + square.fun(5));
		System.out.println("Square of 7 is " + square.fun(7));

		// you can later pass square as parameter to another function

		// TRY THIS
		// write implementation of NumericFun interface
		// that will calculate the factorial of n
		// use lambda expression - block lambda expression
		NumericFun factorial = n -> {
			int result = 1;
			for (int i = n; i > 1; i--) {
				result = result * i;
			}
			return result;
		};
		
		System.out.println("Factorial of 3 is " + factorial.fun(3));
		System.out.println("Factorial of 5 is " + factorial.fun(5));
		System.out.println("Factorial of 7 is " + factorial.fun(7));
	}

}
