Group Abstract Group Abstract

Message Boards Message Boards

0
|
1.2K Views
|
0 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to build a paclet kernel

I have a paclet at https://resources.wolframcloud.com/PacletRepository/resources/PeterBurbery/LittleChildPaclet/.

I am attaching my current kernel file. Notice that all the functions for the paclet are in one notebook.

(* ::Package:: *)

PacletDirectoryLoad["C:\\Users\\Peter\\OneDrive - Marshall University\\little-child-paclet"]


<<PeterBurbery`LittleChildPaclet`ButterflyString`
<<PeterBurbery`LittleChildPaclet`DigitalRoot`
<<PeterBurbery`LittleChildPaclet`NonNegativeIntegerQ`


(* ::Section:: *)
(*Package Header*)


BeginPackage["PeterBurbery`LittleChildPaclet`"];


(* ::Text:: *)
(*Declare your public symbols here:*)


SayHello;
MaxRomanLength;
NumberTriangle;
FizzBuzz;
ThreeFive;
SameStartEndWords;
ToMorseCode;
IntegerPalindromeQ;
SquareSum;
PositiveIntegerQ;
WordListLookup;
TwoAndThreePointers;
OddBeforeEven;
PairsAddToHundred;
MaxRomanNumeralValue;
StringPatternQ;
ButterflyString;
DigitalRoot;
NonNegativeIntegerQ;
Begin["`Private`"];


(* ::Section:: *)
(*Definitions*)


(* ::Text:: *)
(*Define your public and private symbols here:*)


ClearAll["PeterBurbery`LittleChildPaclet`*"](*Its important to clear definitions, which Clear also does, and attributes, which clearAll does.*)
DigitalRoot[n_?NonNegativeIntegerQ]/;n>=0:=FixedPoint[Total[IntegerDigits[#]] &, n]
ButterflyString[input_?StringQ]:=StringJoin[input,StringReverse[input]]

SayHello[name_? StringQ] := "Hello " <> name <> "!";


MaxRomanLength[n_?PositiveIntegerQ]:=Max[StringLength[Array[RomanNumeral,n]]]


NumberTriangle[n_?PositiveIntegerQ]:=Column[Array[p|->Array[#&,p],n]]


FizzBuzz[n_?PositiveIntegerQ]:=Array[Which[Divisible[#,3]&&Divisible[#,5],"fizzbuzz",!Divisible[#,3]&&!Divisible[#,5],#,Divisible[#,3],"fizz",Divisible[#,5],"buzz"]&,n]


ThreeFive[n_?PositiveIntegerQ]:=Quotient[n,15](*also \[LeftFloor]n/15\[RightFloor] will work.*)


SameStartEndWords[char_?StringQ]:=WordListLookup[(char~~___~~char)|char]


Internal`ArgumentCountRegistry[StringPatternQ] ^= {1, 1};

StringPatternQ[_String] := True;

