Pemrograman fungsional untuk pengembang Java, Bagian 1

Java 8 memperkenalkan pengembang Java ke pemrograman fungsional dengan ekspresi lambda. Rilis Java ini secara efektif memberi tahu pengembang bahwa tidak lagi cukup untuk memikirkan tentang pemrograman Java hanya dari perspektif berorientasi objek yang penting. Pengembang Java juga harus dapat berpikir dan membuat kode menggunakan paradigma fungsional deklaratif.

Tutorial ini menyajikan dasar-dasar pemrograman fungsional. Saya akan mulai dengan terminologi, lalu kita akan menggali konsep pemrograman fungsional. Saya akan menyimpulkan dengan memperkenalkan Anda pada lima teknik pemrograman fungsional. Contoh kode di bagian ini akan membantu Anda memulai dengan fungsi murni, fungsi tingkat tinggi, evaluasi malas, penutupan, dan kari.

Pemrograman fungsional sedang meningkat

Institute of Electrical and Electronics Engineers (IEEE) memasukkan bahasa pemrograman fungsional dalam 25 bahasa pemrograman teratasnya untuk tahun 2018, dan Google Trends saat ini menempatkan pemrograman fungsional sebagai lebih populer daripada pemrograman berorientasi objek.

Jelas, pemrograman fungsional tidak dapat diabaikan, tetapi mengapa itu menjadi lebih populer? Antara lain, pemrograman fungsional membuatnya lebih mudah untuk memverifikasi kebenaran program. Ini juga menyederhanakan pembuatan program bersamaan. Konkurensi (atau pemrosesan paralel) sangat penting untuk meningkatkan kinerja aplikasi.

unduh Dapatkan kodenya Unduh kode sumber untuk aplikasi contoh dalam tutorial ini. Dibuat oleh Jeff Friesen untuk JavaWorld.

Apa itu pemrograman fungsional?

Komputer biasanya menerapkan arsitektur Von Neumann, yang merupakan arsitektur komputer yang banyak digunakan berdasarkan deskripsi tahun 1945 oleh ahli matematika dan fisikawan John von Neumann (dan lainnya). Arsitektur ini bias ke arah pemrograman imperatif , yang merupakan paradigma pemrograman yang menggunakan pernyataan untuk mengubah status program. C, C ++, dan Java adalah bahasa pemrograman penting.

Pada tahun 1977, ilmuwan komputer terkemuka John Backus (terkenal karena karyanya di FORTRAN), memberikan ceramah berjudul "Dapatkah pemrograman dibebaskan dari gaya von Neumann ?." Backus menegaskan bahwa arsitektur Von Neumann dan bahasa imperatif yang terkait pada dasarnya cacat, dan menyajikan bahasa pemrograman tingkat fungsional (FP) sebagai solusi.

Memperjelas Backus

Karena kuliah Backus dipresentasikan beberapa dekade lalu, beberapa idenya mungkin sulit untuk dipahami. Blogger Tomasz Jaskuła menambahkan kejelasan dan catatan kaki dalam postingan blognya mulai Januari 2018.

Konsep dan terminologi pemrograman fungsional

Pemrograman fungsional adalah gaya pemrograman di mana perhitungan dikodifikasi sebagai fungsi pemrograman fungsional . Ini adalah konstruksi seperti fungsi matematika (misalnya, fungsi lambda) yang dievaluasi dalam konteks ekspresi.

Bahasa pemrograman fungsional bersifat deklaratif , artinya logika komputasi diekspresikan tanpa menjelaskan aliran kontrolnya. Dalam pemrograman deklaratif, tidak ada pernyataan. Sebaliknya, pemrogram menggunakan ekspresi untuk memberi tahu komputer apa yang perlu dilakukan, tetapi bukan bagaimana menyelesaikan tugas. Jika Anda terbiasa dengan SQL atau ekspresi reguler, maka Anda memiliki pengalaman dengan gaya deklaratif; keduanya menggunakan ekspresi untuk mendeskripsikan apa yang perlu dilakukan, daripada menggunakan pernyataan untuk mendeskripsikan bagaimana melakukannya.

Sebuah perhitungan dalam pemrograman fungsional digambarkan oleh fungsi yang dievaluasi dalam konteks ekspresi. Fungsi ini tidak sama dengan fungsi yang digunakan dalam pemrograman imperatif, seperti metode Java yang mengembalikan nilai. Sebaliknya, fungsi pemrograman fungsional seperti fungsi matematika, yang menghasilkan keluaran yang biasanya hanya bergantung pada argumennya. Setiap kali fungsi pemrograman fungsional dipanggil dengan argumen yang sama, hasil yang sama dicapai. Fungsi dalam pemrograman fungsional dikatakan menunjukkan transparansi referensial . Ini berarti Anda bisa mengganti pemanggilan fungsi dengan nilai yang dihasilkan tanpa mengubah makna komputasi.

Pemrograman fungsional mendukung kekekalan , yang berarti keadaan tidak dapat berubah. Ini biasanya tidak terjadi dalam pemrograman imperatif, di mana fungsi imperatif mungkin dikaitkan dengan status (seperti variabel instance Java). Memanggil fungsi ini pada waktu yang berbeda dengan argumen yang sama dapat menghasilkan nilai kembalian yang berbeda karena dalam kasus ini statusnya bisa berubah , artinya berubah.

Efek samping dalam pemrograman imperatif dan fungsional

Perubahan status adalah efek samping dari pemrograman imperatif, mencegah transparansi referensial. Ada banyak efek samping lain yang perlu diketahui, terutama saat Anda mengevaluasi apakah akan menggunakan gaya imperatif atau fungsional dalam program Anda.

Salah satu efek samping yang umum dalam pemrograman imperatif adalah ketika pernyataan penugasan mengubah variabel dengan mengubah nilai yang disimpan. Fungsi dalam pemrograman fungsional tidak mendukung tugas variabel. Karena nilai awal variabel tidak pernah berubah, pemrograman fungsional menghilangkan efek samping ini.

Efek samping umum lainnya terjadi saat memodifikasi perilaku fungsi imperatif berdasarkan pengecualian yang muncul, yang merupakan interaksi yang dapat diamati dengan pemanggil. Untuk informasi lebih lanjut, lihat diskusi Stack Overflow, "Mengapa munculnya pengecualian merupakan efek samping?"

Efek samping umum ketiga terjadi ketika operasi I / O memasukkan teks yang tidak bisa belum dibaca, atau mengeluarkan teks yang tidak bisa ditulis. Lihat diskusi Stack Exchange "Bagaimana IO dapat menyebabkan efek samping dalam pemrograman fungsional?" untuk mempelajari lebih lanjut tentang efek samping ini.

Menghilangkan efek samping membuatnya lebih mudah untuk memahami dan memprediksi perilaku komputasi. Ini juga membantu membuat kode lebih sesuai untuk pemrosesan paralel, yang sering kali meningkatkan kinerja aplikasi. Meskipun ada efek samping dalam pemrograman fungsional, umumnya lebih sedikit daripada dalam pemrograman imperatif. Menggunakan pemrograman fungsional dapat membantu Anda menulis kode yang lebih mudah dipahami, dipelihara, dan diuji, dan juga lebih dapat digunakan kembali.

Asal (dan pencetus) pemrograman fungsional

Pemrograman fungsional berasal dari kalkulus lambda, yang diperkenalkan oleh Gereja Alonzo. Asal usul lainnya adalah logika kombinatori, yang diperkenalkan oleh Moses Schönfinkel dan kemudian dikembangkan oleh Haskell Curry.

Pemrograman berorientasi objek versus fungsional

Saya telah membuat aplikasi Java yang membedakan pendekatan pemrograman fungsional yang imperatif, berorientasi objek, dan deklaratif untuk menulis kode. Pelajari kode di bawah ini dan kemudian saya akan menunjukkan perbedaan antara kedua contoh tersebut.

Daftar 1. Employees.java

import java.util.ArrayList; import java.util.List; public class Employees { static class Employee { private String name; private int age; Employee(String name, int age) { this.name = name; this.age = age; } int getAge() { return age; } @Override public String toString() { return name + ": " + age; } } public static void main(String[] args) { List employees = new ArrayList(); employees.add(new Employee("John Doe", 63)); employees.add(new Employee("Sally Smith", 29)); employees.add(new Employee("Bob Jone", 36)); employees.add(new Employee("Margaret Foster", 53)); printEmployee1(employees, 50); System.out.println(); printEmployee2(employees, 50); } public static void printEmployee1(List employees, int age) { for (Employee emp: employees) if (emp.getAge() < age) System.out.println(emp); } public static void printEmployee2(List employees, int age) { employees.stream() .filter(emp -> emp.age  System.out.println(emp)); } }

Kode 1 menunjukkan Employeesaplikasi yang membuat beberapa Employeeobjek, kemudian mencetak daftar semua karyawan yang lebih muda dari 50. Kode ini menunjukkan gaya pemrograman berorientasi objek dan fungsional.

The printEmployee1()Metode mengungkapkan imperatif pendekatan, pernyataan-oriented. Seperti yang ditentukan, metode ini mengulang daftar karyawan, membandingkan usia setiap karyawan dengan nilai argumen, dan (jika usia kurang dari argumen), mencetak detail karyawan.

The printEmployee2()Metode mengungkapkan deklaratif pendekatan, ekspresi berorientasi, dalam hal ini dilaksanakan dengan Streaming API. Alih-alih menentukan cara mencetak karyawan (langkah demi langkah), ekspresi menentukan hasil yang diinginkan dan memberikan detail cara melakukannya ke Java. Anggap filter()sebagai padanan fungsional dari sebuah ifpernyataan, dan forEach()secara fungsional setara dengan forpernyataan tersebut.

Anda dapat menyusun Daftar 1 sebagai berikut:

javac Employees.java

Gunakan perintah berikut untuk menjalankan aplikasi yang dihasilkan:

java Employees

The output should look something like this:

Sally Smith: 29 Bob Jone: 36 Sally Smith: 29 Bob Jone: 36

Functional programming examples

In the next sections, we'll explore five core techniques used in functional programming: pure functions, higher-order functions, lazy evaluation, closures, and currying. Examples in this section are coded in JavaScript because its simplicity, relative to Java, will allow us to focus on the techniques. In Part 2 we'll revisit these same techniques using Java code.

Listing 2 presents the source code to RunScript, a Java application that uses Java's Scripting API to facilitate running JavaScript code. RunScript will be the base program for all of the forthcoming examples.

Listing 2. RunScript.java

import java.io.FileReader; import java.io.IOException; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import static java.lang.System.*; public class RunScript { public static void main(String[] args) { if (args.length != 1) { err.println("usage: java RunScript script"); return; } ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); try { engine.eval(new FileReader(args[0])); } catch (ScriptException se) { err.println(se.getMessage()); } catch (IOException ioe) { err.println(ioe.getMessage()); } } }

The main() method in this example first verifies that a single command-line argument (the name of a script file) has been specified. Otherwise, it displays usage information and terminates the application.

Assuming the presence of this argument, main() instantiates the javax.script.ScriptEngineManager class. ScriptEngineManager is the entry-point into Java's Scripting API.

Next, the ScriptEngineManager object's ScriptEngine getEngineByName(String shortName) method is called to obtain a script engine corresponding to the desired shortName value. Java 10 supports the Nashorn script engine, which is obtained by passing "nashorn" to getEngineByName(). The returned object's class implements the javax.script.ScriptEngine interface.

ScriptEngine declares several eval() methods for evaluating a script. main() invokes the Object eval(Reader reader) method to read the script from its java.io.FileReader object argument and (assuming that java.io.IOException isn't thrown) then evaluate the script. This method returns any script return value, which I ignore. Also, this method throws javax.script.ScriptException when an error occurs in the script.

Compile Listing 2 as follows:

javac RunScript.java

I'll show you how to run this application after I have presented the first script.

Functional programming with pure functions

A pure function is a functional programming function that depends only on its input arguments and no external state. An impure function is a functional programming function that violates either of these requirements. Because pure functions have no interaction with the outside world (apart from calling other pure functions), a pure function always returns the same result for the same arguments. Pure functions also have no observable side effects.

Can a pure function perform I/O?

If I/O is a side effect, can a pure function perform I/O? The answer is yes. Haskell uses monads to address this problem. See "Pure Functions and I/O" for more about pure functions and I/O.

Pure functions versus impure functions

The JavaScript in Listing 3 contrasts an impure calculatebonus() function with a pure calculatebonus2() function.

Listing 3. Comparing pure vs impure functions (script1.js)

// impure bonus calculation var limit = 100; function calculatebonus(numSales) { return(numSales > limit) ? 0.10 * numSales : 0 } print(calculatebonus(174)) // pure bonus calculation function calculatebonus2(numSales) { return (numSales > 100) ? 0.10 * numSales : 0 } print(calculatebonus2(174))

calculatebonus() is impure because it accesses the external limit variable. In contrast, calculatebonus2() is pure because it obeys both requirements for purity. Run script1.js as follows:

java RunScript script1.js

Here's the output you should observe:

17.400000000000002 17.400000000000002

Suppose calculatebonus2() was refactored to return calculatebonus(numSales). Would calculatebonus2() still be pure? The answer is no: when a pure function invokes an impure function, the "pure function" becomes impure.

When no data dependency exists between pure functions, they can be evaluated in any order without affecting the outcome, making them suitable for parallel execution. This is one of functional programming's benefits.

More about impure functions

Not all functional programming functions need to be pure. As Functional Programming: Pure Functions explains, it is possible (and sometimes desirable) to "separate the pure, functional, value based core of your application from an outer, imperative shell."

Functional programming with higher-order functions

A higher-order function is a mathematical function that receives functions as arguments, returns a function to its caller, or both. One example is calculus's differential operator, d/dx, which returns the derivative of function f.

First-class functions are first-class citizens

Closely related to the mathematical higher-order function concept is the first-class function, which is a functional programming function that takes other functional programming functions as arguments and/or returns a functional programming function. First-class functions are first-class citizens because they can appear wherever other first-class program entities (e.g., numbers) can, including being assigned to a variable or being passed as an argument to or returned from a function.

The JavaScript in Listing 4 demonstrates passing anonymous comparison functions to a first-class sorting function.

Listing 4. Passing anonymous comparison functions (script2.js)

function sort(a, cmp) { for (var pass = 0; pass 
    
      pass; i--) if (cmp(a[i], a[pass]) < 0) { var temp = a[i] a[i] = a[pass] a[pass] = temp } } var a = [22, 91, 3, 45, 64, 67, -1] sort(a, function(i, j) { return i - j; }) a.forEach(function(entry) { print(entry) }) print('\n') sort(a, function(i, j) { return j - i; }) a.forEach(function(entry) { print(entry) }) print('\n') a = ["X", "E", "Q", "A", "P"] sort(a, function(i, j) { return i 
     
       j; }) a.forEach(function(entry) { print(entry) }) print('\n') sort(a, function(i, j) { return i > j ? -1 : i < j; }) a.forEach(function(entry) { print(entry) })
     
    

In this example, the initial sort() call receives an array as its first argument, followed by an anonymous comparison function. When called, the anonymous comparison function executes return i - j; to achieve an ascending sort. By reversing i and j, the second comparison function achieves a descending sort. The third and fourth sort() calls receive anonymous comparison functions that are slightly different in order to properly compare string values.

Run the script2.js example as follows:

java RunScript script2.js

Here's the expected output:

-1 3 22 45 64 67 91 91 67 64 45 22 3 -1 A E P Q X X Q P E A

Filter and map

Functional programming languages typically provide several useful higher-order functions. Two common examples are filter and map.

  • A filter processes a list in some order to produce a new list containing exactly those elements of the original list for which a given predicate (think Boolean expression) returns true.
  • A map applies a given function to each element of a list, returning a list of results in the same order.

JavaScript supports filtering and mapping functionality via the filter() and map() higher-order functions. Listing 5 demonstrates these functions for filtering out odd numbers and mapping numbers to their cubes.

Listing 5. Filtering and mapping (script3.js)

print([1, 2, 3, 4, 5, 6].filter(function(num) { return num % 2 == 0 })) print('\n') print([3, 13, 22].map(function(num) { return num * 3 }))

Run the script3.js example as follows:

java RunScript script3.js

You should observe the following output:

2,4,6 9,39,66

Reduce

Another common higher-order function is reduce, which is more commonly known as a fold. This function reduces a list to a single value.

Listing 6 uses JavaScript's reduce() higher-order function to reduce an array of numbers to a single number, which is then divided by the array's length to obtain an average.

Listing 6. Reducing an array of numbers to a single number (script4.js)

var numbers = [22, 30, 43] print(numbers.reduce(function(acc, curval) { return acc + curval }) / numbers.length)

Run Listing 6's script (in script4.js) as follows:

java RunScript script4.js

You should observe the following output:

31.666666666666668

You might think that the filter, map, and reduce higher-order functions obviate the need for if-else and various looping statements, and you would be right. Their internal implementations take care of decisions and iteration.

A higher-order function uses recursion to achieve iteration. A recursive function invokes itself, allowing an operation to repeat until it reaches a base case. You can also leverage recursion to achieve iteration in your functional code.

Functional programming with lazy evaluation

Another important functional programming feature is lazy evaluation (also known as nonstrict evaluation), which is the deferral of expression evaluation for as long as possible. Lazy evaluation offers several benefits, including these two:

  • Expensive (timewise) calculations can be deferred until they're absolutely necessary.
  • Unbounded collections are possible. They'll keep delivering elements for as long as they're requested to do so.

Lazy evaluation is integral to Haskell. It won't calculate anything (including a function's arguments before the function is called) unless it's strictly necessary to do so.

Java's Streams API capitalizes on lazy evaluation. A stream's intermediate operations (e.g., filter()) are always lazy; they don't do anything until a terminal operation (e.g., forEach()) is executed.

Although lazy evaluation is an important part of functional languages, even many imperative languages provide builtin support for some forms of laziness. For example, most programming languages support short-circuit evaluation in the context of the Boolean AND and OR operators. These operators are lazy, refusing to evaluate their right-hand operands when the left-hand operand is false (AND) or true (OR).

Listing 7 is an example of lazy evaluation in a JavaScript script.

Listing 7. Lazy evaluation in JavaScript (script5.js)

var a = false && expensiveFunction("1") var b = true && expensiveFunction("2") var c = false || expensiveFunction("3") var d = true || expensiveFunction("4") function expensiveFunction(id) { print("expensiveFunction() called with " + id) }

Run the code in script5.js as follows:

java RunScript script5.js

You should observe the following output:

expensiveFunction() called with 2 expensiveFunction() called with 3

Lazy evaluation is often combined with memoization, an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs reoccur.

Because lazy evaluation doesn't work with side effects (such as code that produces exceptions and I/O), imperative languages mainly use eager evaluation (also known as strict evaluation), where an expression is evaluated as soon as it's bound to a variable.

More about lazy evaluation and memoization

A Google search will reveal many useful discussions of lazy evaluation with or without memoization. One example is "Optimizing your JavaScript with functional programming."

Functional programming with closures

First-class functions are associated with the concept of a closure, which is a persistent scope that holds onto local variables even after the code execution has left the block in which the local variables were defined.

Crafting closures

