top | item 31564958

(no title)

atweiden | 3 years ago

Have you used Raku before? The new and improved regex syntax [1] IMHO completely obsoletes legacy PCRE. Writing regexes in other languages feels like stepping into an ICE vehicle after driving a Tesla: so crufty and old and obvious legacy.

Raku’s built-in grammars make parsers trivial to write. I effortlessly created two [2] — one for reordering fstab entries, and the other for converting human-readable LUKS offsets into cryptsetup sectors — on a lazy afternoon. Grammars in Raku make this second nature.

Then you have Raku’s multi-dispatch. It is more capable than Erlang/Elixir pattern matching:

    # a list with at least one element, extracting the head and tail
    multi sub tail(*@list ($head, *@tail)) { @tail }
    multi sub tail(*@list) { @list }

    [3]
    ==> tail()
    ==> say(); # []

    [3, 4]
    ==> tail()
    ==> say(); # [4]

    # pattern matching with arbitrary guards
    multi sub user($name where { is-valid-user($_) }) { $name.say }
    multi sub user($name) { "invalid name: $name".say }

    sub is-valid-user($name)
    {
        # notice the additive character class in this regex: “letters plus digits”
        # fail the match if $name is root
        try with $name ~~ /(<+:Letter +digit>+)/ { $0 ne 'root' or fail }; $!.not
    }

    user('name'); # name
    user('1234'); # 1234
    user('root'); # invalid name: root

    class Coordinates
    {
        has $.latitude is required;
        has $.longitude is required;
    }

    class City
    {
        has Str:D $.name is required;
        has Str:D $.state is required;
        has Str:D $.country is required;
        has Coordinates:D $.coordinates is required;
    }

    my $latitude = -37.840935;
    my $longitude = 144.946457;
    my Coordinates $coordinates .= new(:$latitude, :$longitude);
    my Str:D $name = 'Melbourne';
    my Str:D $state = 'Victoria';
    my Str:D $country = 'Australia';
    my City $melbourne .= new(:$name, :$state, :$country, :$coordinates);

    my City:D $sydney = do {
        my Coordinates:D $coordinates = do {
            my $latitude = -33.86514;
            my $longitude = 151.209900;
            Coordinates.new(:$latitude, :$longitude);
        };
        my Str:D $name = 'Sydney';
        my Str:D $state = 'New South Wales';
        my Str:D $country = 'Australia';
        City.new(:$name, :$state, :$country, :$coordinates);
    };

    # deeply nested argument deconstruction
    multi sub melbourne-or-bust(
        City:D $city (
            Str:D :$name where 'Melbourne',
            Str:D :$state,
            Str:D :$country,
            :$coordinates (
                :$latitude,
                :$longitude
            )
        )
    )
    {
        my $gist = qq:to/EOF/.trim;
        Welcome to the city of $name. It’s located in $state, $country.

        GPS coordinates: $latitude, $longitude
        EOF
        $gist.say;
    }

    multi sub melbourne-or-bust(City:D $city)
    {
        'This isn’t Melbourne.'.say;
    }

    melbourne-or-bust($melbourne); # Welcome to the city of Melbourne ...
    melbourne-or-bust($sydney); # This isn’t Melbourne
> Perl 6 was treated as the successor of Perl 5 -- and that was the mistake. It meant Perl 5 started dying,

Perl 6 took a long time to make, but how much did that matter? What was Perl going to do about Rails, Clojure, Go, Rust, JS/TS, and more? The world of programming languages used to be a lot smaller than it is today.

> Perl 6 had a new different syntax.

Inline::Perl5 [3] allows running legacy Perl 5 code in Perl 6 codebases.

[1]: https://docs.raku.org/language/5to6-nutshell#Regular_express...

[2]: https://github.com/atweiden/voidvault

[3]: https://github.com/niner/Inline-Perl5

discuss

order

No comments yet.