StringPatternQ[GeneralUtilities`Strings`PackagePrivate`x_] := GeneralUtilities`Strings`PackagePrivate`spq[GeneralUtilities`Strings`PackagePrivate`x]

Developer`Private`LHS$_StringPatternQ := RuleCondition[Developer`CheckArgumentCount[Developer`Private`LHS$, 1, 1]; Fail]


WordListLookup[stringpattern_?(StringPatternQ)]:=Select[StringMatchQ[stringpattern]][WordList[]]


IntegerPalindromeQ[n_?IntegerQ]:=IntegerReverse[n]==n
IntegerPalindromeQ[n_?IntegerQ,b_]/;b>1:=IntegerReverse[n,b]==n


ToMorseCode[text_?StringQ]:=StringRiffle[
  StringRiffle[
     StringReplace[Characters[#], 
      Thread[Alphabet[] -> CloudExpression["https://www.wolframcloud.com/obj/burbery1/CloudExpression/MorseCodeAlphabet"][]]], " "] & /@ 
   TextWords[ToLowerCase@text], "/"]


SquareSum[n_?PositiveIntegerQ]:=Fold[(#1 + #2)^2 &, 1, Rest@Range@n]


PositiveIntegerQ[n_]:=n\[Element]PositiveIntegers


TwoAndThreePointers[n_?PositiveIntegerQ]:=Sort[Sort /@ IntegerPartitions[n, All, {2, 3}]]


OddBeforeEven[list:{__?IntegerQ}]:=Join[Cases[x_?OddQ][list], Cases[x_?EvenQ][list]]


PairsAddToHundred[inputList_?VectorQ]:=Select[list |-> 
   And @@ Table[
     Counts[inputList][number] >= Count[list, number], {number, 
      list}]][IntegerPartitions[100, {2}, inputList]](*for why I chose VectorQ and not ListQ, see https://www.wolframcloud.com/obj/burbery1/Published/PairsAddToHundred.nb.
     *) /;AllTrue[IntegerQ][inputList]


MaxRomanNumeralValue[n_?PositiveIntegerQ]:=Module[{data},data=Array[RomanNumeral,n];Extract[data,List/@PositionLargest[data,1,Order[StringLength[#1],StringLength[#2]]&]]]

NonNegativeIntegerQ[n_]:=n\[Element]NonNegativeIntegers


(* ::Section::Closed:: *)
(*Package Footer*)


End[];
EndPackage[];

I don't think there's a way to put .wl files in Wolfram Community, unfortunately.

I was looking at the Sample Paclet and I noticed that instead of defining all the functions in one file they are loaded in as

<<SamplePublisher`SamplePaclet`AddOne`
<<SamplePublisher`SamplePaclet`AddTwo`

(see https://github.com/WolframResearch/PacletCICD-Examples-Sample/blob/main/Kernel/SamplePaclet.wl)

I think this would be useful to split up the paclet.

I am wondering how this could be done.

I then tried this:

PacletDirectoryLoad["C:\\Users\\Peter\\OneDrive - Marshall University\\little-child-paclet"]


<<PeterBurbery`LittleChildPaclet`ButterflyString`
<<PeterBurbery`LittleChildPaclet`DigitalRoot`
<<PeterBurbery`LittleChildPaclet`NonNegativeIntegerQ`
<<PeterBurbery`LittleChildPaclet`FizzBuzz`;
<<PeterBurbery`LittleChildPaclet`IntegerPalindromeQ`;
<<PeterBurbery`LittleChildPaclet`MaxRomanLength`;
<<PeterBurbery`LittleChildPaclet`MaxRomanNumeralValue`;
<<PeterBurbery`LittleChildPaclet`NonNegativeIntegerQ`;
<<PeterBurbery`LittleChildPaclet`NumberTriangle`;
<<PeterBurbery`LittleChildPaclet`OddBeforeEven`;
<<PeterBurbery`LittleChildPaclet`PairsAddToHundred`;
<<PeterBurbery`LittleChildPaclet`PositiveIntegerQ`;
<<PeterBurbery`LittleChildPaclet`SameStartEndWords`;

<<PeterBurbery`LittleChildPaclet`SayHello`;

<<PeterBurbery`LittleChildPaclet`SquareSum`;
<<PeterBurbery`LittleChildPaclet`StringPatternQ`;
<<PeterBurbery`LittleChildPaclet`ThreeFive`;
<<PeterBurbery`LittleChildPaclet`ToMorseCode`;
<<PeterBurbery`LittleChildPaclet`TwoAndThreePointers`;
<<PeterBurbery`LittleChildPaclet`WordListLookup`;

I'm not sure if I need the PacletDirectoryLoad. Is it necessary, or do the << suffice?

I then thought I'd prefer not to load all the files at once, so maybe there's a way to automatically load everything and I looked at Richard Hennigans Code Equivalence Utillities paclet on GitHub and noticed that he doesn't use << a bunch of times.

PreemptProtect[ BeginPackage[ "Wolfram`CodeEquivalenceUtilities`" ]; EndPackage[ ] ];

Wolfram`CodeEquivalenceUtilitiesLoader`$MXFile = FileNameJoin @ {
    DirectoryName @ $InputFileName,
    ToString @ $SystemWordLength <> "Bit",
    "CodeEquivalenceUtilities.mx"
};

(* :!CodeAnalysis::BeginBlock:: *)
(* :!CodeAnalysis::Disable::SymbolVersionTooNew:: *)
Quiet[
    If[ FileExistsQ @ Wolfram`CodeEquivalenceUtilitiesLoader`$MXFile,
        Get @ Wolfram`CodeEquivalenceUtilitiesLoader`$MXFile,
        WithCleanup[
            Get[ "Wolfram`CodeEquivalenceUtilities`Package`" ],
            { $Context, $ContextPath, System`$ContextAliases } = { ## }
        ] & [ $Context, $ContextPath, System`$ContextAliases ]
    ],
    General::shdw
];
(* :!CodeAnalysis::EndBlock:: *)

I then tried to adapt this to my paclet and I had this

(* ::Package:: *)

PreemptProtect[ BeginPackage[ "PeterBurbery`LittleChildPaclet`" ]; EndPackage[ ] ];

PeterBurbery`LittleChildPaclet`$MXFile = FileNameJoin @ {
    DirectoryName @ $InputFileName,
    ToString @ $SystemWordLength <> "Bit",
    "CodeEquivalenceUtilities.mx"
};

(* :!CodeAnalysis::BeginBlock:: *)
(* :!CodeAnalysis::Disable::SymbolVersionTooNew:: *)
Quiet[
    If[ FileExistsQ @ PeterBurbery`LittleChildPaclet`$MXFile,
        Get @ PeterBurbery`LittleChildPaclet`$MXFile,
        WithCleanup[
            Get[ "PeterBurbery`LittleChildPaclet`Package`" ],
            { $Context, $ContextPath, System`$ContextAliases } = { ## }
        ] & [ $Context, $ContextPath, System`$ContextAliases ]
    ],
    General::shdw
];

but my paclet's kernel file isn't loading. How can I load the files with <<? Is there a way to do something similar to CodeEquivalencePaclet with a mx file instead of manually specifying a bunch of <<?

Attachments:
POSTED BY: Peter Burbery
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard