Live data from Hacker News

Haskell to Perl 6

docs.perl6.org

131–136 of 136 posts

Re: Haskell to Perl 6

#131
post #128
post #121

Earlier quoted context omitted.

You're welcome. Here's another quick example. It may or may not touch on your or the GGGP's point. So, no need to reply. I just thought I'd post something more while we wait for their reply. say now - INIT now This displays the time difference between the normal run-time moment that the `now` call before the minus is called and the `now` call after the minus sign which is run during the earlier INIT phase of executio…

>You're welcome. Here's another quick example. It may or may not touch on your or the GGGP's point. So, no need to reply. I'll reply anyway, to say thanks again :) I'll admit that I'm a bit phased by P6 phasers :) Had come across them just recently in the docs, initially thought, on a brief look, that at least the ENTER and LEAVE phasers were something like Python's __enter__ and __exit__ special methods used with co…

> I'll admit that I'm a bit phased by P6 phasers :)

.oO ( a pun set to stun? )

> ENTER and LEAVE ... like Python's __enter__ and __exit__

Aiui their primary usage involves similar use-cases and sugar.

The common aspect is that some code is declared and then the language/compiler calls that code when the time is right.

> But it seems like phasers may be something more, if not different.

Aiui P6:

* Has more of these hooks for block processing (eg PRE and POST for convenient pre and post condition assertions);

* Has hooks that aren't for blocks but instead much larger scale phases of program execution (eg there's CHECK which runs at the end of compilation, before running the program).

Also...

> Need to look into it some, including the "time traveling" code part.

What I meant by "time traveling" is as follows.

The compiler calls phased code when the times is right. Some of the time this is implicit. For example, in this Python code the __exit__ code gets called automatically by the Python interpreter as the block is exited:

    with foo as bar:
      qux
P6 phases can return values as follows:

    say now - CHECK now
the first `now` happens during regular run time. But the second happens during CHECK time -- which is at the end of compilation, before the program starts to run. The compiler knows to run the CHECK code earlier, then store the result, then subtract that from the `now` run at normal run-time.

I don't recall who first called that time traveling and I'm not even sure on reflection whether it makes sense to call it that but that's what I meant.

Re: Haskell to Perl 6

#132
post #131
post #128

Earlier quoted context omitted.

>You're welcome. Here's another quick example. It may or may not touch on your or the GGGP's point. So, no need to reply. I'll reply anyway, to say thanks again :) I'll admit that I'm a bit phased by P6 phasers :) Had come across them just recently in the docs, initially thought, on a brief look, that at least the ENTER and LEAVE phasers were something like Python's __enter__ and __exit__ special methods used with co…

> I'll admit that I'm a bit phased by P6 phasers :) .oO ( a pun set to stun? ) > ENTER and LEAVE ... like Python's __enter__ and __exit__ Aiui their primary usage involves similar use-cases and sugar. The common aspect is that some code is declared and then the language/compiler calls that code when the time is right. > But it seems like phasers may be something more, if not different. Aiui P6: * Has more of these ho…

>.oO ( a pun set to stun? )

Well done. Good one. :)

Thanks once again, I understand phasers better now.

Re: Haskell to Perl 6

#133
post #85

Earlier quoted context omitted.

One can write pretty much the same code in python/c++11/whatnot and it also feels quite natural to read: for number in numbers: if number Do you have a better example where the Perl syntax would shine?

Note: This is all in the context of Perl 5. You can assume the following works with little or no changes in Perl 6, along with probably 3+ other new ways to do it (which is actually one of my peeves about Perl 6). There's a few things. Full word but lower precedence boolean operators (and, or, not) which allows for combining statements in a way that &&, || and ! doesn't always facilitate, but really shine in making b…

That's a really nice explanation, thanks.

Re: Haskell to Perl 6

#134
post #115
post #114

Earlier quoted context omitted.

>In contrast to Perl5, Perl6 makes it far easier to avoid accidental spaghetti and does so without sacrificing any of the linguistic expressiveness that makes Perl generally so useful. Can you give some examples of that point, w.r.t. Perl6?

I'm not sure if I've nailed what the GP was speaking of or what you're looking for but anyway I do think the following is illustrative of the linguistic evolution P6 represents. Earlier this year at perlmonks (perhaps the top resource along with stackoverflow for experienced perl folk answering questions) a poster wanted solutions to a fairly basic operation:[1] > I need a function which will filter a nested hash, re…

Note that there was no need to actually create an infix operator, as you can use any code as an infix operator if you surround it with `[&( )]`.

    say $filter «[&(  -> $l, $r { $l and $r }  )]» $source
Golfing it down a bit, the shortest I was able to get was

    say $filter «[&(   &[and]   )]» $source;
The `&[and]` references the infix `and` operator.

I think the reason the following doesn't work that way is that `and` is too thunky.

    say $filter «and» $source;
    say $filter «[and]» $source; # identical to previous line
(Note that `and` is supposed to be thunky, so that isn't a bug.)

---

I personally would write this:

    sub infix: -> $_, $r {
      # note that the value to be matched with must be in $_
      # prior to using the `when` keyword.
      # it was done in the signature for conciseness

      $_ when $r
    }
Then the conditions can be code objects, regexes (which are a form of code object in Perl6), or literals. (I tried to get it to work with bare Junctions, but I was unable to get the `«»` part to pass it into the operator.)

It does mean that you have to use `True` rather than `1`.

    my $filter = {
        f2 => True,
        f3 => *.contains('data'),
        f5 => {
            f6 => /data/,
        },
        f10 => * == (0 | 1 | 42), # only match these values
        f11 => 42, # only match something equal to 42
    }

    say $source «l-when-r» $filter;

Re: Haskell to Perl 6

#135
post #129

Earlier quoted context omitted.

The following doesn't do what you think it does * > 10 && * %% 2 That is two WhateverCode objects which each take one argument. Since both are definite it gives you the second one. my &a = * > 10; my &b = * %% 2; my &c = &a && &b; &c === &b; # True If it wasn't split up by the `&&`, it would be a code object that took two arguments. # using multiplication (×) as a boolean and # (it always cooperates in the WhateverCo…

I noticed that error too but decided I didn't have the energy to provide a response worthy of the situation. I'm so glad I didn't try. As always you've provided a helpful answer and I learned something new. I hadn't twigged that `[...]` context would treat each Whatever as the same value -- though in retrospect I can see of course it would given the Perlish principle of doing something very useful rather than doing s…

Right it was late, and I was writing the code in different ways when writing here and writing in the REPL.

Re: Haskell to Perl 6

#136
post #74

Earlier quoted context omitted.

Would you care to elaborate on the non-clean parts in your opinion?

Things like this seem like odd design choices: my @menu = ; say @menu.contains('hamburger'); # True say @menu.contains('hot dog'); # False say @menu.contains('milk'); # True! say @menu.contains('er fr'); # True! say @menu.contains( ); # True!

You are sort-of right.

It should have used a different word.

A similar example would be the word `length`.

There is no `length` in Perl 6 because it is ambiguous. Instead there is `.elems` and `.chars`.

It is perhaps even a worse because there are other languages which use `contains` to see if the container has a given element.

Post reply on HN