On Mon, Oct 12, 2020 at 6:02 AM Brian Duggan <bdug...@matatu.org> wrote:

> On Saturday, October 10, William Michels via perl6-users wrote:
> > I can point to the (functional) R-programming language to show what
> happens
> > there. When manipulating "array-like" (i.e. vector) objects in R, you can
> > do nested function calls, or sequential (piped) function calls, and still
> > get the same data structure out at the end. So a 10-element input gives a
> > 10-element output.
>
> This seems pretty convenient and intuitive.  At least, it is possible
> to mimic that behavior in Raku:
>
>         List.^find_method('split').wrap: { $^a.map: *.split($^b) }
>         List.^find_method('sin').wrap: *.map: *.sin;
>
>         my @words = <a,b c,d>;
>         my @nums = 0, π/2, 3 * π/2;
>
>         say @words.split(',');
>         say @nums.sin;
>
> gives us
>
>   ((a b) (c d))
>   (0 1 -1)
>
> Brian
>

Thank you for your reply, Brian!

user@mbook:~$ #test Brian Duggan code:
user@mbook:~$ raku  #enter REPL
To exit type 'exit' or '^D'
>  List.^find_method('split').wrap: { $^a.map: *.split($^b) }
Routine::WrapHandle.new
>  List.^find_method('sin').wrap: *.map: *.sin;
Routine::WrapHandle.new
> my @words = <a,b c,d>;
[a,b c,d]
> my @nums = 0, π/2, 3 * π/2;
[0 1.5707963267948966 4.71238898038469]

> say @words.elems;
2
> say @words.split('Z').elems;
2
> say @words.split(',').elems;
2
> dd @words.split(',');
(("a", "b").Seq, ("c", "d").Seq).Seq
Nil

> say @nums.elems;
3
> say @nums.sin.elems;
3
> dd @nums.sin;
(0e0, 1e0, -1e0).Seq
Nil

Yes, that works very nicely. There is no longer a difference between
splitting on commas vs splitting on whitespace. And I especially like the
fact that a failed call to split() no longer joins all array elements into
a single string (see examples above and below when attempting to split on
'Z'):

> my @words2 = "a b", "c d e";
[a b c d e]
> dd @words2
Array @words2 = ["a b", "c d e"]
Nil
> say @words2.elems;
2
> say @words2.split('Z').elems;
2
> say @words2.split(' ').elems;
2
> dd @words2.split(' ');
(("a", "b").Seq, ("c", "d", "e").Seq).Seq
Nil

Thanks again, Brian!

Best, Bill.

Reply via email to