concept output in category functional programming

appears as: output, outputs, output, The output, n output
Grokking Functional Programming

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

Create a space to learn

Return a new List with all numbers larger than 4 (or 1).

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.

  • FP leverages higher-order functions (functions that take other functions as input or output); hence the necessity for the language to have functions as first-class values.
  • Since you’re explicitly passing the value 100 as a seed for the random generator, you should get exactly the same results. As you can see, it’s not that random after all. It’s next to impossible to get real randomness with our current computers; instead, random generators use a scrambling algorithm to deterministically produce an output that looks random. Typically, you don’t want to get the same sequence of values every time, so a Random instance is usually initialized without an explicit seed, in which case the current time is used.

    So, if Random is deterministic, how does it produce a different output every time you call Next? The answer is that Random is stateful: every time you call Next, the state of the instance changes, so that the following call to Next will yield a new value. In other words, Next has side effects: although Next produces an int as its explicit output, there’s an implicit input (the current state of the Random instance) that determines the output, and another implicit output, the new state of the Random, that will determine the output of the following call to Next.

    We’re going to create a side-effect-free random generator, where all inputs and outputs are explicit. Generating a number value is a stateful computation, because it requires a seed, and it must also generate a new seed to be used in the next generation. We don’t want to only generate integers, but values of any type, so the type of a generator function can be captured with the following delegate:

  • When writing stateful programs, you can avoid changing state as a side effect by always passing the state explicitly as part of a function’s input and output.
  • public static void Main()
    {
       var inputs = new Subject<string>();
    
       var rates =
          from pair in inputs
          from rate in Yahoo.GetRate(pair)
          select rate;
    
       var outputs = from r in rates select r.ToString();
    
       using (inputs.Trace("inputs"))
       using (rates.Trace("rates"))
       using (outputs.Trace("outputs"))
          for (string input; (input = ReadLine().ToUpper()) != "Q";)
             inputs.OnNext(input);
    }