Lab: The One Style in JavaScript

The One style in JavaScript - Introduction

In this lab you will implement a solution of the The One style in JavaScript for the term frequency task.

(The unchanged task description is available in the Prelude of the first lab.)

These are the constraints for the The One style as specified in the book:

  • Existence of an abstraction to which values can be converted.
  • This abstraction provides operations to (1) wrap around values, so that they become the abstraction; (2) bind itself to functions, to establish sequences of functions; and (3) unwrap the value, to examine the final result.
  • Larger problem is solved as a pipeline of functions bound together, with unwrapping happening at the end.
  • Particularly for The One style, the bind operation simply calls the given function, giving it the value that it holds, and holds on to the returned value.

The goal of this lab is to write code that follows (a clean version of) this style.

Create and Clone Your GitHub Repository

To create the repository for this lab, fork this starter repository.

Your repository now exists on the GitHub servers. If you want to work on it, you first have to “clone” it on your computer.

Implement Your Program

You can find Crista’s Python implementation of the Term Frequency task in The One style in her GitHub repository:

https://github.com/crista/exercises-in-programming-style/blob/master/10-the-one/tf-10.py


⚠️ WARNING ⚠️

In your solution, follow these recommendations to obtain a cleaner and simpler implementation of this style.

  • Ignore point (3) of the second constraint. That is: do not add a printme method to the abstraction. To examine the final result, simply pass an appropriate function to bind. Such a function, as usual, receives the value to output in a parameter. It is OK for this function to be impure​. At the end of the chain in the “main” program, replace .printme() with .bind(my_printing_function).
  • Implement the “abstraction” (e.g., the class TFTheOne) in an immutable​ fashion. Notice how, inside bind, Crista’s implementation mutates the instance variable​ _value. Instead of doing that, your solution should always create new immutable instances.
  • The creation of such immutable instances should be responsibility of the individual functions, and not happen inside bind. That is, bind can be much simpler:
    def bind(self, func):
        return func(self._value)
    

    Each function then takes a normal value and returns “the abstraction”. For example:

    def normalize(str_data):
        return TFTheOne(str_data.lower())
    

If you want, you can use Crista’s implementation as a starting point for your JavaScript code (but be mindful of the aspects described in the warning). You can also make use of the functions’ implementations you wrote for the JavaScript Pipeline lab (or the code snippets provided in the description of that lab).

Once you have the repository (a directory) on your computer, you can open it in your IDE (e.g., in VS Code). You can easily do this using the code command, passing as an argument the path to the appropriate directory:

code ~/lab-05-theone-javascript

Before you start “hacking”, please read the README (file README.md).

Then create a new JavaScript file named TermFrequency.js.

Implement a solution to the term frequency task in JavaScript following the The One style.

Run, Test, and Debug the Program

Read the README.md and the first part of this lab to figure out how to run the program.

Test

We strongly recommend to use the test script, because it shows you whether your solution is functionally correct. For this, it compares your output with the reference output we provided. Obviously, this only works if you strictly follow the rules (e.g., don’t print out debug output), otherwise the script will tell you that you don’t produce the correct output.