Illuminatiiiiii
11/6/2018 - 11:57 PM

Lambda Block Expressions

For Episode 62 of the Java Series: https://youtu.be/hovBaeOIuVY

interface StringFI{ //Functional Interface for working with strings
    String giveString(String a);
}
interface NumberFI{ //Functional interface for working with arrays of ints
    int newArray(int[] array);
}
public class Main {

    public static void main(String[] args) {

        //Implementation for our Functional Interface that reverse a string given
        StringFI reverse = (String a) -> {
            String reversedString = "";
            for (int i = a.length() - 1;i >= 0;i--){
                reversedString = reversedString + a.charAt(i);
            }
            return reversedString; //We HAVE to return the variable manually when using code blocks for lambdas
        }; //semicolon here
        System.out.println(reverse.giveString("ice ice baby"));

        //An expression that defines the implementation to find the sum of all the numbers in an array of ints
        NumberFI sumfinder = (int[] array) -> {
            int sum = 0;
            for (int i = 0; i < array.length;i++){
                sum = sum + array[i];
            }
            return sum;
        };
        int[] numbers = {4, 6, 1, 2, 6};
        System.out.println(sumfinder.newArray(numbers));
    }
}