Executable library: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
Line 2,236:
The strategy here is check whether the main module is the same as the library module and to treat is as executable if it is but as a library otherwise.
 
<syntaxhighlight lang="ecmascriptwren">/* hailstoneExecutable_library.wren */
 
var Hailstone = Fn.new { |n|
Line 2,272:
// Check if it's being used as a library or not.
import "os" for Process
if (Process.allArguments[1] == "hailstoneExecutable_library.wren") { // if true, not a library
libMain_.call()
}</syntaxhighlight>
Line 2,279:
If we run this directly, we get the expected output:
<pre>
$ wren hailstoneExecutable_library.wren
 
For the Hailstone sequence starting with n = 27:
Line 2,292:
 
If we now create a second module which imports the above and calls its Hailstone function:
<syntaxhighlight lang="ecmascriptwren">/* hailstone2Executable_library_2.wren */
 
import "./hailstoneExecutable_library" for Hailstone
 
var freq = {}
Line 2,316:
We can now run this to check that the executable code in hailstone.wren has been suppressed, giving us:
<pre>
$ wren hailstone2Executable_library_2.wren
 
The Hailstone length returned most is 72, which occurs 1467 times.
9,477

edits