neohaskell / NeoHaskell

⏩ NeoHaskell is a dialect of Haskell that is focused on newcomer-friendliness and productivity.
https://neohaskell.org
Apache License 2.0
276 stars 3 forks source link

Implement Option type #25

Closed NickSeagull closed 3 months ago

NickSeagull commented 1 year ago

Reasoning

The Option type is a staple in functional programming languages and has found its way into more mainstream languages as well, such as Swift's Optional and Kotlin's Optional. It is used to represent a value that can either be present (Some) or absent (None). The Option type is commonly used for:

  1. Null Safety: One of the primary uses is to safely encapsulate the absence of a value, as opposed to using null or nil, which can lead to runtime errors.

  2. Function Return Types: It's often used as the return type of functions that may or may not produce a value. For example, a function that searches for an element in a collection can return an Option to signify whether the search was successful.

  3. Configuration: It's useful for optional configuration settings that have sensible defaults.

  4. Pipeline Processing: Higher-order functions like map, filter, and flatMap can be defined on an Option, making it composable in data pipelines.

  5. Error Handling: While Option is not a substitute for proper error types like Either or Result, it can be used for simple error handling scenarios where the only important information is the presence or absence of a value.

Incorporating these considerations, let's define the API for the Option type.

Functions for the Option Module

Constructors

Basic Queries

Transformations

Filtering

Side Effects

Error Handling

Conversions

By adopting a rich and expressive set of operations, the Option type can serve as a powerful tool for safe and expressive programming in NeoHaskell. As always, the API can be expanded or refined based on user feedback and real-world use cases.

NickSeagull commented 3 months ago

Fixed in v0.1.0 - we implemented it as a Maybe reexport