Home > Uncategorized > 5. Java vs C# Lamda Expression

5. Java vs C# Lamda Expression

Lamda expression membuat code delegate jadi lebih singkat

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => (n % 2 == 1)); // find the lambda expression here? and there's extension method too

mungkin ada yg bertanya2 darimana method Count yg ada di array integer? Itu extension method. Aku akan jelasin di postingan berikut

klo make java kita harus buat interface lagi dan melewatkannya sebagai strategy.

interface INumberSpesification {
    boolean isSatisfiedBy(int number);
}

class OddNumberSpesification extends INumberSpesification {
    public boolean isSatisfiedBy(int number) {
         return n % 2 == 1;
    }
}

class Numbers {
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
         
    public int count(INumberSpesification a) {
        // foreach numbers cek dengan a.isSatisfiedBy(_element).
        // jika benar maka increment count
       
        // return count
    }
}

simple is beauty. simple is fast.
more code more bugs

Advertisement
Categories: Uncategorized Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: