nim_iterator_stream_experiment/monad/optional

The Optional[T] monad.

It is a box containing either a value or nothing.

This is another implementation of the Option/Maybe monad, but using only pure functional programming techniques to make it compatible with compile time execution.

Examples can be found here.

Types

Nilable = concept var x
    x = nil
  Source Edit
UnboxError = object of CatchableError
  Source Edit
Optional[T] = object
  when T is Nilable:
    value
  else:
    case empty
    of true:
      
    else:
        value

  
  Source Edit

Procs

proc toSome[T: Nilable](value: T): Optional[T]
  Source Edit
proc toOptional[T: Nilable](value: T): Optional[T]
If value is not nil, returns value.toSome(), otherwise an empty Optional.   Source Edit
proc ifNone[A; B](self: Optional[A]; then: () -> B; `else`: A -> B): B
  Source Edit
proc ifSome[A; B](self: Optional[A]; then: A -> B; `else`: () -> B): B
  Source Edit
proc flatMap[A; B](self: Optional[A]; f: A -> Optional[B]): Optional[B]
Applies f to the value inside self or does nothing if self is empty.   Source Edit
proc map[A; B](self: Optional[A]; f: A -> B): Optional[B]
Applies f to the value inside self or does nothing if self is empty.   Source Edit
proc unboxOr[T](self: Optional[T]; `else`: () -> T): T
  Source Edit
proc filter[T](self: Optional[T]; predicate: Predicate[T]): Optional[T]
  Source Edit

Funcs

func toNone(T: typedesc[Nilable]): Optional[T]
  Source Edit
func toNone(T: typedesc[not Nilable]): Optional[T]
  Source Edit
func toNone[T](): Optional[T]
  Source Edit
func toSome[T: not Nilable](value: T): Optional[T]
  Source Edit
func isNone[T: Nilable](self: Optional[T]): bool
  Source Edit
func isNone[T: not Nilable](self: Optional[T]): bool
  Source Edit
func isSome[T](self: Optional[T]): bool
  Source Edit
func unbox[T](self: Optional[T]): T {...}{.raises: [Exception, UnboxError].}
Retrieves the value inside self or raise an UnboxError if self is empty.   Source Edit
func `==`[T](self, other: Optional[T]): bool
  Source Edit

Templates

template valueType[T; ](X: typedesc[Optional[T]]): typedesc[T]
  Source Edit
template valueType[T; ](self: Optional[T]): typedesc[T]
  Source Edit