concept argument in category functional programming

appears as: arguments, n argument, rguments, argument, Arguments
Grokking Functional Programming

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

  List<String> items = new ArrayList<>(); items.add(“Apple”); ShoppingCart.getDiscountPercentage(items); → 0   items.add(“Book”); ShoppingCart.getDiscountPercentage(items); → 5  

As you see, we don’t have any state, we just pass a list of items to our discount calculation function. We still have all the powers we had before, but now we have less code!

static double f(double x) {
  return x * 95.0 / 100.0;
}
Functional Programming in C++

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

One of FP’s most powerful ideas is pure functions: functions that only use (but don’t modify) the arguments passed to them in order to calculate the result. If a pure function is called multiple times with the same arguments, it must return the same result every time and leave no trace that it was ever invoked (no side effects). This all implies that pure functions are unable to alter the state of the program.

Listing 4.1 Comparing an argument to a predefined value
class greater_than {
public:
    greater_than(int value)
        : m_value
    {
    }

    bool operator()(int arg) const
    {
        return arg > m_value;
    }

private:
    int m_value;
};

…

greater_than greater_than_42(42);    #1  
    #1  
greater_than_42(1);  // false    #1  
greater_than_42(50); // true    #1  

std::partition(xs.begin(), xs.end(), greater_than(6));    #2  

#1   You can create an instance of this object and use it multiple times to check whether other specific values are greater than 42.
#2   And you can create an instance directly when calling a specific algorithm such as std::partition to separate items greater than 6 from those that are less.

In C++11, these two functions were deprecated (they were removed in C++17) in favor of a much more general one called std::bind. The std::bind function isn’t limited to only binary functions and can work with functions that have an arbitrary number of arguments. It also doesn’t limit the user regarding which arguments can be bound; you can bind any number of arguments, in any order, while leaving the rest unbound.

Let’s start with a basic example of using std::bind: binding all arguments of a function to specific values, but without invoking it. The first argument of std::bind is the function whose arguments you want to bind to specific values, and the other arguments are the values the function arguments will be bound to. You’ll bind the arguments of the std::greater comparator function to fixed values: 6 and 42. Technically, this isn’t partial function application, because you’re binding all the arguments of a function, but it’s a good start to introduce the syntax of std::bind.

Listing 4.7 Binding all arguments of a function with std::bind
auto bound =
    std::bind(std::greater<double>(), 6, 42);    #1  

bool is_6_greater_than_42 = bound();             #2  

#1   The std::greater function isn’t yet invoked; you’ve created a function object that will call std::greater with the specified values 6 and 42.
#2   Only when calling the bound function object are the elements 6 and 42 compared.

By providing values for all needed arguments when binding a function, you create a new function object that saves the function whose arguments you’re binding (the std::greater comparator, in this case) and all the passed values (see figure 4.4). Binding defines the values for the arguments, but it doesn’t invoke the function. The function is invoked only when someone calls the function object returned by std::bind. In the example, the std::greater comparator is called only when calling bound().

Figure 4.4 The two-argument function tests whether its first argument is greater than its second. Binding both arguments to specific values such as 6 and 42 creates a nullary function object that, when called, returns the result of greater(6, 42).

c04_04.png

Now, let’s see how you can leave one of the arguments unbound while binding the other one. You can’t just define one value and skip the other, because std::bind wouldn’t know which argument you want to bind—the first or the second. Therefore, std::bind introduces the concept of placeholders. If you want to bind an argument, you pass a value for it to std::bind as you did in the preceding example. But if you want to say that an argument should remain unbound, you have to pass a placeholder instead.

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.

Some HOFs take other functions as arguments and invoke them in order to do their work, somewhat like a company may subcontract some of its work to another company. You’ve seen some examples of such HOFs earlier in this chapter: Sort (an instance method on List) and Where (an extension method on IEnumerable).

We’ll then look at how partial application can be used in practice to first specify configuration arguments that are available at startup, and purely runtime arguments later, as they’re received.

If you want to use the generic Apply to supply arguments to a method, you have to use one of the following forms.

Listing 7.3. Using multi-argument methods as arguments to HOFs requires messy syntax
Listing 7.13. Dependencies can be passed as arguments to a function
public static Validator<BookTransfer> DateNotPast(Func<DateTime> clock) => cmd => cmd.Date.Date < clock().Date ? Errors.TransferDateIsPast : Valid(cmd);

7.1.3. Order of arguments matters

The greet function shows what is generally a good order of arguments: the more general parameters, which are likely to be applied early in the life of the application, should come first, followed by the more specific parameters. We learn to say “Hello” early in life, but we keep meeting and greeting new people until we’re old.

Functional Programming in JavaScript

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

If need be, these subtasks can be decomposed further until you arrive at simpler, pure functions, each of which is an independent unit of work. Remember that this was the thought process I followed when refactoring showStudent in listing 1.4. Modularization in FP is closely related to the singularity principle, which states that functions should have a single purpose; this was also evident in the code for average, shown earlier. Purity and referential transparency encourage you to think this way because in order to glue simple functions together, they must agree on the types of inputs and outputs. From referential transparency, you learn that a function’s complexity is sometimes directly related to the number of arguments it receives (this is merely a practical observation and not a formal concept indicating that the lower the number of function parameters, the simpler the function tends to be).

function applyOperation(a, b, opt) {                   #1
   return opt(a,b);   
}
var multiplier = (a, b) => a * b;
applyOperation(2, 3, multiplier); // -> 6

Types are certainly important but, in JavaScript, not as critical as being compatible with the number of arguments a function accepts.

Passing a function’s return value as input to a unary function is straightforward, but what if the target function expects more parameters? In order to understand currying in JavaScript, first you must understand the difference between a curried and a regular (non-curried) evaluation. In JavaScript, a regular or non-curried function call is permitted to execute with missing arguments. In other words, if you define a function f(a,b,c) and call it with just a, the evaluation proceeds, and the JavaScript runtime sets b and c to undefined, as shown in figure 4.5. This is unfortunate and most likely the reason why currying isn’t a built-in feature of the language. As you can imagine, not declaring any arguments and relying on the arguments object within functions only exacerbates this issue.

Figure 4.5. Calling a non-curried function with missing arguments causes the function to eagerly evaluate missing parameters and fill them with undefined.

On the other hand, a curried function is one where all arguments have been explicitly defined so that, when called with a subset of the arguments, it returns a new function that waits for the rest of the parameters to be supplied before running. Figure 4.6 represents this visually.

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.

When I say “no side effects,” I mean no observable side effects. Functional programs are built by composing functions that take an argument and return a value, and that’s it. You don’t care about what’s happening inside the functions, because, in theory, nothing is happening ever. But in practice, programs are written for computers that aren’t functional at all. All computers are based on the same imperative paradigm; so functions are black boxes that

  • Take an argument (a single one, as you’ll see later)
  • Note that the two notations f ˚ g (x) and f(g(x)) are equivalent. But writing a composition as f(g(x)) implies using x as a placeholder for the argument. Using the f ˚ g notation, you can express a function composition without using this placeholder.

    which means that the result of applying the function f to the argument x is a new function g. Applying this g function to y gives the following:

    g(y) = x + y

    When applying g, x is no longer a variable. It doesn’t depend on the argument or on anything else. It’s a constant. If you apply this to (3, 5), you get the following:

    Write a method to swap the arguments of a curried function.