Operationally, a closure is a record that stores a function and its environment. The environment maps each of the function's free variables (variables used locally, but defined in an enclosing scope) with the value or reference to which the variable's name was bound when the closure was created. It lets the function access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

To help clarify this concept, Listing 8 presents a JavaScript script that introduces a simple closure. The script is based on the example presented here.

Listing 8. A simple closure (script6.js)

function add(x) { function partialAdd(y) { return y + x } return partialAdd } var add10 = add(10) var add20 = add(20) print(add10(5)) print(add20(5))

Listing 8 defines a first-class function named add() with a parameter x and a nested function partialAdd(). The nested function partialAdd() has access to x because x is in add()'s lexical scope. Function add() returns a closure that contains a reference to partialAdd() and a copy of the environment around add(), in which x has the value assigned to it in a specific invocation of add().

Because add() returns a value of function type, variables add10 and add20 also have function type. The add10(5) invocation returns 15 because the invocation assigns 5 to parameter y in the call to partialAdd(), using the saved environment for partialAdd() where x is 10. The add20(5) invocation returns 25 because, although it also assigns 5 to y in the call to partialAdd(), it's now using another saved environment for partialAdd() where x is 20. Thus, while add10() and add20() use the same function partialAdd(), the associated environments differ and invoking the closures will bind x to two different values in the two invocations, evaluating the function to two different results.

Run Listing 8's script (in script6.js) as follows:

java RunScript script6.js

You should observe the following output:

15 25

Functional programming with currying

Currying is a way to translate the evaluation of a multi-argument function into the evaluation of an equivalent sequence of single-argument functions. For example, a function takes two arguments: x and y. Currying transforms the function into taking only x and returning a function that takes only y. Currying is related to but is not the same as partial application, which is the process of fixing a number of arguments to a function, producing another function of smaller arity.

Listing 9 presents a JavaScript script that demonstrates currying.

Listing 9. Currying in JavaScript (script7.js)

function multiply(x, y) { return x * y } function curried_multiply(x) { return function(y) { return x * y } } print(multiply(6, 7)) print(curried_multiply(6)(7)) var mul_by_4 = curried_multiply(4) print(mul_by_4(2))

The script presents a noncurried two-argument multiply() function, followed by a first-class curried_multiply() function that receives multiplicand argument x and returns a closure containing a reference to an anonymous function (that receives multiplier argument y) and a copy of the environment around curried_multiply(), in which x has the value assigned to it in an invocation of curried_multiply().

The rest of the script first invokes multiply() with two arguments and prints the result. It then invokes curried_multiply() in two ways:

  • curried_multiply(6)(7) results in curried_multiply(6) executing first. The returned closure executes the anonymous function with the closure's saved x value 6 being multiplied by 7.
  • var mul_by_4 = curried_multiply(4) executes curried_multiply(4) and assigns the closure to mul_by_4. mul_by_4(2) executes the anonymous function with the closure's 4 value and the passed argument 2.

Run Listing 9's script (in script7.js) as follows:

java RunScript script7.js

You should observe the following output:

42 42 8

Why use currying?

In his blog post "Why curry helps," Hugh Jackson observes that "little pieces can be configured and reused with ease, without clutter." Quora's "What are the advantages of currying in functional programming?" describes currying as "a cheap form of dependency injection," that eases the process of mapping/filtering/folding (and higher order functions generally). This Q&A also notes that currying "helps us create abstract functions."

In conclusion

In this tutorial you've learned some basics of functional programming. We've used examples in JavaScript to study five core functional programming techniques, which we'll further explore using Java code in Part 2. In addition to touring Java 8's functional programming capabilities, the second half of this tutorial will help you begin to think functionally, by converting an example of object-oriented Java code to its functional equivalent.

Learn more about functional programming

I found the book Introduction to Functional Programming (Richard Bird and Philip Wadler, Prentice Hall International Series in Computing Science, 1992) helpful in learning the basics of functional programming.

This story, "Functional programming for Java developers, Part 1" was originally published by JavaWorld .