자바 개발자를위한 함수형 프로그래밍, Part 1

Java 8은 Java 개발자에게 람다 식을 사용한 함수형 프로그래밍을 도입했습니다. 이 Java 릴리스는 명령형 객체 지향 관점에서만 Java 프로그래밍에 대해 생각하는 것만으로는 더 이상 충분하지 않다는 사실을 개발자에게 효과적으로 알 렸습니다. Java 개발자는 선언적 기능 패러다임을 사용하여 사고하고 코딩 할 수 있어야합니다.

이 튜토리얼은 함수형 프로그래밍의 기초를 보여줍니다. 용어로 시작한 다음 함수형 프로그래밍 개념을 파헤칠 것입니다. 5 가지 함수형 프로그래밍 기법을 소개하는 것으로 마무리하겠습니다. 이 섹션의 코드 예제는 순수 함수, 고차 함수, 지연 평가, 클로저 및 커링으로 시작하는 데 도움이됩니다.

함수형 프로그래밍이 증가하고 있습니다.

IEEE (Institute of Electrical and Electronics Engineers)는 2018 년 상위 25 개 프로그래밍 언어에 함수형 프로그래밍 언어를 포함했으며 현재 Google Trends는 함수형 프로그래밍을 객체 지향 프로그래밍보다 더 인기있는 것으로 평가했습니다.

분명히 함수형 프로그래밍은 무시할 수 없지만 왜 더 인기를 얻고 있습니까? 무엇보다도 함수형 프로그래밍을 사용하면 프로그램의 정확성을 쉽게 확인할 수 있습니다. 또한 동시 프로그램 작성을 단순화합니다. 동시성 (또는 병렬 처리)은 애플리케이션 성능을 향상시키는 데 필수적입니다.

다운로드 코드 받기이 자습서에서 예제 응용 프로그램의 소스 코드를 다운로드합니다. JavaWorld를 위해 Jeff Friesen이 만들었습니다.

함수형 프로그래밍이란 무엇입니까?

컴퓨터는 일반적으로 수학자이자 물리학자인 John von Neumann (및 기타)의 1945 년 설명을 기반으로 널리 사용되는 컴퓨터 아키텍처 인 Von Neumann 아키텍처를 구현합니다. 이 아키텍처는 명령문을 사용하여 프로그램의 상태를 변경하는 프로그래밍 패러다임 인 명령형 프로그래밍에 편향 되어 있습니다. C, C ++ 및 Java는 모두 명령형 프로그래밍 언어입니다.

1977 년 저명한 컴퓨터 과학자 John Backus (FORTRAN에 대한 그의 연구로 유명)는 "프로그래밍이 폰 노이만 스타일에서 해방 될 수 있는가?"라는 제목의 강의를했습니다. Backus는 Von Neumann 아키텍처 및 관련 명령형 언어가 근본적으로 결함이 있으며 솔루션으로 기능 수준 프로그래밍 언어 (FP)를 제시했다고 주장했습니다.

Backus 명확화

Backus 강의는 수십 년 전에 발표 되었기 때문에 그 아이디어 중 일부는 이해하기 어려울 수 있습니다. Blogger Tomasz Jaskuła는 2018 년 1 월 블로그 게시물에 명확성과 각주를 추가합니다.

함수형 프로그래밍 개념 및 용어

함수형 프로그래밍 은 계산이 함수형 프로그래밍 함수 로 코드화 된 프로그래밍 스타일입니다 . 이들은 표현식 컨텍스트에서 평가되는 수학 함수와 유사한 구조 (예 : 람다 함수)입니다.

함수형 프로그래밍 언어는 선언적입니다 . 즉, 계산 논리가 제어 흐름을 설명하지 않고 표현됩니다. 선언적 프로그래밍에는 문이 없습니다. 대신 프로그래머는 표현식을 사용하여 수행해야 할 작업을 컴퓨터에 알리지 만 작업을 수행하는 방법은 아닙니다. SQL 또는 정규식에 익숙하다면 선언적 스타일에 대한 경험이 있습니다. 둘 다 수행 방법을 설명하기 위해 명령문을 사용하는 대신 수행해야 할 작업을 설명하기 위해 표현식을 사용합니다.

함수형 프로그래밍 의 계산 은 식 컨텍스트에서 평가되는 함수로 설명됩니다. 이러한 함수는 값을 반환하는 Java 메서드와 같은 명령형 프로그래밍에 사용되는 함수와 동일하지 않습니다. 대신 함수형 프로그래밍 함수는 일반적으로 인수에만 의존하는 출력을 생성하는 수학 함수와 같습니다. 함수형 프로그래밍 함수가 동일한 인수를 사용하여 호출 될 때마다 동일한 결과를 얻습니다. 함수형 프로그래밍의 함수는 참조 투명성 을 나타낸다고 합니다. 즉, 계산의 의미를 변경하지 않고 함수 호출을 결과 값으로 바꿀 수 있습니다.

함수형 프로그래밍은 불변성을 선호 하므로 상태를 변경할 수 없습니다. 이는 일반적으로 명령형 함수가 상태 (예 : Java 인스턴스 변수)와 연관 될 수있는 명령형 프로그래밍의 경우가 아닙니다. 동일한 인수를 사용하여 다른 시간에이 함수를 호출하면이 경우 상태가 변경 가능 하므로 다른 반환 값이 발생할 수 있습니다 .

명령형 및 함수형 프로그래밍의 부작용

상태 변경은 명령형 프로그래밍의 부작용으로 참조 투명성을 방해합니다. 특히 프로그램에서 명령형 또는 기능적 스타일을 사용할지 여부를 평가할 때 알아야 할 다른 많은 부작용이 있습니다.

명령형 프로그래밍의 일반적인 부작용 중 하나는 할당 문이 저장된 값을 변경하여 변수를 변경하는 경우입니다. 함수형 프로그래밍의 함수는 변수 할당을 지원하지 않습니다. 변수의 초기 값은 절대 변경되지 않기 때문에 함수형 프로그래밍은 이러한 부작용을 제거합니다.

또 다른 일반적인 부작용은 호출자와의 관찰 가능한 상호 작용 인 throw 된 예외를 기반으로 명령형 함수의 동작을 수정할 때 발생합니다. 자세한 내용은 스택 오버플로 토론, "예외 발생이 부작용 인 이유는 무엇입니까?"를 참조하십시오.

세 번째 일반적인 부작용은 I / O 작업에서 읽을 수없는 텍스트를 입력하거나 쓸 수없는 텍스트를 출력 할 때 발생합니다. Stack Exchange 토론 "IO가 함수형 프로그래밍에서 어떻게 부작용을 일으킬 수 있습니까?"를 참조하십시오. 이 부작용에 대해 자세히 알아보십시오.

부작용을 제거하면 계산 동작을 훨씬 쉽게 이해하고 예측할 수 있습니다. 또한 코드를 병렬 처리에 더 적합하게 만들어 종종 애플리케이션 성능을 향상시킵니다. 함수형 프로그래밍에는 부작용이 있지만 일반적으로 명령형 프로그래밍보다 부작용이 적습니다. 함수형 프로그래밍을 사용하면 이해, 유지 관리 및 테스트가 더 쉽고 재사용이 가능한 코드를 작성할 수 있습니다.

함수형 프로그래밍의 기원 (및 기원)

함수형 프로그래밍은 Alonzo Church에서 소개 한 람다 미적분에서 시작되었습니다. 또 다른 기원은 모세 쇤 핑켈이 소개 한 뒤 하스켈 커리가 개발 한 조합 논리입니다.

객체 지향 프로그래밍과 함수 프로그래밍

필자는 코드 작성에 대한 명령형, 객체 지향적 , 선언적, 기능적 프로그래밍 접근 방식 과 대조되는 Java 애플리케이션을 만들었습니다 . 아래 코드를 연구 한 다음 두 예제의 차이점을 지적하겠습니다.

목록 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)); } }

목록 1은 Employees몇 개의 Employee객체 를 생성 한 다음 50 세 미만의 모든 직원 목록을 인쇄 하는 애플리케이션을 보여줍니다.이 코드는 객체 지향 및 함수 프로그래밍 스타일을 모두 보여줍니다.

The printEmployee1() method reveals the imperative, statement-oriented approach. As specified, this method iterates over a list of employees, compares each employee's age against an argument value, and (if the age is less than the argument), prints the employee's details.

The printEmployee2() method reveals the declarative, expression-oriented approach, in this case implemented with the Streams API. Instead of imperatively specifying how to print the employees (step-by-step), the expression specifies the desired outcome and leaves the details of how to do it to Java. Think of filter() as the functional equivalent of an if statement, and forEach() as functionally equivalent to the for statement.

You can compile Listing 1 as follows:

javac Employees.java

Use the following command to run the resulting application:

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 .