You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/imperative.rst
+19-19Lines changed: 19 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -104,7 +104,7 @@ In this section, we discuss the different options for running Scala code, includ
104
104
`The list performance example <https://github.com/lucproglangcourse/cs2-listperformance-scala/blob/main/src/test/scala/cs271/lab/list/TestList.scala>`_ illustrates this approach.
105
105
106
106
107
-
- Finally, to turn an sbt-based Scala application into a script (console application) you can run outside sbt, you can use the `sbt-native-packager <https://github.com/sbt/sbt-native-packager>`_ plugin.
107
+
- Finally, to turn an sbt-based Scala application into a script (command-line application) you can run outside sbt, you can use the `sbt-native-packager <https://github.com/sbt/sbt-native-packager>`_ plugin.
108
108
To use this plugin, add this line to the end of ``build.sbt``:
109
109
110
110
.. code-block:: bash
@@ -226,13 +226,13 @@ This approach appears to work within IDEs such as IntelliJ but not in a standalo
226
226
assertEquals("hello hello", lines(1))
227
227
228
228
229
-
The role of console applications
230
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
229
+
The role of command-line applications
230
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231
231
232
-
Console applications have always been an important part of the UNIX command-line environment.
232
+
Command-line applications have always been an important part of the UNIX environment.
233
233
Each application typically focuses on a specific task, and several applications can be composed to solve a more complex task.
234
234
235
-
The typical console application interacts with its environment in the following ways:
235
+
The typical command-line application interacts with its environment in the following ways:
236
236
237
237
- *environment variables* defined in your system
238
238
- zero or more application-specific *command-line arguments* for passing options to the application: ``app arg1 arg2 ...``
@@ -288,13 +288,13 @@ Similarly, we can redirect input from a file using this notation:
288
288
289
289
$ wc -l < testdata.txt
290
290
291
-
There is a close relationship between UNIX pipes and functional programming: When viewing a console application as a function that transforms its input to its output, UNIX pipes correspond to function composition. The pipeline ``p | q`` corresponds to the function composition ``q o p``.
291
+
There is a close relationship between UNIX pipes and functional programming: When viewing a command-line application as a function that transforms its input to its output, UNIX pipes correspond to function composition. The pipeline ``p | q`` corresponds to the function composition ``q o p``.
292
292
293
293
294
-
Console applications in Scala
295
-
`````````````````````````````
294
+
Command-line applications in Scala
295
+
``````````````````````````````````
296
296
297
-
The following techniques are useful for creating console applications in Scala.
297
+
The following techniques are useful for creating command-line applications in Scala.
298
298
As in Java, command-line arguments are available to a Scala application as ``args`` of type ``Array[String]``.
299
299
300
300
We can read the standard input as lines using this iterator:
@@ -331,7 +331,7 @@ In Scala, ``print`` and ``println`` print to stdout, which is is an instance of
331
331
This class converts any ``IOException`` to a boolean flag accessible through its ``checkError()`` method.
332
332
(See also `this discussion <https://stackoverflow.com/questions/62658078/jvm-not-killed-on-sigpipe>`_ for more details.)
333
333
334
-
Therefore, to use a Scala (or Java) console application in a UNIX pipeline as an upstream component that produces an unbounded (potentially infinite) output sequence, we have to monitor this flag when printing to stdout and, if necessary, terminate execution.
334
+
Therefore, to use a Scala (or Java) command-line application in a UNIX pipeline as an upstream component that produces an unbounded (potentially infinite) output sequence, we have to monitor this flag when printing to stdout and, if necessary, terminate execution.
335
335
336
336
For example, this program reads one line at a time and prints the line count along with the line read.
337
337
After printing, it checks whether an error occured and, if necessary, terminates execution by exiting the program:
@@ -348,7 +348,7 @@ After printing, it checks whether an error occured and, if necessary, terminates
348
348
Command-line argument parsing
349
349
`````````````````````````````
350
350
351
-
A common concern when developing console applications is command-line argument and option parsing.
351
+
A common concern when developing command-line applications is argument and option parsing.
352
352
As briefly mentioned above, arguments and options are application-specific settings we can pass an application in the form ``app arg1 arg2 ...`` at the time when we're invoking the application.
353
353
Importantly, these settings are separate from the application's input data.
354
354
@@ -431,7 +431,7 @@ For example, we could run the application with these arguments:
431
431
Determining whether an app is running interactively
In some cases, it's convenient to determine programmatically whether our console app is running interactively, i.e., reading from the console, or running in batch mode, where its standard input has been redirected from a file.
434
+
In some cases, it's convenient to determine programmatically whether our command-line app is running interactively, i.e., reading from the console, or running in batch mode, where its standard input has been redirected from a file.
435
435
For example, if our app running interactively, we might automatically want to prompt the user for console input.
436
436
437
437
We can use this technique to determine whether stdin is coming from the console.
@@ -450,8 +450,8 @@ Allowing the user to edit their input
450
450
`````````````````````````````````````
451
451
Many REPLs, including the Python and Scala ones, allow the user to edit their input in various ways, including scrolling through the input history using the up and down arrows, adding or deleting characters from the input line, etc.
452
452
453
-
To add this capability to a Java- or Scala-based console app, we can use the `JLine library <https://github.com/jline/jline3>`_, which is the Java equivalent of the `GNU Readline library <https://en.wikipedia.org/wiki/GNU_Readline>`_.
454
-
If you want to make your console app convenient to use and give it a professional touch, consider using JLine instead of basic console input.
453
+
To add this capability to a Java- or Scala-based command-line app, we can use the `JLine library <https://github.com/jline/jline3>`_, which is the Java equivalent of the `GNU Readline library <https://en.wikipedia.org/wiki/GNU_Readline>`_.
454
+
If you want to make your command-line app convenient to use and give it a professional touch, consider using JLine instead of basic console input.
455
455
JLine has excellent documentation; please look there for examples.
@@ -544,7 +544,7 @@ The importance of constant-space complexity
544
544
545
545
Common application scenarios involve processing large volumes of input data or indefinite input streams, e.g., sensor data from an internet-of-things device.
546
546
To achieve the nonfunctional requirements of reliability/availability and scalability for such applications, it is critical to ensure that the application does not exceed a constant memory footprint during its execution.
547
-
*These considerations apply to any potentially long-running application, be it a console app, mobile app, or back-end service.*
547
+
*These considerations apply to any potentially long-running application, be it a command-line app, mobile app, or back-end service.*
548
548
549
549
550
550
Using iterators to represent indefinite input
@@ -595,11 +595,11 @@ When working in a command-line environment, we can also use an interactive proce
595
595
.. todo:: add suitable htop screenshot
596
596
597
597
598
-
Making console applications testable
599
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
598
+
Making command-line applications testable
599
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
600
600
601
-
Recognizing the importance of *testability* as a static nonfunctional requirement, we'd like to make our console applications testable.
602
-
While we could use command-line tools to set up automatic testing of the end-to-end functionality of our applications, we would also like to unit-test the logical functionality of our applications in isolation from input/output code.
601
+
Recognizing the importance of *testability* as a static nonfunctional requirement, we'd like to make our command-line applications testable.
602
+
While we could use other command-line tools to set up automatic testing of the end-to-end functionality of our applications, we would also like to unit-test the logical functionality of our applications in isolation from input/output code.
603
603
604
604
A key barrier to achieving this goal is the tangling (interweaving) of logical functionality and input/output in our code, i.e., there is a lack of separation of these two concerns.
605
605
In particular, how can we make our code testable *without* sacrificing the important dynamic nonfunctional requirement of a constant-space memory footprint?
0 commit comments