Skip to content

Commit 4555fa8

Browse files
authored
Merge pull request #45 from jetbrains-academy/stephen-hero-patch-38
Update task.md
2 parents f3ec3ae + b1a2a52 commit 4555fa8

File tree

21 files changed

+82
-89
lines changed

21 files changed

+82
-89
lines changed

Early Returns/Baby Steps/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ If no valid user data has been found, we return `None` after traversing the enti
6363

6464
```scala 3
6565
/**
66-
* Imperative approach that uses un-idiomatic `return`.
66+
* Imperative approach that uses unidiomatic `return`.
6767
*/
6868
def findFirstValidUser1(userIds: Seq[UserId]): Option[UserData] =
6969
for userId <- userIds do
@@ -121,7 +121,7 @@ Consult the `breedCharacteristics` map for the appropriate fur characteristics f
121121

122122
Finally, implement the search using the conversion and validation methods:
123123
* `imperativeFindFirstValidCat`, which works in an imperative fashion.
124-
* `functionalFindFirstValidCat`, utilizing an functional style.
124+
* `functionalFindFirstValidCat`, utilizing a functional style.
125125
* `collectFirstFindFirstValidCat`, using the `collectFirst` method.
126126

127127
Ensure that your search does not traverse the entire database.

Early Returns/Breaking Boundaries/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ One important thing is that it ensures that the users never call `break` without
99
the code much safer.
1010

1111
The following snippet showcases the use of boundary/break in its simplest form.
12-
If our conversion and validation work out then `break(Some(userData))` jumps out of the loop labeled with `boundary:`.
12+
If our conversion and validation work out, then `break(Some(userData))` jumps out of the loop labeled with `boundary:`.
1313
Since it's the end of the method, it immediately returns `Some(userData)`.
1414

1515
```scala 3

Early Returns/Unapply/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ the `for` loop.
5555

5656
Unlike the imperative approach, the functional implementation separates the logic of conversion and validation
5757
from the sequence traversal, which results in more readable code.
58-
Taking care of possible missing records in the database amounts to modifying the unapply method, while the
58+
Taking care of possible missing records in the database amounts to modifying the `unapply` method, while the
5959
search function stays the same.
6060

6161
```scala 3
@@ -77,7 +77,7 @@ Imagine that there is a user who doesn't have an email.
7777
In this case, `complexValidation` returns `false`, but the user might still be valid.
7878
For example, it may be an account that belongs to a child of another user.
7979
We don't need to message the child; instead, it's enough to reach out to their parent.
80-
Even though this case is less common than the one we started with, we still need to keep it mind.
80+
Even though this case is less common than the one we started with, we still need to keep it in mind.
8181
To account for it, we can create a different extractor object with its own `unapply` and pattern match against it
8282
if the first validation fails.
8383
We do run the conversion twice in this case, but its impact is less significant due to the rarity of this scenario.

Expressions over Statements/Using Clause for Carrying an Immutable Context/task.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
## `Using` Clause for Carrying Immutable Context
2-
31
When writing pure functions, we often end up carrying some immutable context, such as configurations, as extra arguments.
42
A common example is when a function expects a specific comparator of objects, such as in computing the maximum value or sorting:
53

Functions as Data/filter/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ val blackCats = cats.filter { cat => cat.color == Black }
3737

3838
In the exercises, we will be working with a more detailed representation of cats than in the lessons.
3939
Check out the `Cat` class in `src/Cat.scala`.
40-
A cat has multiple characteristics: its name, breed, color, pattern, and a set of additional fur characteristics, such as
40+
A cat has multiple characteristics: its name, breed, color pattern, and a set of additional fur characteristics, such as
4141
`Fluffy` or `SleekHaired`.
4242
Familiarize yourself with the corresponding definitions in other files in `src/`.
4343

@@ -48,4 +48,4 @@ There are multiple cats available, and you wish to adopt a cat with one of the f
4848
* The cat is fluffy.
4949
* The cat is of the Abyssinian breed.
5050

51-
To simplify decision making, you first identify all the cats which possess at least one of the characteristics above. Your task is to implement the necessary functions and then apply the filter.
51+
To simplify decision making, you first identify all the cats that possess at least one of the characteristics above. Your task is to implement the necessary functions and then apply the filter.

Functions as Data/flatMap/task.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# `flatMap`
21
`def flatMap[B](f: A => IterableOnce[B]): Iterable[B]`
32

43
You can consider `flatMap` as a generalized version of the `map` method. The function `f`, used by `flatMap`, takes every element of the original collection and, instead of returning just one new element of a different (or the same) type, produces a whole new collection of new elements. These collections are then "flattened" by the `flatMap` method, resulting in just one large collection being returned.
@@ -7,7 +6,7 @@ Essentially, the same effect can be achieved with `map` followed by `flatten`. `
76

