Dynamic method generation?

2011-01-08 Thread Sean McAfee
Hello-- I recently wrote some Perl 5 code similar to the following, and I'm wondering what might be the analogous construction in Perl 6: package MyMatcher; my @SIMPLE_FIELDS = qw(FOO BAR BAZ BLETCH QUUX ...); for my $field (@SIMPLE_FIELDS) { no strict 'refs'; *{ is_\L$field } = sub {

Re: Perl 5 list assignment idiom

2017-03-13 Thread Sean McAfee
On Mon, Mar 13, 2017 at 12:37 PM, Will Coleda wrote: > Works the same in Perl 6, and you can avoid the parens. Using helper > subs that return one or two item lists, here's some sample code: > > $ perl6 > > sub one-thing { return ("hi",) } > sub one-thing () {

Design question re: function parameters

2017-03-07 Thread Sean McAfee
Recently this Perl 6 version of the factorial function was mentioned: sub postfix:(Int $n) { [*] 2 .. $n } I experimented a bit with it and found that I couldn't do '3'! as I naively expected from my Perl 5 intuition. Similarly, I can't pass an integer literal to a function that takes a

flatmap considered harmful?

2017-07-27 Thread Sean McAfee
While browsing the Perl 6 docs recently, here: https://docs.perl6.org/type/List#method_flatmap I noticed this paragraph for the first time: It is considered *bad practice* to use flatmap. Instead of .flatmap( ), > please use .map( ).flat as it is clear when the .flat is called and is > not

Re: another one liner

2017-08-05 Thread Sean McAfee
On Fri, Aug 4, 2017 at 10:18 PM, ToddAndMargo wrote: > On 08/04/2017 08:43 PM, Bruce Gray wrote: > >> >> P6-ish version: >> ifconfig | perl6 -e 'say lines.map({ ~$0 if /^(\S+) ": flags="/ >> }).sort[1]' >> > > Wait a second. How does map skip input elements like that? >

Re: flatmap considered harmful?

2017-07-29 Thread Sean McAfee
On Thu, Jul 27, 2017 at 12:43 PM, Will Coleda wrote: > I agree, that seems like pointless editorializing. > > If you can open a ticket at perl6/doc/issues on github, I'll remove > that sentence this evening. (or someone can beat me to it.) > > OK, it's done:

Re: Parse a string into a regex?

2017-05-11 Thread Sean McAfee
On Thu, May 11, 2017 at 4:01 PM, Timo Paulssen wrote: > The only way that comes to mind is to use EVAL, but that's not > golf-friendly at all. > > Perhaps you can find something sufficiently short based on .contains, > .index, .starts-with, .ends-with, and friedns? > I'm open

Parse a string into a regex?

2017-05-11 Thread Sean McAfee
I've been searching for how to parse a string into a regex, like qr/$str/ does in Perl 5, but so far without success. At first I assumed, by analogy with .Str, .List, etc, that I could call .Regex on a string, but this is not the case. On IRC's #perl6 I learned about the <$str> construct, which

Perl 6 ignores SIGPIPE

2017-06-16 Thread Sean McAfee
I see at http://www.moarvm.com/releases.html ...that as of the 2017.03 release, Perl 6 "ignores SIGPIPE by default." I discovered this for myself when I piped a program that generates unlimited output to the head utility, and the program did not exit when head was finished. Simple example: $

Re: Need append help

2017-09-29 Thread Sean McAfee
On Fri, Sep 29, 2017 at 11:27 AM, Brandon Allbery wrote: > On Fri, Sep 29, 2017 at 2:04 PM, ToddAndMargo > wrote: > >> >> Question: Is thee a pretty way like the above to do a prepend? >> > > No, sorry. > Actually, there seems to be: > my $x =

Call a function in a Whatever star

2017-12-13 Thread Sean McAfee
I just tried doing this: > map *(1), *+1, *+2, *+3 But I get an error: "No such method 'CALL-ME' for invocant of type 'Whatever'". Fair enough. This works: > map { $_(1) }, *+1, *+2, *+3 (2 3 4) Is there any alternative way to call a function through a whatever-star, as in my first example?

%% with zero denominator

2017-12-10 Thread Sean McAfee
I think of %% as being Perl 6's is-divisible-by operator, so I was a little surprised to discover this behavior: > 1 %% 0 Attempt to divide 1 by zero using infix:<%%> in block at line 1 The docs say a %% b is True if a % b is 0, so the error is as-designed, at least. But mightn't it make

Re: %% with zero denominator

2017-12-11 Thread Sean McAfee
On Mon, Dec 11, 2017 at 1:52 AM, Elizabeth Mattijsen <l...@dijkmat.nl> wrote: > > On 11 Dec 2017, at 04:42, Sean McAfee <eef...@gmail.com> wrote: > > The docs say a %% b is True if a % b is 0, so the error is as-designed, > at least. But mightn't it make more sense

Re: %% with zero denominator

2017-12-11 Thread Sean McAfee
On Mon, Dec 11, 2017 at 3:01 PM, <zof...@zoffix.com> wrote: > > On 2017-12-11 12:22 PM, Sean McAfee wrote: > >> Well, not really. I don't think x %% 0 should return a Failure at all. >> > > Is there a particular problem the current implementation fails to solv

Chained sequence operators

2018-01-26 Thread Sean McAfee
Today I stumbled across the fact that the sequence operator can be chained: > 1...5...1 (1 2 3 4 5 4 3 2 1) You can even reduce with it: > [...] 1, 5, 3, 10, 8 (1 2 3 4 5 4 3 4 5 6 7 8 9 10 9 8) And even sequences with custom generators can be joined: > 0,1,*+*...144,*/2...9 (0 1 1 2 3 5 8 13

Junctions now being stringified as one alternative per line?

2018-02-24 Thread Sean McAfee
I recently upgraded to Rakudo 2018.01 and just now encountered some perplexing behavior. Junctions, regardless of type, are being stringified one alternative per line. 1 & 2, 1 | 2, 1 ^ 2, and none(1, 2) are all displayed as a 1, a newline, and 2. Previously I would see eg. "all(1, 2)", which

Catching exceptions in expressions

2018-08-03 Thread Sean McAfee
I posted about this subject on Stack Overflow yesterday[1], but I chose a poor example of something that raises an exception (dividing by zero, which apparently doesn't necessarily do so) on which the answers have mostly focused. I was looking for a way to evaluate an expression, and if the

Re: Odd Complex behavior

2019-06-13 Thread Sean McAfee
On Thu, Jun 13, 2019 at 11:12 AM Brad Gilbert wrote: > > (-i).reals > (-0 -1) > Ah, so it's nothing particular to Complex: > bag 0e0, -0e0 bag(-0, 0) Can't say I'm thrilled to have two distinct zeroes.

Odd Complex behavior

2019-06-13 Thread Sean McAfee
> bag i, 2i - i bag(0+1i(2)) Well and good. > bag -i, -2i + i bag(0-1i, -0-1i) Huh? But this works as expected: > bag Complex.new(0, -1), Complex.new(0, -2) + Complex.new(0, 1) bag(0-1i(2)) Is this a bug, or is there a sense in which it's correct?

Subscripting with a list of slices

2019-08-16 Thread Sean McAfee
Today I was surprised and pleased to find that I could apparently subscript an array with a list of lists of indices, and get a list of slices back: > my @array = 9 ... 0 [9 8 7 6 5 4 3 2 1 0] > @array[map { $_, $_ + 1 }, ^9] ((9 8) (8 7) (7 6) (6 5) (5 4) (4 3) (3 2) (2 1) (1 0)) Neat! But

Re: Lookbehind assertion weirdness

2019-08-23 Thread Sean McAfee
On Thu, Aug 22, 2019 at 6:11 PM yary wrote: > Perl 6 is doing the right thing. The dot matches any character. In > this case, matching the final ':'. The next bit of the regex says the > cursor has to be after 1:, and indeed, after matching the ':' the > cursor is after '1:', so the substitution

Lookbehind assertion weirdness

2019-08-22 Thread Sean McAfee
This seems like a bug, but I thought I'd ask here before reporting it. $_ = '1:'; s/./x/; .say; This prints "1x". Is that what's supposed to happen somehow? I would have thought that '1:' should only match a literal "1:", leaving nothing for the dot to match.

Re: Raku -npe command line usage

2020-05-08 Thread Sean McAfee
On Fri, May 8, 2020 at 6:53 AM Brad Gilbert wrote: > So together that would be: > > raku -ne 'BEGIN my @i; @i.push($_) if /^WARN/; END .say for @i.sort' > Or alternately the main body of the loop can be written: (my @i).push($_) if /^WARN/; Or even: push my @i: $_ if /^WARN/;

Re: print particular lines question

2020-08-25 Thread Sean McAfee
On Tue, Aug 25, 2020 at 2:31 PM Andy Bach wrote: > Pretty cool - I didn't know about the bare "$" as a magic state var. > They can be pretty great, especially when combined with the magic op= operators that (in essence) know about identity elements. I've done a few challenges on the Code Golf

Re: Brace interpolation in strings creates a new scope?

2020-10-26 Thread Sean McAfee
On Mon, Oct 26, 2020 at 12:04 PM Elizabeth Mattijsen wrote: > Personally, I would avoid the use of the anonymous state variable. I > think it is an example where Larry would nowadays apply rule #2. > Well, I wouldn't use a construction like that in real code. It just seems like one of the

Brace interpolation in strings creates a new scope?

2020-10-26 Thread Sean McAfee
I'm putting together a Raku presentation for my co-workers and have a section on state variables. This is my example: sub how-many { state $times; say "You called me {++$times} times"; } > how-many You called me 1 times > how-many You called me 2 times > how-many You called me 3 times

Re: regex interpolation of array

2020-06-13 Thread Sean McAfee
On Sat, Jun 13, 2020 at 10:21 AM Brad Gilbert wrote: > That was just a dumb example. > An incredibly dumb example. > > So what happens is that `Bool.pick` chooses The Bool values of either > `True` or `False`. > It does this at every position in the string. > > 'TrueFalse' ~~ / <{ Bool.pick

Sending a string to a subprocess

2020-11-11 Thread Sean McAfee
I posted this question on Stack Overflow a few days ago: https://stackoverflow.com/questions/64757138/raku-sending-a-string-to-a-subprocesss-standard-input ...but there haven't been any answers, so I'm asking here. The tl;dr is that I don't see an obvious way to send data to a subprocess's

Re: Sending a string to a subprocess

2020-11-11 Thread Sean McAfee
On Wed, Nov 11, 2020 at 4:44 PM Paul Procacci wrote: > So sorry... hit enter way too early. > > https://docs.raku.org/type/Proc > > my $p = run "ssh", "myhost", "cat - > outfile", :in, :out; > $p.in.say: "Hello,\nworld!"; > > Ah, of course. It's so obvious now. Thanks!

Junctions wrapped in singleton lists

2020-11-13 Thread Sean McAfee
I just tried making a sequence of junctions and found that each one ended up wrapped in a singleton list somehow: > ({ 1 | -1 } ... *)[^3] ((any(1, -1)) (any(1, -1)) (any(1, -1))) Strangely, I can set an ending condition that works like I would expect, but the sequence still produces

Re: Constructing a number from digits in an arbitrary base

2020-11-02 Thread Sean McAfee
On Fri, Oct 30, 2020 at 2:19 PM Elizabeth Mattijsen wrote: > > On 30 Oct 2020, at 22:11, Sean McAfee wrote: > > With polymod, I can get the values of digits even in large bases like > 101: > > > > > 1234567890.polymod(101 xx *) > > (46 20 26 87 11)

Re: Constructing a number from digits in an arbitrary base

2020-10-30 Thread Sean McAfee
Sorry, it seems I wasn't explicit enough. With polymod, I can get the values of digits even in large bases like 101: > 1234567890.polymod(101 xx *) (46 20 26 87 11) Given a list of digit values like that, and the base, I want to reconstruct the original number. One way would be the one

Re: Constructing a number from digits in an arbitrary base

2020-10-30 Thread Sean McAfee
On Fri, Oct 30, 2020 at 2:19 PM Elizabeth Mattijsen wrote: > > On 30 Oct 2020, at 22:11, Sean McAfee wrote: > > > sum <46 20 26 87 11> Z* (1, * * 101 ... *) > > 123456789 > > > > ...but I'm hoping there's something more concise, shorter, and/o

Constructing a number from digits in an arbitrary base

2020-10-30 Thread Sean McAfee
I want to construct a number from its digits in some arbitrary base. That is, to essentially do the inverse of this kind of polymod call: my @digits = $number.polymod($base xx *); I have a nagging feeling that I've seen a very concise way to do this in Raku before, but now it escapes me,

div & mod don't coerce arguments to Int?

2021-12-18 Thread Sean McAfee
While coding today, I was a little surprised to discover that the div operator doesn't coerce its arguments to integers. So for example, the expressions 25 div '5' and pi div 5 raise errors. I checked the Operators doc page and saw that, sure enough, div (and mod, I found) accept only actual

A nice touch

2021-10-30 Thread Sean McAfee
Recently I was golfing the "hyperfactorial," defined for a number 푛 as 푛**푛 × (푛-1)**(푛-1) × (푛-2)**(푛-2) × ... × 1. I created a quite concise Raku function: { [*] [\*] $_...1 } The only problem was that this function returns zero for a zero input, whereas the hyperfactorial of 0 is

Re: how do I turn a real into and array of Integers?

2021-10-31 Thread Sean McAfee
On Sun, Oct 31, 2021 at 6:51 PM ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > >> How do I get more digits out of sqrt? > > On 10/31/21 17:50, Kevin Pye wrote: > > You don't. > > > > sqrt is a function which acts on 64-bit floating point numbers, and > there's no more meaningful

Re: A nice touch

2021-10-31 Thread Sean McAfee
On Sat, Oct 30, 2021 at 9:21 PM Wes Peng wrote: > What's the "**" operator in perl? I most of the time use R for math, not > familiar with this operation. > It's exponentiation: https://docs.raku.org/language/operators#infix_** That wasn't a Raku expression I used it in, though, just my

Re: how do I turn a real into and array of Integers?

2021-10-31 Thread Sean McAfee
On Sun, Oct 31, 2021 at 9:08 AM Andinus via perl6-users < perl6-users@perl.org> wrote: > put 2.sqrt.comb.grep(*.Int)>>.Int[^10].raku # 10 digits > comb takes an argument that can save you a method call: 2.sqrt.comb.grep(*.Int) ==> 2.sqrt.comb(/\d/)

Re: A nice touch

2021-10-31 Thread Sean McAfee
On Sat, Oct 30, 2021 at 9:03 PM Ralph Mellor wrote: > On Sat, Oct 30, 2021 at 8:38 PM Sean McAfee wrote: > > Anyway, pretty cool! > > I agree it's cool, at least in a golfing sense. > > But is your explanation right? > > The *range* operator (`..`), if the rhs is l

Re: how do I turn a real into and array of Integers?

2021-11-01 Thread Sean McAfee
On Mon, Nov 1, 2021 at 1:53 AM sisyphus wrote: > Note that what you have there is a 200-decimal-digit (around 658-bit) > precision representation of the square root of 2 - which is quite different > to the (53-bit precision) Real sqrt(2). > Always gotta be careful with reals. I also attempted

Subscripting, semicolons, and adverbs

2021-12-10 Thread Sean McAfee
I was writing some code that involved accessing an array-of-arrays. I wanted to check whether both of a given pair of indices were in bounds, so I wrote: return () if @array[$y]:!exists or @array[$y][$x]:!exists or ... The double index check bugged me a bit, since I wondered if there might

Re: Subscripting, semicolons, and adverbs

2021-12-10 Thread Sean McAfee
On Fri, Dec 10, 2021 at 1:48 PM I wrote: > So this looks pretty buggy, but I wonder if I'm somehow invoking undefined > behavior by combining semicolons and adverbs while subscripting. The > online docs cover both features separately, but not in tandem that I can > see. > Actually, immediately

Re: Subscripting, semicolons, and adverbs

2021-12-11 Thread Sean McAfee
On Sat, Dec 11, 2021 at 11:28 AM Ralph Mellor wrote: > On Fri, Dec 10, 2021 at 9:49 PM Sean McAfee wrote: > > > > @array[-1]:exists is a syntax error, even though it looks like > > it should just evaluate to True. > > In Raku `foo[-N]` is invalid. At compile-time

Well, this is disappointing.

2023-10-02 Thread Sean McAfee
A little while ago I developed a Raku program to look up file ownership in a Github CODEOWNERS file. My first attempt worked by converting the glob-like CODEOWNERS patterns into regexes, eg: app/views/**/*.rb --> /^ 'app/views/' [<-[/]>+]* % '/' '/' <-[/]>* '.rb' $/ Surprisingly, this approach

Regex surprises

2022-09-12 Thread Sean McAfee
Hello-- I stumbled across a couple of bits of surprising regex behavior today. First, consider: S:g[ x { say 1 } ] = say 2 given "xx" I expected this to print 1, 2, 1, 2, but it prints 1, 1, 2, 2. So it looks like, in a global substitution like this, Raku doesn't look for successive matches

Re: Using Inline::Python

2022-09-09 Thread Sean McAfee
, then it > would help using the latest released version (aka 2022.07). Especially > since 2021.04 is from before the new-disp work being merged, so it is not > completely unlikely it functions better now. > > > On 9 Sep 2022, at 20:44, Sean McAfee wrote: > > > > I still s

Re: Using Inline::Python

2022-09-09 Thread Sean McAfee
ne::Python seems to be mostly even older. On Fri, Sep 9, 2022 at 11:35 AM Elizabeth Mattijsen wrote: > To rule out any REPL artefacts, do you see the same thing if you put the > code in a script and run the script? > > > On 9 Sep 2022, at 20:17, Sean McAfee wrote: > >

Using Inline::Python

2022-09-09 Thread Sean McAfee
Hello-- I recently started playing around with PySpark. It soon occurred to me that it would be a lot more fun to work in Raku instead of Python, and I recalled that it's supposed to be possible to get handles to Python objects from Raku and call methods on them seamlessly, so I tried to make it

Re: $/ not always set after a regex match?

2022-12-30 Thread Sean McAfee
would be very much in favour of getting rid of that "feature", fwiw. > > > On 28 Dec 2022, at 18:45, Sean McAfee wrote: > > > > But if a sequence has its own $/, why does * ~~ /9/ set $/? > > > > Actually it's not just sequences, as a little more experi

Re: $/ not always set after a regex match?

2022-12-28 Thread Sean McAfee
c 28, 2022 at 12:01 PM Elizabeth Mattijsen wrote: > This isn't specific to the REPL: > > $ raku -e 'say 1 ... /9/; say $/' > (1 2 3 4 5 6 7 8 9) > Nil > > I can only assume that the sequence has its own scope for $/, and thus > isn't visible outside of it. > > >

$/ not always set after a regex match?

2022-12-28 Thread Sean McAfee
In a fresh 2022.12 Raku REPL, when the endpoint of a sequence is a Regex, the $/ variable seems not to be set: [0] > 1 ... /9/ (1 2 3 4 5 6 7 8 9) [1] > $/ Nil If I match more explicitly using a WhateverCode, it works: [2] > 1 ... * ~~ /9/ (1 2 3 4 5 6 7 8 9) [3] > $/ 「9」 Is this the intended

Re: $/ not always set after a regex match?

2022-12-30 Thread Sean McAfee
On Fri, Dec 30, 2022 at 4:12 PM The Sidhekin wrote: > On Fri, Dec 30, 2022 at 8:51 PM Vadim Belman wrote: > >> It is not "untrue". The sequence you produced goes nowhere. Thus the sink >> context. >> > > "Sink context" is true. > > "Useless use" is debatable, at least. > It's not useless

Re: `lines.contains( / \h / )` returning True for input strings not containing horizonal whitespace

2023-01-26 Thread Sean McAfee
On Thu, Jan 26, 2023 at 12:05 PM William Michels via perl6-users < perl6-users@perl.org> wrote: > ~$ raku -e 'put "1\n2\n3";' | raku -e 'put lines.contains(/ \h /) ?? True > !! False;' > True > lines() returns a Seq. The contains method for a Seq coerces its argument to a Str and calls contains

Strange behavior with sequence of functions

2023-06-29 Thread Sean McAfee
I was trying to construct a sequence of functions using the sequence operator, and encountered some very puzzling behavior. I was able to reduce it down to some very simple examples. [1] > my @s = +*, -> { } ... * [...] [2] > @s[0] Too few positionals passed; expected 1 argument but got 0 in