Anonymous user
Talk:Menu: Difference between revisions
→J implementation
(some J tutorial text) |
|||
(9 intermediate revisions by 6 users not shown) | |||
Line 10:
==J implementation==
===Code removed===
Here are some introductory notes for people not familiar with J, who might care about the definition.▼
because it didn't work in Beta-3: commercial/2017-04-10T17:51:14 and was flagged for improvement.
<lang j>require 'general/misc/prompt' NB. in older versions of J this was: require'misc'
showMenu =: i.@# smoutput@,&":&> ' '&,&.>
makeMsg =: 'Choose a number 0..' , ': ',~ ":@<:@#
errorMsg =: [ smoutput bind 'Please choose a valid number!'
select=: ({::~ _&".@prompt@(makeMsg [ showMenu)) :: ($:@errorMsg)</lang>
See [[Talk:Select#J_implementation|Talk page]] for explanation of code.
'''Example use:'''
<lang j> select 'fee fie'; 'huff and puff'; 'mirror mirror'; 'tick tock'</lang>
This would display:
0 fee fie
1 huff and puff
2 mirror mirror
3 tick tock
choose a number 0..3:
And, if the user responded with 2, would return: <tt>mirror mirror</tt>
--LambertDW 17:59, 8 September 2018 (UTC)
Note: this description of the code is based on an older version (the line below) and is now out of sync with the copy on the main page.
▲Here are some introductory notes for people not familiar with J, who might care about the
<lang J>select=: ({::~ 'choose a number 0..' 0&".@prompt@, ': ',~ ":@<:@# [ i.@# smoutput@,&":&> ' '&,&.>) :: (select@([ smoutput bind 'please choose a valid number'))</lang>
Line 20 ⟶ 48:
And, as written, it expects the user is displaying the code in a short, wide window (or that they can resize their window that way).
===
The first part of the code to be executed is:
Line 104 ⟶ 132:
Here, we have printed each of the composed strings. A subtle distinction here is that they will be displayed before evaluation of the rest of the sentence.
===List indexing===
In all my years, I have never seen:
You have two choices:
0 go
1 don't go
Everybody (except "C" people) are used to choices starting with 1, not zero. Like a book, real people expect
the first page to be 1 (one). Ask anyone for a top ten list, and you won't see 0 --> 9.
[[User:Gerard Schildberger|Gerard Schildberger]] 17:38, 21 January 2011 (UTC)
:But given the topic of this site, it is ''very'' common to have to consider lists with a minimum index of zero as well as one. --[[User:Paddy3118|Paddy3118]] 21:22, 21 January 2011 (UTC)
===prompt user===
Line 113 ⟶ 156:
":@<:@#
<code>#</code> counts the number of elements in its argument
<: is a two element token which when used with one argument subtracts one from its argument. I could have instead used (-&1) but that's more than twice as long.
Line 178 ⟶ 221:
Finally, hypothetically speaking, both this writeup and the original code might be changed. For example, perhaps, instead of a single long line, a user would be more comfortable with a series of short definitions of (throwaway) words. [In my experience, this sort of thing can be a handy learning tool. But -- unless particularly relevant words are used -- it also introduces distracting complexities, instead of illuminating anything.]
:I'd prefer something like the following as being more readily parseable:
<lang j>
load 'misc'
displayMenu =: i.@# smoutput@,&":&> ' '&,&.>
makeMsg =: 'Choose a number 0..' , ': ',~ ":@<:@#
displayBadNumMsg =: [ smoutput bind 'Please choose a valid number!'
select=: ({::~ _&".@prompt@(makeMsg [ displayMenu)) :: ($:@displayBadNumMsg)
</lang>
:--[[User:Tikkanz|Tikkanz]] 02:46, 15 October 2009 (UTC)
::I have adopted this version, with some minor name changes (and sticking with require instead of load) --[[User:Rdm|Rdm]] 12:43, 26 January 2010 (UTC)
|