diff --git a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/00_refactoring.md b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/00_refactoring.md index 86a163e..45aa807 100644 --- a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/00_refactoring.md +++ b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/00_refactoring.md @@ -19,8 +19,8 @@ In this series we cover the following conversions from the imperative to the fun | Tutorial |Imperative Style | Functional Style Equivalent | |------------------------------------------------------|-----------------|------------------------------| -| [Converting Simple Loops](id:refactoring.simple.loops) | `for()` | `range()` or `rangeClosed()` | -| [Converting Loops with Steps](id:refactoring.loops.withsteps) | `for(...i = i + ...)` | `iterate()` with `takeWhile()` | -| [Converting foreach with if](id:refactoring.foreach.withif) | `foreach(...) { if... }` | `stream()` with `filter()` | -| [Converting Iteration with transformation](id:refactoring.iteration.withtransformation) | `foreach(...) { ...transformation... }` | `stream()` with `map()` | +| [Converting Simple Loops](id:refactoring.simple.loops) | `for (---)` | `range()` or `rangeClosed()` | +| [Converting Loops with Steps](id:refactoring.loops.withsteps) | `for (...i = i + ...)` | `iterate()` with `takeWhile()` | +| [Converting for-each with if](id:refactoring.foreach.withif) | `for (...) { if... }` | `stream()` with `filter()` | +| [Converting Iteration with transformation](id:refactoring.iteration.withtransformation) | `for (...) { ...transformation... }` | `stream()` with `map()` | diff --git a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/01_converting_simple_loops.md b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/01_converting_simple_loops.md index 3988370..69201d3 100644 --- a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/01_converting_simple_loops.md +++ b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/01_converting_simple_loops.md @@ -33,7 +33,7 @@ In this tutorial we'll focus on simple loops. Let's start with the traditional for loop where we perform an action for values of an index over a range. ```java -for(int i = 0; i < 5; i++) { +for (int i = 0; i < 5; i++) { System.out.println(i); } ``` @@ -65,7 +65,7 @@ The functional style code is more concise, easier to read, and the intention is What if your `for` loop runs to include the ending value, like in the following code, you may wonder. ```java -for(int i = 0; i <= 5; i++) { +for (int i = 0; i <= 5; i++) { System.out.println(i); } ``` diff --git a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/02_converting_loops_with_steps.md b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/02_converting_loops_with_steps.md index 687c068..39cd8c2 100644 --- a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/02_converting_loops_with_steps.md +++ b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/02_converting_loops_with_steps.md @@ -30,19 +30,19 @@ When looping over a range of values, one at a time, the `range()` method of `Int Here's a loop that uses step to skip a few values in the desired range: ```java -for(int i = 0; i < 15; i = i + 3) { +for (int i = 0; i < 15; i = i + 3) { System.out.println(i); } ``` The value of the index variable `i` starts at `0` and then is incremented by `3` as the iteration moves forward. When you find yourself looking at a loop like that where the iteration is not over every single value in a range, but some values are skipped, consider using the `iterate()` method of `IntStream`. -Before we refactor the code, let's take a closer look at the `for()` loop in the previous code, but with a pair of imaginary glasses that let us look at potential uses for lambdas. +Before we refactor the code, let's take a closer look at the `for` loop in the previous code, but with a pair of imaginary glasses that let us look at potential uses for lambdas. ```java -//imaginary code -for(int i = 0; i < 15; i = i + 3) //imperative -for(seed, i -> i < 15, i -> i + 3) //functional +// Imaginary code +for (int i = 0; i < 15; i = i + 3) // Imperative +for (seed, i -> i < 15, i -> i + 3) // Functional ``` The first argument passed to the `for` loop is the starting value or the seed for the iteration and it can stay as is. The second argument is a predicate that tells the value of the index variable, `i`, should not exceed the value of `15`. We can replace that in the functional style with a `IntPredicate`. The third argument is incrementing the value of the index variable and that, in functional style, is simply a `IntUnaryOperator`. The `IntStream` interface has a `static` method named `iterate()` that nicely represents the imaginary code: `iterate(int seed, IntPredicate hasNext, IntUnaryOperator next)`. @@ -67,8 +67,8 @@ In addition to stepping over values, we often use an unbounded loop and that thr Let's take a look at the following imperative style loop which, in addition to the step, is unbounded and uses the `break` statement. ```java -for(int i = 0;; i = i + 3) { - if(i > 20) { +for (int i = 0;; i = i + 3) { + if (i > 20) { break; } diff --git a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/03_converting_foreach_with_if.md b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/03_converting_foreach_with_if.md index 4a20eb5..fbe3e23 100644 --- a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/03_converting_foreach_with_if.md +++ b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/03_converting_foreach_with_if.md @@ -20,24 +20,24 @@ author: ["VenkatSubramaniam"]   ## Iterating with foreach -In the previous articles in this [tutorial series](id:refactoring) we looked at converting loops written in the imperative style to the functional style. In this article we'll see how to convert an imperative style iteration using `foreach` to the functional style. In addition, we'll also see how to pick select elements using `if` transforms to the functional style. +In the previous articles in this [tutorial series](id:refactoring) we looked at converting loops written in the imperative style to the functional style. In this article we'll see how to convert an imperative style iteration using for-each to the functional style. In addition, we'll also see how to pick select elements using `if` transforms to the functional style. -Java 5 introduced the very popular `foreach` syntax. For example, to iterate over a collection of `String`s representing names, we'd write something like `for(String name: names)`. Under the hood, the `foreach` is converted, at the bytecode level, to use an `Iterator`—while the iterator tells us there is another element, fetch the next element for processing. In other words, the `foreach` is a nice concise syntax sugar for iteration with a `while` loop over the elements provided by an `Iterator`. We can convert a `foreach` into the functional style quite easily. Let's see how. +Java 5 introduced the very popular for-each syntax. For example, to iterate over a collection of `String`s representing names, we'd write something like `for (String name : names)`. Under the hood, the for-each is converted, at the bytecode level, to use an `Iterator`—while the iterator tells us there is another element, fetch the next element for processing. In other words, the for-each is a nice concise syntax sugar for iteration with a `while` loop over the elements provided by an `Iterator`. We can convert a for-each into the functional style quite easily. Let's see how.   ## From Imperative to Functional Style -Here's an example of iteration, using the `foreach`, over a collection of names: +Here's an example of iteration, using the for-each, over a collection of names: ```java List names = List.of("Jack", "Paula", "Kate", "Peter"); -for(String name: names) { +for (String name : names) { System.out.println(name); } ``` -Each step through the iteration, the `name` variable is bound to a new value, as the iteration advances from one element to the next in the given collection. Converting the imperative style `foreach` to the functional style is a straight up use of the `forEach` internal iterator method. It is called an internal iterator because the advancing to the next element is handled internally and automatically instead of externally or explicitly. +Each step through the iteration, the `name` variable is bound to a new value, as the iteration advances from one element to the next in the given collection. Converting the imperative style for-each to the functional style is a straight up use of the `forEach` internal iterator method. It is called an internal iterator because the advancing to the next element is handled internally and automatically instead of externally or explicitly. Let's refactor the loop to use functional style. @@ -67,8 +67,8 @@ Suppose, in the middle of the iteration, we want to pick some values from the co ```java List names = List.of("Jack", "Paula", "Kate", "Peter"); -for(String name: names) { - if(name.length() == 4) { +for (String name : names) { + if (name.length() == 4) { System.out.println(name); } } @@ -88,10 +88,10 @@ names.stream() The `filter()` method acts like a gate, it opens to let some elements pass through and closes to reject or discard some elements, as the iteration moves forward. -We saw in the previous articles the functional style equivalent for the traditional `for` loops. In this article we saw how the imperative style `foreach` of Java 5 transforms into an elegant syntax in the functional style. Furthermore, the `if` condition within a loop of the imperative style translates to a call to the `filter()` method of the `Stream` API. +We saw in the previous articles the functional style equivalent for the traditional `for` loops. In this article we saw how the imperative style for-each of Java 5 transforms into an elegant syntax in the functional style. Furthermore, the `if` condition within a loop of the imperative style translates to a call to the `filter()` method of the `Stream` API.   ## Mappings -Anywhere you see a `foreach` loop, use the `forEach()` method directly on the collection. If the body of the `foreach` has a `if` statement to selectively pick some value, then use the `stream()` API with the call to the `filter()` method. +Anywhere you see a for-each loop, use the `forEach()` method directly on the collection. If the body of the for-each has a `if` statement to selectively pick some value, then use the `stream()` API with the call to the `filter()` method. diff --git a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/04_converting_foreach_with_transformation.md b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/04_converting_foreach_with_transformation.md index 844cc4b..34ace28 100644 --- a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/04_converting_foreach_with_transformation.md +++ b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/04_converting_foreach_with_transformation.md @@ -28,17 +28,17 @@ Anytime we are transforming data in an imperative style loop, we can use the `ma   ## From Imperative to Functional Style -Here's an example of an iteration, using the `foreach`, that transforms to uppercase a collection of names: +Here's an example of an iteration, using the for-each, that transforms to uppercase a collection of names: ```java List names = List.of("Jack", "Paula", "Kate", "Peter"); -for(String name: names) { +for (String name : names) { System.out.println(name.toUpperCase()); } ``` -Each step through the iteration, the `name` variable is bound to a new value. As the iteration advances from one element to the next in the given collection, each name is transformed to uppercase using the `toUpperCase()` function and the resulting value is printed. We have already seen, in the previous article, how to convert the imperative style `foreach` to the functional style—using the `stream()` internal iteration. If we merely apply what we have seen before, the resulting functional style code would be rather unwieldy, with the lambda passed to `forEach` performing both transformation and printing, like so: +Each step through the iteration, the `name` variable is bound to a new value. As the iteration advances from one element to the next in the given collection, each name is transformed to uppercase using the `toUpperCase()` function and the resulting value is printed. We have already seen, in the previous article, how to convert the imperative style for-each to the functional style—using the `stream()` internal iteration. If we merely apply what we have seen before, the resulting functional style code would be rather unwieldy, with the lambda passed to `forEach` performing both transformation and printing, like so: ```java List names = List.of("Jack", "Paula", "Kate", "Peter"); @@ -53,7 +53,7 @@ Before refactoring the imperative style code above to the function style, we sho ```java List names = List.of("Jack", "Paula", "Kate", "Peter"); -for(String name: names) { +for (String name : names) { String nameInUpperCase = name.toUpperCase(); System.out.println(nameInUpperCase); } @@ -90,8 +90,8 @@ Suppose, in the middle of the iteration, we want to pick some values from the co ```java List names = List.of("Jack", "Paula", "Kate", "Peter"); -for(String name: names) { - if(name.length() == 4) { +for (String name : names) { + if (name.length() == 4) { System.out.println(name.toUpperCase()); } } diff --git a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/04_classes_objects/01_enums.md b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/04_classes_objects/01_enums.md index 651a2ea..f17a973 100644 --- a/app/pages/learn/01_tutorial/03_getting-to-know-the-language/04_classes_objects/01_enums.md +++ b/app/pages/learn/01_tutorial/03_getting-to-know-the-language/04_classes_objects/01_enums.md @@ -138,11 +138,11 @@ i.e. in the order of occurrence of the enum constant. This allows for comparing instances of enums as well as sorting or searching. ```java -public void compareDayOfWeek(DayOfWeek dayOfWeek){ +public void compareDayOfWeek(DayOfWeek dayOfWeek) { int comparison = dayOfWeek.compareTo(DayOfWeek.WEDNESDAY); - if ( comparison < 0) { + if (comparison < 0) { System.out.println("It's before the middle of the work week."); - } else if(comparison > 0){ + } else if (comparison > 0) { System.out.println("It's after the middle of the work week."); } else { System.out.println("It's the middle of the work week.");