> On 13 Oct 2015, at 17:15, Marc Chantreux <m...@unistra.fr> wrote:
> 
> hello,
> 
> playing with <>, two questions came to my mind:
> 
> a) why isn't it "regular" ?
> 
>    use Test;
>    ok < foo bar bang >  ~~ List, "a list";
>    ok < foo bar      >  ~~ List, "a list";
>    ok < foo          >  ~~ List, "a list"; diag "actually a Str";
>    ok < >  ~~ List, "a list";
> 
> not only it is an exception to a simple rule but it waste memorizable and 
> short
> way to write a 1 element list. as always with perl6, i trust the perl6
> designers for having a very good reason and i'm curious: what is actually 
> this good reason for such a weird behave.

Well, for one, it is according to spec.  :-)

But the real reason, is that you can also use <a> to indicate the value of a 
key in a hash:

my %h = a => 42;
dd %h<a>;

Now, you want that to return a single Int, not a List with one Int in it.

So, that’s why <a> is a Str, and not a List.



> b) shortest way to hash ?
> 
> i used %(< class foo id bar >) in my code
> 
>    https://github.com/eiro/p6-Rototo/blob/master/t/basic.t#L21
> 
> i know it sounds stupid but i'm very sorry not being able to write (and most
> of all: read and edit)
> 
>    %< class foo id bar >

That is really a slice on an unnamed hash.  So that is ambiguous.

> 
> which isn't allowed ... but perl6 let me cheat: i can define a %
> operator working with an extra space
> 
>    use v6; 
>    use Rototo::html;
> 
>    sub H (*@data) { join '', @data }
>    sub prefix:<%> ( List $l ) is tighter(&infix:<,>) { %(|$l) }
> 
>    say % < id foo class bar >;             # class => bar, id => foo
>    ( % < id foo class bar > ).^name.say;   #   Hash
>    say H br :id<foo>, :class<bar>;         #  <br id="foo" class="bar"/>
>    say H br |% < id foo class bar >;       #  <br id="foo" class="bar"/>
>    say H p  |% < id foo class bar >, "this is a good thing”;
>        # <p id="foo" class="bar">this is a good thing</p>
> 
> this is working but raised 2 questions:
> 
> * if it was so easy, why isn't it in perl6? 
>    [ ] i missed the good paragraph of the documentation ?
>    [ ] i'm going to do something very stupid ? 
>    [ ] other, your answer here

$ 6 'dd %(<a b c d>)’
Hash % = {:a("b"), :c("d")}


> * i just don't know way i need | in front of % ... it just works but for
>  me %() was enought and explicit on what i wanted to get. can someone explain 
> ?

I guess without the |, you would pass a Hash to br.  With the |, it became a 
list of pairs.  Can’t really tell without the br code.  But, fwiw, I don’t 
think you need the prefix % at all  :-)



Liz

Reply via email to