Talk:Modulinos: Difference between revisions

m
Mcandre moved page Talk:Scripted main to Talk:Modulinos: Use community accepted name for this article's content. "Modulinos" is still obscure, but less obscure than my goofy "scripted main" name.
m (Mcandre moved page Talk:Scripted main to Talk:Modulinos: Use community accepted name for this article's content. "Modulinos" is still obscure, but less obscure than my goofy "scripted main" name.)
 
(17 intermediate revisions by 7 users not shown)
Line 50:
 
::::: Yes, there is some confusion over compiled vs scripted vs interpreted vs assembled code and what qualifies as an executable. For the purposes of this article, I consider any ELF/MACH-O/DOS/WIN32 binary file, and any shebang'ed file as an executable. Strictly compiled languages (C, C++, D, Objective C) don't seem to belong in this article, because programs in those languages already have a good idea of what a main() function does and where it belongs. But, in the subset of compiled languages, some people prefer not to use separate files for API and CLI. E.g., "weak" allows this in C/C++.
 
:::::: I think windows COM servers would be an example of doing this kind of thing with a compiled language. It is, of course, OS specific. But the whole concept of "executable" is also OS specific. Tasks which ask us for a subtle variation on an OS specific concept would typically be OS specific tasks. --[[User:Rdm|Rdm]] 16:19, 12 May 2011 (UTC)
 
::::: For major scripting languages (Perl, Python, Ruby), it's often assumed that API and CLI work together. To beat a dead horse, Python's if __name__=="__main__" is a much sought-after snippet (2,030,000 results in Google at last count). Coders want to know how to do this. I think they want to do this in other languages as well.
::::: For languages in the middle (Octave, Common Lisp, Chicken Scheme, Clojure), it's plumb difficult to achieve this behavior. Some languages can only partially do "scripted main"; they either require obscure shebangs (Clojure), or worse, require special commands in the shell (LLVM). If I don't add a runtime example for a language, that means that both "python scriptedmain.py" and "./scriptedmain.py" work as expected. IRC users refuse to help me, claiming that their language's REPL is infinitely better than shell. That may be, but I like to provide standard, POSIX, CLI interfaces whenever possible. It took me hours to wrestle working snippets--other coders shouldn't have to.
 
::::: I want to get back on IRC and chat with you guys. You seem interested, and rightly curious. In the mean time, feel free to twitter me at @mcandre. --[[User:Mcandre|Mcandre]]
 
:::::: FWIW, there's no problem with OS-specific examples — that's how life is sometimes — though it's best if they're described as such. It does mean that there's an opportunity to describe how to do it on other operating systems/platforms, but that's all cool. (Re language snootiness, hang in there and fight the good fight. The world is open, not closed.) –[[User:Dkf|Donal Fellows]] 09:53, 13 May 2011 (UTC)
 
== Ambiguities ==
Line 75 ⟶ 79:
 
:That said, my current impression is that you are asking us to illustrate putting the API in a separate file from the definition of "main". If that is the case, many of the examples here (including the one I wrote) are incorrect. --[[User:Rdm|Rdm]] 13:49, 6 March 2011 (UTC)
 
:: I was wrong about using a blank header file, but one of you on #rosettacode helped me write a definitely working version using the "weak" attribute. Thanks much.
 
:: My examples could certainly be cleaned up or even patched up. They're currently a work in progress--see the activity at GitHub. https://github.com/mcandre/scriptedmain/commits/master
 
:: My hope is that once everyone understand the purpose of this article, people will contribute more and more examples until a "scripted main" can be written in almost any language, and those for which scripted main is impossible will be documented so that coders don't waste time trying. --[[User:Mcandre|Mcandre]]
 
== Is this something like what you want to show? ==
Line 116 ⟶ 126:
 
==What ''is'' an "Executable library"==
''(This section needs expansion and discussion to see if we have enough to create a task that is capable of being completed by enough languages.)'' --[[User:Paddy3118|Paddy3118]] 07:45, 7 March 2011 (UTC)<br><br>
 
Since my words only carry so far, someone else wrote a short description of this behavior at [http://stackoverflow.com/questions/1973373/why-does-it-do-this-if-name-main/1973391#1973391 Stack Overflow]. --[[User:Mcandre]]
 
When given a task in a programming 'contest' such as to create a simple function and to find values of that function at certain points; then you would be giving your example in the form of an executable library if:
Line 150 ⟶ 162:
 
--[[User:Paddy3118|Paddy3118]] 07:45, 7 March 2011 (UTC)
 
 
: Perfect example Paddy! Is Rosetta Code's main purpose to help coders in programming contests, or are you just framing the article with that mindset for easier reading? --[[User:Mcandre|Mcandre]]
 
:: Hi Mcandre, I was 'framing' with an eye to maybe rewriting the task goals in a manner that was easier for more people to understand. I'm glad that we are on the same wavelength so far on the goals, but as a task description, the above has holes:
::# It needs to specify a task for the program that is importing the library to do. Maybe: ''"Use the libraries hailstone function, in the standard manner, (or document how this use deviates from standard use of a library); to find the hailstone length returned most often for 1 <= n < 100,000"''
::# It needs a solution in another language
::# It would be good, but probably not essential to get a solution in another non-scripting language too.
 
:: '''Here is more to flesh out an initial Python solution:'''
::<lang python>from collections import Counter
from executable_hailstone_library import hailstone
 
def hailstone_length_frequency(hrange):
return Counter(len(hailstone(n)) for n in hrange).most_common()
 
if __name__ == '__main__':
upto = 100000
hlen, freq = hailstone_length_frequency(range(1, upto))[0]
print("The length of hailstone sequence that is most common for\n"
"hailstone(n) where 1<=n<%i, is %i. It occurs %i times."
% (upto, hlen, freq))</lang>
 
::'''Sample output''':
::<code>The length of hailstone sequence that is most common for</code>
::<code>hailstone(n) where 1<=n<100000, is 72. It occurs 1467 times</code>
 
:: '''So, who's for the change!''' --[[User:Paddy3118|Paddy3118]] 08:15, 13 March 2011 (UTC)
 
::: Good stuff. My only concern with hailstone vs meaning of life is that the hailstone sequence may warrant its own article, the duplication may confuse both hailstone geeks and scripting geeks, and the meaning of life snippet is much simpler to read, write, and understand. One caveat I can think of for the meaning of life example is that it fails to demonstrate export of a useful function ("return 42" is not a useful function). The function must be useful enough that the reader understands why it's worthing coding as both a command line script and as an API, yet useless enough that it's not duplicating another article. If you're set on using the hailstone sequence, feel free to borrow code from [https://github.com/mcandre/cspace My Github] --[[User:Mcandre|Mcandre]]
 
:::: Hi Mcandre, the new task [[Executable library]] allows people to copy code from the pre-existing [[Hailstone sequence]] task. Check it out. --[[User:Paddy3118|Paddy3118]] 10:25, 4 April 2011 (UTC)
 
====On fleshing-out a rewrite====
I'll check-back over the next 12 hours and, with enough support of the points I made, move and rewrite the task.
 
(Thanks for the Ruby support)! --[[User:Paddy3118|Paddy3118]] 05:18, 14 March 2011 (UTC)
=====[[Ruby]]=====
 
Yes, I guess that it would be a good idea to change all the examples to hailstone libraries. Here is [[Ruby]] code. Might require Ruby 1.9.
 
<lang ruby># hailstone.rb
module Hailstone
def sequence(n)
seq = [n]
seq << (n = if n.even? then n / 2 else n * 3 + 1 end) until n == 1
seq
end
module_function :sequence
 
if __FILE__ == $0
big_hs = Enumerator.new do |y|
(1...100_000).each { |n| y << sequence(n) }
end.max_by { |hs| hs.size }
puts "#{big_hs[0]} has a hailstone sequence length of #{big_hs.size}."
puts "The largest number in that sequence is #{big_hs.max}."
end
end</lang>
 
<lang ruby># hsfreq.rb
require 'hailstone'
 
module Hailstone
def most_common_length(enum)
h = Hash.new(0)
enum.each { |n| h[sequence(n).length] += 1 }
h.max_by { |length, count| count }
end
module_function :most_common_length
 
if __FILE__ == $0
last = 99_999
length, count = most_common_length(1..last)
puts "Given the hailstone sequences for 1 to #{last},"
puts "the most common sequence length is #{length},"
puts "with #{count} such sequences."
end
end</lang>
 
<pre>$ ruby19 hailstone.rb
77031 has a hailstone sequence length of 351.
The largest number in that sequence is 21933016.
$ ruby19 -I. hsfreq.rb
Given the hailstone sequences for 1 to 99999,
the most common sequence length is 72,
with 1467 such sequences.</pre>
 
--[[User:Kernigh|Kernigh]] 04:16, 14 March 2011 (UTC)
 
== New goals that could possibly be integrated into this ==
 
Some languages (like gambas and visual basic) support two startup modes. Applications written on these platforms start with an open window that waits for events, and it is necessary to do some jiggerypokery to cause a main procedure to run instead. I was going to create a new task to demonstrate how to achieve this, and then I remembered this "scripted main". Maybe this can be reorganized to include these requirements? It is not necessarily a "scripted" main, because visual basic is compiled, so we would probably need a rename, though I have not decided on a name for this yet, I am open to ideas. [[User:Markhobley|Markhobley]] 21:58, 11 July 2011 (UTC)
:FWIW in documentation that I have written, this is usually headed "Making the application startup from a main routine". [[User:Markhobley|Markhobley]] 22:08, 11 July 2011 (UTC)
 
:I would vote to start a new draft task for new topics as this is marked for a slow, lingering, death :-)
:I was wondering however, in your proposed task? Is it about, for example, clicking on a script file in windows to get the script to run versus clicking on the executable to get a [[wp:REPL|REPL]] to run on Windows? --[[User:Paddy3118|Paddy3118]] 06:34, 12 July 2011 (UTC)
::No. The jiggerypokery is done at project build time, not at run time invocation, so that would be a separate task. [[User:Markhobley|Markhobley]] 07:30, 12 July 2011 (UTC)
Anonymous user