concept none in category functional programming

appears as: None, None, Nones, none
Grokking Functional Programming

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

def validateLength(start: Int,
                   end: Int,
                   minLength: Int): Option[Int] = ???
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.

Listing 3.6. Idealized relation of Option to its cases None and Some
namespace LaYumba.Functional { interface Option<out T> { } namespace Option { public struct None : Option<T> { /* ... */ } public struct Some<T> : Option<T> { /* ... */ } } }

That is, I’d like to say that None is an Option<T>, and so is Some<T>. Unfortunately, there are several problems with the preceding code (which, as a consequence, doesn’t compile):

  • None doesn’t have (and doesn’t need) a type parameter T; it can’t therefore implement the generic interface Option<T>. It would be nice if None could be treated as an Option<T> regardless of what type the type parameter T is eventually assigned, but this isn’t supported by C#’s type system.
  • An Option<T> can only be one of two things: None or Some<T>. It shouldn’t be possible for any client assembly to define any other implementations of Option<T>, but there’s no language feature to enforce this.
  • Given these issues, using an interface or abstract class for Option doesn’t work very well. Instead, I defined Option<T> as a separate class and defined methods so that both None and Some<T> can be implicitly converted into Option<T> (inheritance by implicit conversion, if you like).

    Listing 3.7. Option<T> can capture both the Some and None states

    This implementation of Option can represent both None and Some; it has a Boolean value to discriminate between these two states, as well as a field of type T to store the inner value of a Some.

    You can now treat None as an Option<T> for any type T. When None is converted to an Option<T>, the isSome flag will be false; the inner value will be the default value for T and will be disregarded. When Some<T> is converted into an Option<T>, the isSome flag is true and the inner value is stored.

    I also added a method to implicitly lift a value of type T into an Option<T>, which will prove convenient in some scenarios. It yields an Option in the None state if the value is null, and it wraps the value into a Some otherwise.

    Partial functions are problematic because it’s not clear what the function should do when given an input for which it can’t compute a result? The Option type offers a perfect solution to model such cases: if the function is defined for the given input, it returns a Some wrapping the result; otherwise, it returns None.

    Lookup takes a NameValueCollection and a string (the key), and will return Some with the value if the key exists, and None otherwise. Here’s the implementation:

    public static Option<string> Lookup
       (this NameValueCollection @this, string key)
       => @this[key];

    That’s it! The expression @this[key] is of type string, whereas the return value is Option<string>, so the string value will be implicitly converted into an Option<string>. (Remember, in the implementation of Option shown earlier, implicit conversion from a value T to an Option<T> was defined to return None if the value was null, and to lift the value into a Some otherwise.) We’ve gone from a null-based API to an Option-based API.

    If you need to create an Age from an int, instead of calling the constructor (which has to throw an exception if it’s unable to create a valid instance), you can define a function that returns Some or None to indicate the successful creation of an Age. This is known as a smart constructor: it’s “smart” in the sense that it’s aware of some rules and can prevent the construction of an invalid object.

    Listing 3.8. Implementing a smart constructor for Age

    If you now need to obtain an Age from an int, you’ll get an Option<Age> instead, which forces you to account for the failure case. If your Option<Age> is None, what do you do with it? Well, that depends on the context and requirements. In upcoming chapters we’ll look at how you can work effectively with Options. Although Match is the basic way of interacting with an Option, we’ll build a rich, high-level API starting in the next chapter.

    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.

    None of these versions (recursive and corecursive) are equivalent to the for loop. This is because, although Java is mostly a strict language (it’s strict regarding method arguments), the for loop, like all Java control structures and some operators, is lazy. This means that in the for loop you used as an example, the order of evaluation will be index, computation, index, computation ..., although using the range method will first compute the complete list before mapping the function.

    The None implementation is simple. You just have to return a None instance. As I said earlier, the Option class contains a None singleton that can be used for this:

    public <B> Option<B> map(Function<A, B> f) {
      return none();
    }

    Note that although this and none refer to the same object, you can’t return this because it’s parameterized with A. The none reference points to the same object, but with a raw type (no parameter). This is why you annotate none with @SuppressWarnings ("rawtypes") in order to keep compiler warnings from leaking to the caller. In the same manner, you use a call to the none() factory method instead of directly accessing the none instance in order to avoid the “Unchecked assignment warning” that you already avoided in the none() method by using the @SuppressWarnings ("unchecked") annotation.

    There’s another reason why Option (or Optional) should probably be used rarely (if ever). Generally, the absence of data is the result of errors that you should often handle by throwing an exception in imperative Java. As I said previously, returning Option.None instead of throwing an exception is like catching an exception and swallowing it silently. Usually it’s not a billion-dollar mistake, but it’s still a big one. You’ll learn in the next chapter how to deal with this situation. After that, you’ll hardly ever need the Option data type again. But don’t worry. All you’ve learned in this chapter will still be extremely useful.

    6.4.1. Testing for Some or None

    Until now, you haven’t needed to test an option to know whether it was a Some or a None. Ideally, you should never have to do this. In practice, though, there are times when it’s simpler to use this trick than to resort to real functional techniques.