Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how do you run Perl functions that take blocks as arguments? #161

Open
stuart-little opened this issue May 3, 2021 · 7 comments
Open

how do you run Perl functions that take blocks as arguments? #161

stuart-little opened this issue May 3, 2021 · 7 comments

Comments

@stuart-little
Copy link

stuart-little commented May 3, 2021

I am trying to do this:

#!/usr/bin/env perl6
use v6;

use List::AllUtils:from<Perl5> <bsearchidx>;

say bsearchidx {$_ - 1} (1,2,3);

as per that module's docs. The result in Raku:

===SORRY!=== Error while compiling <path-to-script>
Strange text after block (missing semicolon or comma?)
at <path-to-script>:6
------> say bsearchidx {$_ - 1}⏏ (1,2,3);
    expecting any of:
        infix
        infix stopper
        postfix
        statement end
        statement modifier
        statement modifier loop
@vendethiel
Copy link

vendethiel commented May 3, 2021

In Raku, there's no implicit comma after a block. You need to actually write out bsearchidx {$_ - 1}, (1,2,3);. That's not related to Inline::Perl5

@stuart-little
Copy link
Author

Sure, that was the first thing I tried.. However:

#!/usr/bin/env perl6
use v6;
use List::AllUtils:from<Perl5> <bsearchidx>;
say bsearchidx {$_ - 1}, (1,2,3);

Result:

Use of uninitialized value of type Any in numeric context
  in block <unit> at <script-path> line 6

-1

It should find the 1 at position 0, and hence return 0. So this doesn't play well with functions like bsearchidx, apparently.

@lizmat
Copy link
Collaborator

lizmat commented May 3, 2021

Perhaps Array::Sorted::Util can be of use?

@stuart-little
Copy link
Author

stuart-little commented May 3, 2021

Perhaps Array::Sorted::Util can be of use?

Oh, I don't doubt it. I can also EVAL arbitrary Perl5 code, as noted here, and I've gotten bsearchidx working that way.

But this doesn't quite resolve the matter generally: there are a great many functions all over the place in Perl5 packages that are advertised as taking arguments of the form

function { BLOCK } @LIST

(e.g. reduce, first, etc. etc.). I would still like to know what the best way is to render those in Raku by inlining. This comma/no-comma issue makes them all choke, it seems:

#!/usr/bin/env perl6
use v6;
use List::Util:from<Perl5> <first>;
say first {$_ > 0} (1,2,3);

complains without the comma because it's not there, and with the comma because of a

Use of uninitialized value of type Any

@niner
Copy link
Owner

niner commented May 3, 2021 via email

@stuart-little
Copy link
Author

That worked, thank you! I'm glad I asked :).

@tbrowder
Copy link

tbrowder commented May 3, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants