aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/id-id/doc/functions/function-literals.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs/id-id/doc/functions/function-literals.md')
-rw-r--r--doc/docs/id-id/doc/functions/function-literals.md86
1 files changed, 43 insertions, 43 deletions
diff --git a/doc/docs/id-id/doc/functions/function-literals.md b/doc/docs/id-id/doc/functions/function-literals.md
index 316e07c..589a26d 100644
--- a/doc/docs/id-id/doc/functions/function-literals.md
+++ b/doc/docs/id-id/doc/functions/function-literals.md
@@ -1,21 +1,21 @@
1# Function Literals 1# Literal Fungsi
2 2
3All functions are created using a function expression. A simple function is denoted using the arrow: **->**. 3Semua fungsi dibuat menggunakan ekspresi fungsi. Fungsi sederhana ditandai dengan panah: **->**.
4 4
5```yuescript 5```yuescript
6my_function = -> 6my_function = ->
7my_function() -- call the empty function 7my_function() -- memanggil fungsi kosong
8``` 8```
9<YueDisplay> 9<YueDisplay>
10 10
11```yue 11```yue
12my_function = -> 12my_function = ->
13my_function() -- call the empty function 13my_function() -- memanggil fungsi kosong
14``` 14```
15 15
16</YueDisplay> 16</YueDisplay>
17 17
18The body of the function can either be one statement placed directly after the arrow, or it can be a series of statements indented on the following lines: 18Badan fungsi bisa berupa satu pernyataan yang ditulis langsung setelah panah, atau berupa serangkaian pernyataan yang diindentasi di baris berikutnya:
19 19
20```yuescript 20```yuescript
21func_a = -> print "hello world" 21func_a = -> print "hello world"
@@ -36,7 +36,7 @@ func_b = ->
36 36
37</YueDisplay> 37</YueDisplay>
38 38
39If a function has no arguments, it can be called using the ! operator, instead of empty parentheses. The ! invocation is the preferred way to call functions with no arguments. 39Jika fungsi tidak memiliki argumen, ia dapat dipanggil menggunakan operator `!`, sebagai ganti tanda kurung kosong. Pemanggilan `!` adalah cara yang disarankan untuk memanggil fungsi tanpa argumen.
40 40
41```yuescript 41```yuescript
42func_a! 42func_a!
@@ -51,7 +51,7 @@ func_b()
51 51
52</YueDisplay> 52</YueDisplay>
53 53
54Functions with arguments can be created by preceding the arrow with a list of argument names in parentheses: 54Fungsi dengan argumen dapat dibuat dengan menaruh daftar nama argumen dalam tanda kurung sebelum panah:
55 55
56```yuescript 56```yuescript
57sum = (x, y) -> print "sum", x + y 57sum = (x, y) -> print "sum", x + y
@@ -64,7 +64,7 @@ sum = (x, y) -> print "sum", x + y
64 64
65</YueDisplay> 65</YueDisplay>
66 66
67Functions can be called by listing the arguments after the name of an expression that evaluates to a function. When chaining together function calls, the arguments are applied to the closest function to the left. 67Fungsi dapat dipanggil dengan menuliskan argumen setelah nama ekspresi yang mengevaluasi ke fungsi. Saat merangkai pemanggilan fungsi, argumen diterapkan ke fungsi terdekat di sebelah kiri.
68 68
69```yuescript 69```yuescript
70sum 10, 20 70sum 10, 20
@@ -83,7 +83,7 @@ a b c "a", "b", "c"
83 83
84</YueDisplay> 84</YueDisplay>
85 85
86In order to avoid ambiguity in when calling functions, parentheses can also be used to surround the arguments. This is required here in order to make sure the right arguments get sent to the right functions. 86Untuk menghindari ambiguitas saat memanggil fungsi, tanda kurung juga bisa digunakan untuk mengelilingi argumen. Ini diperlukan di sini agar argumen yang tepat dikirim ke fungsi yang tepat.
87 87
88```yuescript 88```yuescript
89print "x:", sum(10, 20), "y:", sum(30, 40) 89print "x:", sum(10, 20), "y:", sum(30, 40)
@@ -96,9 +96,9 @@ print "x:", sum(10, 20), "y:", sum(30, 40)
96 96
97</YueDisplay> 97</YueDisplay>
98 98
99There must not be any space between the opening parenthesis and the function. 99Tidak boleh ada spasi antara tanda kurung buka dan nama fungsi.
100 100
101Functions will coerce the last statement in their body into a return statement, this is called implicit return: 101Fungsi akan memaksa pernyataan terakhir di badannya menjadi pernyataan return, ini disebut return implisit:
102 102
103```yuescript 103```yuescript
104sum = (x, y) -> x + y 104sum = (x, y) -> x + y
@@ -113,7 +113,7 @@ print "The sum is ", sum 10, 20
113 113
114</YueDisplay> 114</YueDisplay>
115 115
116And if you need to explicitly return, you can use the return keyword: 116Dan jika Anda perlu return secara eksplisit, Anda bisa menggunakan kata kunci `return`:
117 117
118```yuescript 118```yuescript
119sum = (x, y) -> return x + y 119sum = (x, y) -> return x + y
@@ -126,7 +126,7 @@ sum = (x, y) -> return x + y
126 126
127</YueDisplay> 127</YueDisplay>
128 128
129Just like in Lua, functions can return multiple values. The last statement must be a list of values separated by commas: 129Seperti di Lua, fungsi dapat mengembalikan beberapa nilai. Pernyataan terakhir harus berupa daftar nilai yang dipisahkan koma:
130 130
131```yuescript 131```yuescript
132mystery = (x, y) -> x + y, x - y 132mystery = (x, y) -> x + y, x - y
@@ -141,9 +141,9 @@ a, b = mystery 10, 20
141 141
142</YueDisplay> 142</YueDisplay>
143 143
144## Fat Arrows 144## Panah Tebal
145 145
146Because it is an idiom in Lua to send an object as the first argument when calling a method, a special syntax is provided for creating functions which automatically includes a self argument. 146Karena sudah menjadi idiom di Lua untuk mengirim objek sebagai argumen pertama saat memanggil method, disediakan sintaks khusus untuk membuat fungsi yang otomatis menyertakan argumen `self`.
147 147
148```yuescript 148```yuescript
149func = (num) => @value + num 149func = (num) => @value + num
@@ -156,9 +156,9 @@ func = (num) => @value + num
156 156
157</YueDisplay> 157</YueDisplay>
158 158
159## Argument Defaults 159## Nilai Default Argumen
160 160
161It is possible to provide default values for the arguments of a function. An argument is determined to be empty if its value is nil. Any nil arguments that have a default value will be replace before the body of the function is run. 161Dimungkinkan untuk menyediakan nilai default bagi argumen fungsi. Argumen dianggap kosong jika nilainya nil. Argumen nil yang memiliki nilai default akan diganti sebelum badan fungsi dijalankan.
162 162
163```yuescript 163```yuescript
164my_function = (name = "something", height = 100) -> 164my_function = (name = "something", height = 100) ->
@@ -175,7 +175,7 @@ my_function = (name = "something", height = 100) ->
175 175
176</YueDisplay> 176</YueDisplay>
177 177
178An argument default value expression is evaluated in the body of the function in the order of the argument declarations. For this reason default values have access to previously declared arguments. 178Ekspresi nilai default argumen dievaluasi di dalam badan fungsi sesuai urutan deklarasi argumen. Karena itu, nilai default dapat mengakses argumen yang dideklarasikan sebelumnya.
179 179
180```yuescript 180```yuescript
181some_args = (x = 100, y = x + 1000) -> 181some_args = (x = 100, y = x + 1000) ->
@@ -190,11 +190,11 @@ some_args = (x = 100, y = x + 1000) ->
190 190
191</YueDisplay> 191</YueDisplay>
192 192
193## Considerations 193## Pertimbangan
194 194
195Because of the expressive parentheses-less way of calling functions, some restrictions must be put in place to avoid parsing ambiguity involving whitespace. 195Karena cara pemanggilan fungsi tanpa tanda kurung yang ekspresif, beberapa pembatasan harus diterapkan untuk menghindari ambiguitas parsing yang melibatkan spasi.
196 196
197The minus sign plays two roles, a unary negation operator and a binary subtraction operator. Consider how the following examples compile: 197Tanda minus memiliki dua peran, operator negasi unari dan operator pengurangan biner. Perhatikan bagaimana contoh berikut dikompilasi:
198 198
199```yuescript 199```yuescript
200a = x - 10 200a = x - 10
@@ -213,11 +213,11 @@ d = x- z
213 213
214</YueDisplay> 214</YueDisplay>
215 215
216The precedence of the first argument of a function call can be controlled using whitespace if the argument is a literal string. In Lua, it is common to leave off parentheses when calling a function with a single string or table literal. 216Prioritas argumen pertama pada pemanggilan fungsi dapat dikendalikan menggunakan spasi jika argumennya adalah literal string. Di Lua, sudah umum untuk menghilangkan tanda kurung saat memanggil fungsi dengan satu literal string atau tabel.
217 217
218When there is no space between a variable and a string literal, the function call takes precedence over any following expressions. No other arguments can be passed to the function when it is called this way. 218Ketika tidak ada spasi antara variabel dan literal string, pemanggilan fungsi akan memiliki prioritas atas ekspresi yang mengikuti. Tidak ada argumen lain yang dapat diberikan pada fungsi ketika dipanggil dengan cara ini.
219 219
220Where there is a space following a variable and a string literal, the function call acts as show above. The string literal belongs to any following expressions (if they exist), which serves as the argument list. 220Ketika ada spasi setelah variabel dan literal string, pemanggilan fungsi bertindak seperti yang dijelaskan di atas. Literal string menjadi milik ekspresi berikutnya (jika ada), yang berfungsi sebagai daftar argumen.
221 221
222```yuescript 222```yuescript
223x = func"hello" + 100 223x = func"hello" + 100
@@ -232,11 +232,11 @@ y = func "hello" + 100
232 232
233</YueDisplay> 233</YueDisplay>
234 234
235## Multi-line arguments 235## Argumen Multi-baris
236 236
237When calling functions that take a large number of arguments, it is convenient to split the argument list over multiple lines. Because of the white-space sensitive nature of the language, care must be taken when splitting up the argument list. 237Saat memanggil fungsi yang menerima banyak argumen, akan lebih nyaman untuk memecah daftar argumen menjadi beberapa baris. Karena sifat bahasa yang peka terhadap spasi, perlu hati-hati saat memecah daftar argumen.
238 238
239If an argument list is to be continued onto the next line, the current line must end in a comma. And the following line must be indented more than the current indentation. Once indented, all other argument lines must be at the same level of indentation to be part of the argument list 239Jika daftar argumen akan dilanjutkan ke baris berikutnya, baris saat ini harus diakhiri dengan koma. Dan baris berikutnya harus lebih terindentasi daripada indentasi saat ini. Setelah diindentasi, semua baris argumen lainnya harus berada pada tingkat indentasi yang sama agar menjadi bagian dari daftar argumen.
240 240
241```yuescript 241```yuescript
242my_func 5, 4, 3, 242my_func 5, 4, 3,
@@ -261,7 +261,7 @@ cool_func 1, 2,
261 261
262</YueDisplay> 262</YueDisplay>
263 263
264This type of invocation can be nested. The level of indentation is used to determine to which function the arguments belong to. 264Jenis pemanggilan ini dapat dinest. Tingkat indentasi digunakan untuk menentukan argumen milik fungsi yang mana.
265 265
266```yuescript 266```yuescript
267my_func 5, 6, 7, 267my_func 5, 6, 7,
@@ -280,7 +280,7 @@ my_func 5, 6, 7,
280 280
281</YueDisplay> 281</YueDisplay>
282 282
283Because tables also use the comma as a delimiter, this indentation syntax is helpful for letting values be part of the argument list instead of being part of the table. 283Karena tabel juga menggunakan koma sebagai pemisah, sintaks indentasi ini membantu agar nilai menjadi bagian dari daftar argumen, bukan bagian dari tabel.
284 284
285```yuescript 285```yuescript
286x = [ 286x = [
@@ -301,7 +301,7 @@ x = [
301 301
302</YueDisplay> 302</YueDisplay>
303 303
304Although uncommon, notice how we can give a deeper indentation for function arguments if we know we will be using a lower indentation further on. 304Meskipun jarang, perhatikan bahwa kita bisa memberikan indentasi yang lebih dalam untuk argumen fungsi jika kita tahu akan menggunakan indentasi yang lebih dangkal di bagian selanjutnya.
305 305
306```yuescript 306```yuescript
307y = [ my_func 1, 2, 3, 307y = [ my_func 1, 2, 3,
@@ -320,7 +320,7 @@ y = [ my_func 1, 2, 3,
320 320
321</YueDisplay> 321</YueDisplay>
322 322
323The same thing can be done with other block level statements like conditionals. We can use indentation level to determine what statement a value belongs to: 323Hal yang sama juga dapat dilakukan pada pernyataan tingkat blok lainnya seperti kondisional. Kita bisa menggunakan tingkat indentasi untuk menentukan nilai milik pernyataan apa:
324 324
325```yuescript 325```yuescript
326if func 1, 2, 3, 326if func 1, 2, 3,
@@ -353,13 +353,13 @@ if func 1, 2, 3,
353 353
354</YueDisplay> 354</YueDisplay>
355 355
356## Parameter Destructuring 356## Destrukturisasi Parameter
357 357
358YueScript now supports destructuring function parameters when the argument is an object. Two forms of destructuring table literals are available: 358YueScript kini mendukung destrukturisasi parameter fungsi ketika argumen berupa objek. Dua bentuk destrukturisasi literal tabel tersedia:
359 359
360* **Curly-brace wrapped literals/object parameters**, allowing optional default values when fields are missing (e.g., `{:a, :b}`, `{a: a1 = 123}`). 360* **Literal berkurung kurawal/parameter objek**, memungkinkan nilai default opsional ketika field hilang (misalnya, `{:a, :b}`, `{a: a1 = 123}`).
361 361
362* **Unwrapped simple table syntax**, starting with a sequence of key-value or shorthand bindings and continuing until another expression terminates it (e.g., `:a, b: b1, :c`). This form extracts multiple fields from the same object. 362* **Sintaks tabel sederhana tanpa pembungkus**, dimulai dengan urutan key-value atau binding singkat dan berlanjut sampai ekspresi lain menghentikannya (misalnya, `:a, b: b1, :c`). Bentuk ini mengekstrak beberapa field dari objek yang sama.
363 363
364```yuescript 364```yuescript
365f1 = (:a, :b, :c) -> 365f1 = (:a, :b, :c) ->
@@ -390,9 +390,9 @@ f2 arg1, arg2
390 390
391</YueDisplay> 391</YueDisplay>
392 392
393## Prefixed Return Expression 393## Ekspresi Return Berawalan
394 394
395When working with deeply nested function bodies, it can be tedious to maintain readability and consistency of the return value. To address this, YueScript introduces the **Prefixed Return Expression** syntax. Its form is as follows: 395Saat bekerja dengan badan fungsi yang sangat bertingkat, menjaga keterbacaan dan konsistensi nilai return bisa terasa melelahkan. Untuk mengatasinya, YueScript memperkenalkan sintaks **Ekspresi Return Berawalan**. Bentuknya sebagai berikut:
396 396
397```yuescript 397```yuescript
398findFirstEven = (list): nil -> 398findFirstEven = (list): nil ->
@@ -415,7 +415,7 @@ findFirstEven = (list): nil ->
415 415
416</YueDisplay> 416</YueDisplay>
417 417
418This is equivalent to: 418Ini setara dengan:
419 419
420```yuescript 420```yuescript
421findFirstEven = (list) -> 421findFirstEven = (list) ->
@@ -440,11 +440,11 @@ findFirstEven = (list) ->
440 440
441</YueDisplay> 441</YueDisplay>
442 442
443The only difference is that you can move the final return expression before the `->` or `=>` token to indicate the function’s implicit return value as the last statement. This way, even in functions with multiple nested loops or conditional branches, you no longer need to write a trailing return expression at the end of the function body, making the logic structure more straightforward and easier to follow. 443Satu-satunya perbedaan adalah Anda dapat memindahkan ekspresi return terakhir sebelum token `->` atau `=>` untuk menunjukkan nilai return implisit fungsi sebagai pernyataan terakhir. Dengan cara ini, bahkan pada fungsi dengan banyak loop bertingkat atau cabang kondisional, Anda tidak lagi perlu menulis ekspresi return di akhir badan fungsi, sehingga struktur logika menjadi lebih lurus dan mudah diikuti.
444 444
445## Named Varargs 445## Varargs Bernama
446 446
447You can use the `(...t) ->` syntax to automatically store varargs into a named table. This table will contain all passed arguments (including `nil` values), and the `n` field of the table will store the actual number of arguments passed (including `nil` values). 447Anda dapat menggunakan sintaks `(...t) ->` untuk otomatis menyimpan varargs ke tabel bernama. Tabel ini berisi semua argumen yang diteruskan (termasuk nilai `nil`), dan field `n` pada tabel menyimpan jumlah argumen yang benar-benar diteruskan (termasuk nilai `nil`).
448 448
449```yuescript 449```yuescript
450f = (...t) -> 450f = (...t) ->
@@ -457,7 +457,7 @@ f 1, 2, 3
457f "a", "b", "c", "d" 457f "a", "b", "c", "d"
458f! 458f!
459 459
460-- Handling cases with nil values 460-- Menangani kasus dengan nilai nil
461process = (...args) -> 461process = (...args) ->
462 sum = 0 462 sum = 0
463 for i = 1, args.n 463 for i = 1, args.n
@@ -480,7 +480,7 @@ f 1, 2, 3
480f "a", "b", "c", "d" 480f "a", "b", "c", "d"
481f! 481f!
482 482
483-- Handling cases with nil values 483-- Menangani kasus dengan nilai nil
484process = (...args) -> 484process = (...args) ->
485 sum = 0 485 sum = 0
486 for i = 1, args.n 486 for i = 1, args.n