{"id":16723685,"url":"https://github.com/eckankar/types-algebraic","last_synced_at":"2025-06-10T18:08:20.171Z","repository":{"id":56837636,"uuid":"99966414","full_name":"Eckankar/Types-Algebraic","owner":"Eckankar","description":"Algebraic Data Types in Perl!","archived":false,"fork":false,"pushed_at":"2021-02-20T13:18:48.000Z","size":62,"stargazers_count":2,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T05:41:34.602Z","etag":null,"topics":["algebraic-data-types","perl","perl-module"],"latest_commit_sha":null,"homepage":"https://metacpan.org/pod/Types::Algebraic","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Eckankar.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-10T21:31:37.000Z","updated_at":"2023-11-07T12:46:18.000Z","dependencies_parsed_at":"2022-09-12T11:51:41.013Z","dependency_job_id":null,"html_url":"https://github.com/Eckankar/Types-Algebraic","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/Eckankar%2FTypes-Algebraic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eckankar%2FTypes-Algebraic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eckankar%2FTypes-Algebraic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eckankar%2FTypes-Algebraic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eckankar","download_url":"https://codeload.github.com/Eckankar/Types-Algebraic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eckankar%2FTypes-Algebraic/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259123865,"owners_count":22808876,"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":["algebraic-data-types","perl","perl-module"],"created_at":"2024-10-12T22:39:17.308Z","updated_at":"2025-06-10T18:08:20.123Z","avatar_url":"https://github.com/Eckankar.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/Eckankar/Types-Algebraic.svg?branch=master)](https://travis-ci.org/Eckankar/Types-Algebraic)\n[![Kwalitee status](http://cpants.cpanauthors.org/dist/Types-Algebraic.png)](https://cpants.cpanauthors.org/dist/Types-Algebraic)\n\n# NAME\n\nTypes::Algebraic - Algebraic data types in perl\n\n# SYNOPSIS\n\n    use Types::Algebraic;\n\n    data Maybe = Nothing | Just :v;\n\n    my $sum = 0;\n    my @vs = ( Nothing, Just(5), Just(7), Nothing, Just(6) );\n    for my $v (@vs) {\n        match ($v) {\n           with (Nothing) { }\n           with (Just $v) { $sum += $v; }\n        }\n    }\n    say $sum;\n\n# DESCRIPTION\n\nTypes::Algebraic is an implementation of [algebraic data types](https://en.wikipedia.org/wiki/Algebraic_data_type) in perl.\n\nThese kinds of data types are often seen in functional languages, and allow you to create and consume structured data containers very succinctly.\n\nThe module provides two keywords: [\"data\"](#data) for creating a new data type, and [\"match\"](#match) to provide pattern matching on the type.\n\n# USAGE\n\n## Creating a new type with `data`\n\nThe `data` keyword is used for creating a new type.\n\nThe code\n\n    data Maybe = Nothing | Just :v;\n\ncreates a new type, of name `Maybe`, which has 2 _data constructors_, `Nothing` (taking no parameters), and `Just` (taking 1 parameter).\n\nYou may insantiate values of this type by using one of the constructors with the appropriate number of arguments.\n\n    my $a = Nothing;\n    my $b = Just 5;\n\n## Unpacking values with `match`\n\nIn order to access the data stored within one of these values, you can use the `match` keyword.\n\n    my $value = Just 7;\n    match ($value) {\n        with (Nothing) { say \"There was nothing in there. :(\"; }\n        with (Just $v) { say \"I got the value $v!\"; }\n    }\n\nThe cases are matched from the top down, and only the first matching case is run.\n\nYou can also create a default fallback case, which will always run if reached.\n\n    data Color = Red | Blue | Green | White | Black;\n    match ($color) {\n        with (Red) { say \"Yay, you picked my favorite color!\"; }\n        default    { say \"Bah. You clearly have no taste.\"; }\n    }\n\n## Nested patterns\n\nNote, patterns can be nested, allowing for more complex unpacking:\n\n    data PairingHeap = Empty | Heap :head :subheaps;\n    data Pair = Pair :left :right;\n\n    # Merge two pairing heaps (https://en.wikipedia.org/wiki/Pairing_heap)\n    sub merge {\n        my ($h1, $h2) = @_;\n\n        match (Pair($h1, $h2)) {\n            with (Pair Empty $h) { return $h; }\n            with (Pair $h Empty) { return $h; }\n            with (Pair (Heap $e1 $s1) (Heap $e2 $s2)) {\n                return $e1 \u003c $e2 ? Heap($e1, [$h2, @$s1])\n                                 : Heap($e2, [$h1, @$s2]);\n            }\n        }\n    }\n\n# LIMITATIONS\n\n- Currently, match statements can't be nested.\n\n# BUGS\n\nPlease report bugs directly on [the project's GitHub page](https://github.com/Eckankar/Types-Algebraic).\n\n# AUTHOR\n\nSebastian Paaske Tørholm \u003csebbe@cpan.org\u003e\n\n# COPYRIGHT\n\nCopyright 2020- Sebastian Paaske Tørholm\n\n# LICENSE\n\nThis library is free software; you can redistribute it and/or modify\nit under the same terms as Perl itself.\n\n# SEE ALSO\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feckankar%2Ftypes-algebraic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feckankar%2Ftypes-algebraic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feckankar%2Ftypes-algebraic/lists"}