Executable library: Difference between revisions

no edit summary
No edit summary
Line 366:
 
For a serious library the <code>libhail.so</code> would have been put into a system lib dir, but for now we'll just leave it in the same directory, so to run the program, we need to give additional hints to tell it where to find the lib: <code>LD_LIBRARY_PATH=. ./hailtest</code>
 
=={{header|Clojure}}==
In a project's source directory, you might have a subfolder named <code>rosetta_code</code> with two Clojure source files:
<pre>
.
├── clojure.jar
└── rosetta_code
├── frequent_hailstone_lengths.clj
└── hailstone_sequence.clj
</pre>
The file <code>hailstone_sequence.clj</code> is an executable library:
<lang clojure>(ns rosetta-code.hailstone-sequence)
 
(defn next-in-hailstone
"Returns the next number in the Hailstone sequence that starts with x.
If x is less than 2, returns nil."
[x]
(when (> x 1)
(if (even? x)
(/ x 2)
(inc (* 3 x)))))
 
(defn hailstone-seq
"Returns a lazy Hailstone sequence starting with the number x."
[x]
(take-while some?
(iterate next-in-hailstone x)))
 
(defn -main [& args]
(let [h27 (hailstone-seq 27)]
(printf "The Hailstone sequence starting at 27 contains %s elements:\n%s ... %s.\n"
(count h27)
(vec (take 4 h27))
(vec (take-last 4 h27)))
(let [[number length] (apply max-key second
(map (fn [x] [x (count (hailstone-seq x))])
(range 100000)))]
(printf "The number %s has the longest Hailstone sequence under 100000, of length %s.\n"
number length))))</lang>
You can run it from the command line (<code>clojure.jar</code> is [https://clojure.org/community/downloads the jar that contains the Clojure language]):
<pre>$ java -cp clojure.jar clojure.main -m rosetta-code.hailstone-sequence
The Hailstone sequence starting at 27 contains 112 elements:
[27 82 41 124] ... [8 4 2 1].
The number 77031 has the longest Hailstone sequence under 100000, of length 351.
</pre>
You can also use its functions from other programs. The file <code>frequent_hailstone_lengths.clj</code>:
<lang clojure>(ns rosetta-code.frequent-hailstone-lengths
(:require [rosetta-code.hailstone-sequence
:refer [hailstone-seq]]))
 
(defn -main [& args]
(let [frequencies (apply merge-with +
(for [x (range 1 100000)]
{(count (hailstone-seq x)) 1}))
[most-frequent-length frequency]
(apply max-key val (seq frequencies))]
(printf (str "The most frequent Hailstone sequence length for numbers under 100000 is %s,"
" with a frequency of %s.\n")
most-frequent-length frequency)))</lang>
You can run it from the command line:
<pre>$ java -cp clojure.jar clojure.main -m rosetta-code.frequent-hailstone-lengths
The most frequent Hailstone sequence length for numbers under 100000 is 72, with a frequency of 1467.</pre>
Clojure also supports ahead-of-time compilation to class files, and the standard packaging tools [https://leiningen.org/ Leiningen] and [http://boot-clj.com/ Boot] provide meta-commands to compile and package Clojure programs into stand-alone runnable jars. At this point, Clojure has more or less the same executable library story as Java (class files can be named as the main entry point, and they can be included by other classes that import them).
 
=={{header|Déjà Vu}}==
Anonymous user