{"id":17197173,"url":"https://github.com/thomaslevesque/hamlet","last_synced_at":"2025-04-13T20:51:12.917Z","repository":{"id":139542853,"uuid":"169099328","full_name":"thomaslevesque/Hamlet","owner":"thomaslevesque","description":"“To be, or not to be, that is the question”. A simple Option type for .NET","archived":false,"fork":false,"pushed_at":"2022-02-10T08:10:54.000Z","size":62,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T11:13:24.555Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomaslevesque.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-02-04T15:25:00.000Z","updated_at":"2025-03-23T14:34:35.000Z","dependencies_parsed_at":"2023-06-08T23:15:40.241Z","dependency_job_id":null,"html_url":"https://github.com/thomaslevesque/Hamlet","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FHamlet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FHamlet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FHamlet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FHamlet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomaslevesque","download_url":"https://codeload.github.com/thomaslevesque/Hamlet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248782278,"owners_count":21160716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T01:55:39.316Z","updated_at":"2025-04-13T20:51:12.882Z","avatar_url":"https://github.com/thomaslevesque.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hamlet\n\n![Logo](assets/hamlet.png)\n\n[![NuGet version](https://img.shields.io/nuget/v/Hamlet.svg?logo=nuget)](https://www.nuget.org/packages/Hamlet)\n[![AppVeyor build](https://img.shields.io/appveyor/ci/thomaslevesque/hamlet.svg?logo=appveyor\u0026logoColor=cccccc)](https://ci.appveyor.com/project/thomaslevesque/hamlet)\n[![AppVeyor tests](https://img.shields.io/appveyor/tests/thomaslevesque/hamlet.svg?logo=appveyor\u0026logoColor=cccccc)](https://ci.appveyor.com/project/thomaslevesque/hamlet/build/tests)\n\n\u003e “To be, or not to be, that is the question”\n\nHamlet is a small .NET library that provides a simple option type, common operations for working with\noptions (map, bind, filter...), as well as some extension methods for using Linq syntax on options.\n\nAn option type is useful to represent the presence or absence of a value. In C#, `null` is often used\nfor that purpose, but it has some issues:\n- it's a frequent source of bugs, because developers often forget to check for null\n- `null` might be a valid value in some scenarios, so it doesn't really mean the same thing as an\nabsence of value; \"I have a value that is null\" is not the same as \"I have no value\".\n\nMost functional languages have an option type, often called `Option` (as in F#) or `Maybe` (as in\nHaskell). C# could also benefit from such a type, so here it is!\n\n## Usage\n\n### Creating an option\n\n```csharp\nusing Hamlet;\n\n// An optional int with the value 42\nOption\u003cint\u003e a = Option.Some(42);\n// or just\nOption\u003cint\u003e a = 42;\n\n// An optional int with no value\nOption\u003cint\u003e b = Option.None();\n// or\nvar b = Option.None\u003cint\u003e();\n// or just\nOption\u003cint\u003e b = default;\n```\n\n### Checking if a value is present and getting the value\n\n```csharp\nOption\u003cint\u003e option = ...\n\n// Explicit check\nif (option.IsSome)\n{\n    int value = option.Value;\n\t...\n}\n\n// Using TryGetValue\nif (option.TryGetValue(out var value))\n{\n    ...\n}\n```\n\n### Usage with Linq\n\nLinq can be used to work with the value that might be contained in an option, by using projections or filters.\n\nIn the following example, `result` will be `Some(option.Value + 1)` if `option` has a value, and `None` otherwise:\n\n```csharp\nusing Hamlet;\nusing System.Linq;\n\nOption\u003cint\u003e option = ...\nOption\u003cint\u003e result =\n    from value in option\n    select value + 1;\n\n```\n\nIn the following example, `result` will be `Some(option.Value)` if `option` has a value greater than 0, and\n`None` otherwise:\n\n```csharp\nusing Hamlet;\nusing System.Linq;\n\nOption\u003cint\u003e option = ...\nOption\u003cint\u003e result =\n    from value in option\n    where value \u003e 0\n    select value;\n\n```\n\nValues from multiple options can also be combined. In the following example, `result` will be\n`Some(optA.Value + optB.Value)` if both `optA` and `optB` have a value, and `optB`'s value is greater than 0:\n\n```csharp\nusing Hamlet;\nusing System.Linq;\n\nOption\u003cint\u003e optA = ...\nOption\u003cint\u003e optB = ...\nOption\u003cint\u003e result =\n    from a in optA\n    from b in optB\n    where b \u003e 0\n    select a + b;\n```\n\n## Credits\n\nThe package logo is the icon [Hamlet](https://thenounproject.com/term/hamlet/258402/) by Priscilla Alves from the Noun Project, under license [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomaslevesque%2Fhamlet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomaslevesque%2Fhamlet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomaslevesque%2Fhamlet/lists"}