Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: java lambda expression #72

Merged
merged 3 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.vscode
*.log
*.sh
out/

### IntelliJ IDEA ###
.idea
Expand Down
19 changes: 19 additions & 0 deletions learn/advanced/JavaLambdaExpressions/Lambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package learn.advanced.JavaLambdaExpressions;

public class Lambda {
// Membuat interface
interface PrintNumbers{
void print(Integer number);
}
public static void main(String[] args) {
// pada ekspresi berikut nilai n akan melakukan perulangan sampai batasnya yaitu 20
// dimana n merupakan parameter dan perulangan merupakan fungsi yang akan dilakukan
PrintNumbers p = n -> {
while(n <= 20) {
System.out.println(n++);
}
};
// akan mencetak angka 1 sampai dengan 20
p.print(1);
}
}
34 changes: 34 additions & 0 deletions learn/advanced/JavaLambdaExpressions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Java Lambda Expressions

_Lambda Expressions_ atau biasa dikenal _Lambda Functions_ adalah blok kode yang memiliki parameter dan mengembalikan suatu nilai. _Lambda Function_ sama seperti _method_ atau _function_ , namun tidak perlu mendefinisikannya secara eksplisit dan bisa langsung implementasi didalam _body_ sebuah _method_

Implementasi simpel dari _Lambda Expression_ memiliki satu parameter dan _expression_


> *parameter* -> *expression*

Untuk menggunakan lebih dari satu parameter bisa meletakkan parameter tersebut kedalam tanda kurung

> *(parameter1, parameter2)* -> *expression*

Namun, jika ingin melakukan sebuah operasi kondisional bisa menggunakan tanda kurung kurawal untuk membungkus blok kode tersebut

> *(parameter1, parameter2)* -> { *blok kode* }

Contoh simpel penggunaan _Lambda Expressions_

```java
public class Lambda {
interface PrintNumbers{
void print(Integer number);
}
public static void main(String[] args) {
PrintNumbers p = n -> {
while(n <= 20) {
System.out.println(n++);
}
};
p.print(1);
}
}
```