concept number in category functional programming

appears as: number, number, numbers, numbers, The number
Grokking Functional Programming

This is an excerpt from Manning's book Grokking Functional Programming.

In this book, we’ll dive deeper into these topics, step by step, and eventually will be able to build real-world programs without even thinking about the old habits. This alone will be a game changer. However, the benefits don’t stop there. There are other useful side-effects you get when learning functional programming with this book.

The main reason programmers are focusing on software design is to make it maintainable. In order for a piece of code to be maintainable, it needs to be easily understood by programmers on our team so that they can change it quickly and with certainty. That’s why we focus on making the code readable. We apply the same general rules of good design in functional programming as in other programming paradigms. The difference is that we apply them on a function level. Therefore, we focus on making our functions implement one small business requirement each.

Functional Programming with Kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.

The example takes two parameters: a CreditCard, and the Int number of coffees to be purchased. After the Coffees have been successfully purchased, they are placed into a List data type. The list is initialized using the List(n) { buyCoffee(cc) } syntax, where n describes the number of coffees, and { buyCoffee(cc) } a function that is used to initialize each element of the list.

Generalize tail to the function drop, which removes the first n elements from a list. Note that this function takes time proportional only to the number of elements being dropped—we don’t need to make a copy of the entire List.

Functional Programming in C++

This is an excerpt from Manning's book Functional Programming in C++.

Consider a small example: calculating the sum of a list of numbers. In the imperative world, you implement this by iterating over the list and adding the numbers to the accumulator variable. You explain the step-by-step process of how to sum a list of numbers. On the other hand, in the functional style, you need to define only what a sum of a list of numbers is. The computer knows what to do when it’s required to calculate a sum. One way you can do this is to say that the sum of a list of numbers equals the first element of the list added to the sum of the rest of the list, and that the sum is zero if the list is empty. You define what the sum is without explaining how to calculate it.

This difference is the origin of the terms imperative and declarative programming. Imperative means you command the computer to do something by explicitly stating each step it needs to perform in order to calculate the result. Declarative means you state what should be done, and the programming language has the task of figuring out how to do it. You define what a sum of a list of numbers is, and the language uses that definition to calculate the sum of a given list of numbers.

When you implement something like this, you usually create a class to contain all the data you need. You end up with a class that contains the handler through which the web page is accessed (a socket or a data stream), the counter to contain the number of words, and flags that indicate whether the process has started and whether it has finished.

Figure 9.3 When counting the number of words on a web page, you have three main states. Before you have the web page, you can’t do anything. When you get the web page, you can start counting. While counting, you need to store the current count and the handler to the web page. When the counting is finished, you no longer need the handle to the web page; you can just store the final count.

c09_03.png
void count_words(const std::string& web_page)
{
    assert(m_state.index() == 0);

    m_state = running_t(web_page);

    … // count the number of words

    counting_finished();
}
Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

Arity is a funny word that refers to the number of arguments that a function accepts:

Listing 2.3. Combining elements from parallel lists with Zip
Enumerable.Zip( new[] {1, 2, 3}, new[] {"ichi", "ni", "san"}, (number, name) => $"In Japanese, {number} is: {name}") // => ["In Japanese, 1 is: ichi", // "In Japanese, 2 is: ni", // "In Japanese, 3 is: san"]

Notice that Retry is recursive: if the operation fails, it waits for the specified interval and then retries the same operation, decreasing the number of retries left, and doubling the interval to wait the next time around (a retry strategy known as exponential backoff).

Functional Programming in Java: How functional techniques improve your Java programs

This is an excerpt from Manning's book Functional Programming in Java: How functional techniques improve your Java programs.

Although it is true that most functional languages do use a number of these techniques, you may find, for each of them, examples of functional programming languages that don’t, as well as non-functional languages that do. As you’ll see when studying each of these techniques in this book, it’s not the language that makes programming functional. It’s the way you write the code. But some languages are more functional-friendly than others.

You also need multiplication, but multiplication is a bit different. Addition adds things of the same type, whereas multiplication multiplies things of one type by a number. So multiplication isn’t commutative when it isn’t applied just to numbers. Here’s an example of multiplication for Product:

Functional Programming in Scala

This is an excerpt from Manning's book Functional Programming in Scala.

Listing 2.1. A simple Scala program
// A comment! /* Another comment */ /** A documentation comment */ object MyModule { #1 def abs(n: Int): Int = #2 if (n < 0) -n #3 else n private def formatAbs(x: Int) = { #4 val msg = "The absolute value of %d is %d" #5 msg.format(x, abs(x)) #6 } def main(args: Array[String]): Unit = #7 println(formatAbs(-42)) } #1: Declares a singleton object, which simultaneously declares a class and its only instance. #2: abs takes an integer and returns an integer. #3: Returns the negation of n if it’s less than zero. #4: A private method can only be called by other members of MyModule. #5: A string with two placeholders for numbers marked as %d. #6: Replaces the two %d placeholders in the string with x and abs(x) respectively. #7: Unit serves the same purpose as void in languages like Java or C.

A parser is a specialized program that takes unstructured data (such as text, or any kind of stream of symbols, numbers, or tokens) as input, and outputs a structured representation of that data. For example, we can write a parser to turn a comma-separated file into a list of lists, where the elements of the outer list represent the records, and the elements of each inner list represent the comma-separated fields of each record. Another example is a parser that takes an XML or JSON document and turns it into a tree-like data structure.

First, let’s consider the parser that recognizes zero or more repetitions of the character 'a' and returns the number of characters it has seen. We can start by adding a primitive combinator for it; let’s call it many:

def many[A](p: Parser[A]): Parser[List[A]]

This isn’t exactly what we’re after—we need a Parser[Int] that counts the number of elements. We could change the many combinator to return a Parser[Int], but that feels too specific—undoubtedly there will be occasions where we care about more than just the list length. Better to introduce another combinator that should be familiar by now, map: