Jump to content

Modulinos: Difference between revisions

no edit summary
No edit summary
Line 1,344:
print("Life means %s." % meaning_of_life())
print("Death means invisible scary skeletons.")</lang>
 
=={{header|Scala}}==
 
Note: Either the current directory (.) must be in the CLASSPATH environment variable, or the CLASSPATH should not be set.
 
~/.profile:
 
<lang sh># Scala
export CLASSPATH=$CLASSPATH:.</lang>
 
or
 
~/.profile:
 
(empty)
 
Example:
 
<lang sh>$ scala ScriptedMain.scala
Main: The meaning of life is 42
$ scalac ScriptedMain.scala Test.scala
$ scala Test
Test: The meaning of life is 42</lang>
 
ScriptedMain.scala:
 
Note the omission of shebangs. scalac refuses to compile code with shebangs.
 
<lang scala>object ScriptedMain {
val meaningOfLife = 42
 
def main(args: Array[String]) {
println("Main: The meaning of life is " + meaningOfLife)
}
}</lang>
 
Test.scala:
 
<lang scala>object Test {
def main(args: Array[String]) {
println("Test: The meaning of life is " + ScriptedMain.meaningOfLife)
}
}</lang>
 
=={{header|Smalltalk}}==
 
Note that the ScriptedMain package must be installed in order for test.st to access code from scriptedmain.st.
 
Example
 
<lang shell>$ gst-package -t ~/.st package.xml &>/dev/null
 
$ ./scriptedmain.st
Main: The meaning of life is 42
 
$ ./test.st
Test: The meaning of life is 42</lang>
 
package.xml
 
<lang xml><packages>
<package>
<name>ScriptedMain</name>
<filein>scriptedmain.st</filein>
<file>scriptedmain.st</file>
</package>
</packages></lang>
 
scriptedmain.st
 
<lang smalltalk>"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
 
Object subclass: ScriptedMain [
ScriptedMain class >> meaningOfLife [ ^42 ]
]
 
| main |
 
main := [
Transcript show: 'Main: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.
].
 
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
main value.
].</lang>
 
test.st
 
<lang smalltalk>"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
 
"
PackageLoader fileInPackage: 'ScriptedMain'.
 
Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.</lang>
 
=={{header|R}}==
A way to check if code is running at "top level" is to check <code>length(sys.frames())</code>. This value will be zero for a file being run with <code>Rscript</code>, the <code>--file=</code> argument, or at the command line, and will be greater than 0 in all other conditions (such as package loading or code being sourced from another file.)
Line 1,572 ⟶ 1,475:
$ ./test
Test: The meaning of life is 42</lang>
 
 
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Scala}}
===Unix version===
<lang bash>#!/bin/sh
exec scala "$0" "$@"
!#
def hailstone(n: Int): Stream[Int] =
n #:: (if (n == 1) Stream.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))
 
val nr = argv.headOption.map(_.toInt).getOrElse(27)
val collatz = hailstone(nr)
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(collatz.toList)
println(s"It has ${collatz.length} elements.")</lang>
===Windows cmd.exe interpretation===
<lang winbatch>::#!
@echo off
call scala %0 %*
pause
goto :eof
::!#
def hailstone(n: Int): Stream[Int] =
n #:: (if (n == 1) Stream.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))
 
val nr = argv.headOption.map(_.toInt).getOrElse(27)
val collatz = hailstone(nr)
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(collatz.toList)
println(s"It has ${collatz.length} elements.")
</lang>{{out}}<pre>C:\>Hailstone.cmd 42
Use the routine to show that the hailstone sequence for the number: 42.
List(42, 21, 64, 32, 16, 8, 4, 2, 1)
It has 9 elements.</pre>
 
=={{header|Smalltalk}}==
 
Note that the ScriptedMain package must be installed in order for test.st to access code from scriptedmain.st.
 
Example
 
<lang shell>$ gst-package -t ~/.st package.xml &>/dev/null
 
$ ./scriptedmain.st
Main: The meaning of life is 42
 
$ ./test.st
Test: The meaning of life is 42</lang>
 
package.xml
 
<lang xml><packages>
<package>
<name>ScriptedMain</name>
<filein>scriptedmain.st</filein>
<file>scriptedmain.st</file>
</package>
</packages></lang>
 
scriptedmain.st
 
<lang smalltalk>"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
 
Object subclass: ScriptedMain [
ScriptedMain class >> meaningOfLife [ ^42 ]
]
 
| main |
 
main := [
Transcript show: 'Main: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.
].
 
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
main value.
].</lang>
 
test.st
 
<lang smalltalk>"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
 
"
PackageLoader fileInPackage: 'ScriptedMain'.
 
Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.</lang>
 
=={{header|SAC}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.