7

I created a minimal working module called new. The folder structure, link here is as follows:

new
│   .gitignore
│   Changes
│   dist.ini
│   LICENSE
│   META6.json
│   README.md
│
├───lib
│   │   new.rakumod
│   │
│   ├───Desc
│   │       Mean.rakumod
│   │
│   └───Deviation
│           DeviationMean.rakumod
│
└───t
        01-basic.rakutest

I have two functions, mean in Desc::Mean.rakumod and deviation_from_mean in Deviation::DeviationMean.rakumod modules in lib. These are simple functions, I don't want to have any namespace defined for them. So when I install this module, and try to use this module with use new, I want to be able to access these two functions without calling their sub-module names.

What I want to do is (which does not work now)

use new;

my @test1 = [6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4];
say mean(@test1);
say deviation_from_mean(@test1);

instead of (which works)

use new;
use Desc::Mean;
use Deviation::DeviationMean;

my @test1 = [6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4];
say mean(@test1);
say deviation_from_mean(@test1);

Is there a way to do this?

Suman Khanal
  • 3,079
  • 1
  • 17
  • 29

2 Answers2

5

Roughly speaking & following the docs you can put these method names as exports into the new namespace like this (in new.rakumod):

my package EXPORT::DEFAULT {
    OUR::mean := "Desc::Mean::mean";
    OUR::deviation_from_mean :=
         "Deviation::DeviationMean::deviation_from_mean";
}
librasteve
  • 6,832
  • 8
  • 30
  • 1
    @raiph - I can see that the referred prior question has a good answer - but its title is very obscure - if you reinstate this one and refer to it, then IMO there is a much bigger change that searchers will find their answer https://stackoverflow.com/questions/61179586/use-haskell-like-prelude-modules-in-a-module-in-raku – librasteve Mar 05 '22 at 23:29
  • I too thought the title was obscure. Last I knew marking a Q as a duplicate didn't remove a Q from either SO's or Google's search indexes and thus didn't impact searching. And I'm presuming it still doesn't and won't in the future. But I hear you, and see someone has upvoted. So I went ahead and re-opened. I was expecting SO to keep the duplicate mark too. (Per a meta post I had read.) But SO removed the duplicate marking! :( Sigh. Anyhoo, I decided to resolve that problem by writing another answer here that referred to jnthn's, as you suggested. Hth. – raiph Mar 08 '22 at 00:16
1
# main.raku
use lib 'lib';
use new;
say mean; # 42

# Desc::Mean
unit module Desc::Mean;
sub mean is export { 42 }

# new
sub EXPORT {
  {
    use Desc::Mean;
    return ::.pairs.grep(*.key ne '$_').Map;
  }
}
unit module new;

Notes:

  • The sub EXPORT { ... } in the new module must come before the unit module new; statement.

  • use further modules (eg Deviation::DeviationMean) as desired in the EXPORT sub to import those module's symbols into the new compunit; the return ::.pairs.grep(*.key ne '$_').Map; will then re-export all their symbols to whatever uses new.

For an explanation of the above see:

raiph
  • 31,607
  • 3
  • 62
  • 111