Executable library: Difference between revisions
Content added Content deleted
m (Removed {{omit from|Go}}) |
(Added Go) |
||
Line 574: | Line 574: | ||
Among Hailstone sequences from 1 <= n < 100000, |
Among Hailstone sequences from 1 <= n < 100000, |
||
there are 72 sequences of length 1467.</pre> |
there are 72 sequences of length 1467.</pre> |
||
=={{header|Go}}== |
|||
To be directly executable, a Go application must contain a package called 'main' which includes a main() function from which execution begins automatically. Any other application is regarded as a library. |
|||
However, a package can consist of not just one but several source code files contained in the same directory. |
|||
Consequently, Go can emulate an executable library by splitting package 'main' into two files: |
|||
1. A file including the library code which includes (say) a libMain() function but no main() function as such. |
|||
2. A file including a main() function which calls libMain(). |
|||
If these two files are 'run' as a package, then they can mimic executing the library code directly. |
|||
To use the library from another executable application, all that is needed is to copy the first file to the application's directory and then 'run' the files as a package. Care would be needed to ensure that there are no name clashes in the 'combined' package. |
|||
Note that, although the 'go run' command may look like it's launching an interpreter, it does in fact build an application from the package files in a temporary directory and then executes it in a single step. |
|||
To complete the task, first create these two files in the 'modulino' directory: |
|||
<lang go>// modulino.go |
|||
package main |
|||
import "fmt" |
|||
// Function borrowed from Hailstone sequence task. |
|||
// 1st arg is the number to generate the sequence for. |
|||
// 2nd arg is a slice to recycle, to reduce garbage. |
|||
func hailstone(n int, recycle []int) []int { |
|||
s := append(recycle[:0], n) |
|||
for n > 1 { |
|||
if n&1 == 0 { |
|||
n = n / 2 |
|||
} else { |
|||
n = 3*n + 1 |
|||
} |
|||
s = append(s, n) |
|||
} |
|||
return s |
|||
} |
|||
func libMain() { |
|||
seq := hailstone(27, nil) |
|||
fmt.Println("\nHailstone sequence for the number 27:") |
|||
fmt.Println(" has", len(seq), "elements") |
|||
fmt.Println(" starts with", seq[0:4]) |
|||
fmt.Println(" ends with", seq[len(seq)-4:]) |
|||
var longest, length int |
|||
for i := 1; i < 100000; i++ { |
|||
if le := len(hailstone(i, nil)); le > length { |
|||
longest = i |
|||
length = le |
|||
} |
|||
} |
|||
fmt.Printf("\n%d has the longest Hailstone sequence, its length being %d.\n", longest, length) |
|||
}</lang> |
|||
<lang go>// modulino_main.go |
|||
package main |
|||
func main() { |
|||
libMain() |
|||
}</lang> |
|||
To emulate an executable library: |
|||
{{output}} |
|||
<pre> |
|||
$ go run modulino |
|||
Hailstone sequence for the number 27: |
|||
has 112 elements |
|||
starts with [27 82 41 124] |
|||
ends with [8 4 2 1] |
|||
77031 has the longest Hailstone sequence, its length being 351. |
|||
</pre> |
|||
Now create this file in the 'hailstone' directory: |
|||
<lang go>// hailstone.go |
|||
package main |
|||
import "fmt" |
|||
func main() { |
|||
freq := make(map[int]int) |
|||
for i := 1; i < 100000; i++ { |
|||
freq[len(hailstone(i, nil))]++ |
|||
} |
|||
var mk, mv int |
|||
for k, v := range freq { |
|||
if v > mv { |
|||
mk = k |
|||
mv = v |
|||
} |
|||
} |
|||
fmt.Printf("\nThe Hailstone length returned most is %d, which occurs %d times.\n", mk, mv) |
|||
}</lang> |
|||
and copy modulino.go to the 'hailstone' directory. The library can then be used in the 'normal' way: |
|||
{{output}} |
|||
<pre> |
|||
$ go run hailstone |
|||
The Hailstone length returned most is 72, which occurs 1467 times. |
|||
</pre> |
|||
=={{header|Io}}== |
=={{header|Io}}== |