87
However, the applications of `flatMap` extend far beyond this simplistic use case. It's probably the most crucial method in functional programming in Scala. We will talk more about this in later chapters about monads and chains of execution. For now, let's focus on a straightforward example to demonstrate how exactly `flatMap` works.
98

10-
Just as in the `map` example, we will use a list of four cats. But this time, for every cat, we will create a list of cars of different brands but the same color as the cat. Finally, we will use `flatMap to combine all four lists of cars of different brands and colors into one list.
9+
Just as in the `map` example, we will use a list of four cats. But this time, for every cat, we will create a list of cars of different brands but the same color as the cat. Finally, we will use `flatMap` to combine all four lists of cars of different brands and colors into one list.
1110

1211
```scala
1312
// We define the Color enum

Functions as Data/foldLeft/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ We call the `foldLeft` method on the numbers range, stating that the accumulator
4545
The second argument to `foldLeft` is a function that takes the current accumulator value (`acc`) and an element from the numbers range (`n`).
4646
This function calls our `fizzBuzz` method with the number and appends the result to the accumulator list using the `:+` operator.
4747

48-
Once all the elements have been processed, `foldLeft returns the final accumulator value, which is the complete list of numbers and strings "Fizz", "Buzz", and "FizzBuzz", replacing the numbers that were divisible by 3, 5, and 15, respectively.
48+
Once all the elements have been processed, `foldLeft` returns the final accumulator value, which is the complete list of numbers and strings "Fizz", "Buzz", and "FizzBuzz", replacing the numbers that were divisible by 3, 5, and 15, respectively.
4949

5050
Finally, we print out the results.
5151

Functions as Data/map/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The `map` method works on any Scala collection that implements `Iterable`.
44
It takes a function `f` and applies it to each element in the collection, similar to `foreach`. However, in the case of `map`, we are more interested in the results of `f` than its side effects.
55
As you can see from the declaration of `f`, it takes an element of the original collection of type `A` and returns a new element of type `B`.
6-
Finally, the map method returns a new collection of elements of type `B`.
6+
Finally, the `map` method returns a new collection of elements of type `B`.
77
In a special case, `B` can be the same as `A`. So, for example, we could use the `map` method to take a collection of cats of certain colors and create a new collection of cats of different colors.
88
But, we could also take a collection of cats and create a collection of cars with colors that match the colors of our cats.
99

Functions as Data/passing_functions_as_arguments/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Then, we create a class `Cat`, which includes a value for the color of the cat.
2727
Finally, we use the `filter` method and provide it with an anonymous function as an argument. This function takes an argument of the `Cat` class and returns `true` if the cat's color is black.
2828
The `filter` method will apply this function to each cat in the original set and create a new set containing only those cats for which the function returns `true`.
2929

30-
However, our function that checks if the cat is black doesn't have to be anonymous. The `filter method will work just as well with a named function.
30+
However, our function that checks if the cat is black doesn't have to be anonymous. The `filter` method will work just as well with a named function.
3131

3232
```scala
3333
def isCatBlack(cat: Cat): Boolean = cat.color == Color.Black

Immutability/A View/task.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
## A View
2-
31
A view in Scala collections is a lazy rendition of a standard collection.
42
While a lazy list needs intentional construction, you can create a view from any "eager" Scala collection simply by calling `.view` on it.
53
A view computes its transformations (like map, filter, etc.) in a lazy manner,
64
meaning these operations are not immediately executed; instead, they are computed on the fly each time a new element is requested.
7-
This can enhabce both performance and memory usage.
5+
This can enhance both performance and memory usage.
86
On top of that, with a view, you can chain multiple operations without the need for intermediary collections —
97
the operations are applied to the elements of the original "eager" collection only when requested.
108
This can be particularly beneficial in scenarios where operations like map and filter are chained, so a significant number of

0 commit comments

Comments
 (0)