I recently started creating Mathematica packages. I find it easiest to develop functions in a Notebook (.nb file). This allows me to add sections, comments and examples.
I don't want to duplicate code, certainly not by copy/pasting my function from the .nb file to the .m package file. So I would prefer automatically generate Package code from a Notebook.
Are there direct methods of generating a .m file? Alternative methods of developping Packages in a Notebook environment?
The following is just an example to illustrate one possible method to avoid duplication of code. Imagine that I have a .nb with the following function:
toSquare = Function[{x}, x^2]
Now I want to export this Input as pure code, for example by adding a button which exports this function to a .m file:
Button["Export as package", toPackage["packageName.m", ...]]
This should generate the file packageName.m with the following content:
BeginPackage["Square`"];
toSquare::usage="toSquare[x] squares numbers.";
toSquare=Function[{x},x^2];
EndPackage[]
Can Inputs be converted to strings? Can Functions be converted to strings? Exporting strings to a .m file seems slightly hacky to me, so if there are alternatives, I'd prefer that.