Abbreviations, simple: Difference between revisions

→‎{{header|REXX}}: ooRexx conformance and readabie
(→‎{{header|REXX}}: ooRexx conformance and readabie)
 
(3 intermediate revisions by 3 users not shown)
Line 2,769:
'' ->
''</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
''' Adapted from [[#Wren|Wren]]
<syntaxhighlight lang=jq>
def table:
"add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3 " +
"compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate " +
"3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2 " +
"forward 2 get help 1 hexType 4 input 1 powerInput 3 join 1 split 2 spltJOIN load " +
"locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2 macro merge 2 modify 3 move 2 " +
"msg next 1 overlay 1 parse preserve 4 purge 3 put putD query 1 quit read recover 3 " +
"refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left " +
"2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1"
;
 
# Input: {commands, minLens}
# Output: array of expansions or error markers corresponding to $words
def validate($words):
.commands as $commands
| .minLens as $minLens
| [ $words[] as $word
| ($word|length) as $wlen
| first( range(0; $commands|length) as $i
| $commands[$i]
| select($minLens[$i] != 0 and $wlen >= $minLens[$i] and $wlen <= length)
| ascii_upcase
| select(startswith(($word|ascii_upcase))) )
// "*error*" ];
 
 
# Output: {commands, minLens} corresponding to the $table string
def commands($table):
[$table|splits(" *")] as $split_table
| ($split_table|length) as $slen
| {commands:[], minLens:[], i:0}
| until(.found;
.commands += [ $split_table[.i] ]
| ($split_table[.i]|length) as $len
| if (.i == $slen - 1)
then .minLens += [$len]
| .found = true
else .
end
| .i += 1
| ($split_table[.i] | try (tonumber) // null) as $num
| if ($num != null)
then .minLens += [ if ($num < $len) then $num else $len end ]
| .i += 1
| if (.i == $slen) then .found = true else . end
else .minLens += [$len]
end );
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def task($sentence):
[$sentence | splits(" *")] as $words
| commands(table)
| validate($words)
| $words, .
| map(lpad(10))
| join(" ") ;
 
task("riG rePEAT copies put mo rest types fup. 6 poweRin")
</syntaxhighlight>
{{output}}
'''Invocation''' jq -rnf abbreviations-simple.jq
</pre>
riG rePEAT copies put mo rest types fup. 6 poweRin
RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT
</pre>
 
=={{header|Julia}}==
Line 3,764 ⟶ 3,837:
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program validates a user "word" against a "command table" with abbreviations.*/
parseParse argArg uw /*obtain optional arguments from the CL*/
ifIf uw='' thenThen uw= 'riG rePEAT copies put mo rest types fup. 6 poweRin'
saySay 'user words: ' uw
 
@keyws= 'add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3',
'compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate',
'3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2',
'forward 2 get help 1 hexType 4 input 1 powerInput 3 join 1 split 2 spltJOIN load',
'locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2 macro merge 2 modify 3 move 2',
'msg next 1 overlay 1 parseParse preserve 4 purge 3 put putD query 1 quit read recover 3',
'refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left',
'2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1'
 
saySay 'full words: ' validate(uw) /*display the result(s) to the terminal*/
exitExit /*stick a fork in it, we're all done. */
/*--------------------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
validate: Procedure Expose keyws
validate: procedure expose @; arg x; upper @ /*ARG capitalizes all the X words. */
keyws=translate(keyws)
$= /*initialize the return string to null.*/
Arg userwords do j=1 to words(x); _=word(x, j) /*obtain a word from the X list./*ARG capitalizes all the userwords. */
res='' do k=1 to words(@); a=word(@, k) /*get a legitmate command name from @ /*initialize the Return string to null.*/
Do j=1 to words(userwords) L=word(@, k+1) /* loop over userwords /*··· and maybe get it's abbrev length.*/
uword=word(userwords,j) if datatype(L, 'W') then k=k + 1/*obtain a word from the userword /*yuppers, it's an abbrev lengthlist.*/
Do k=1 to words(keyws) /* loop over keywords else L=length(a) /*nope, it can't be abbreviated.*/
kw=word(keyws,k) if abbrev(a, _, L) then do; $=$ a; iterate/*get j;a legitmate endcommand name /*is validfrom abbrev?keyws.*/
L=word(keyws,k+1) end /*k··· and maybe get its abbrev length.*/
If $=$ datatype(L,'*error*W') Then /* it's a number - /*processedan theabbrev wholelength. list, not valid. */
k=k + 1 end /*j skip it for next kw */
Else return strip($) /*elide theotherwise superfluous leading blank. */</syntaxhighlight>
L=length(kw) /* it can't be abbreviated. */
If abbrev(kw,uword,L) Then Do /* is valid abbreviation */
res=res kw $= /* add to result string /*initialize the return string to null.*/
Iterate j /* proceed with next userword */
End
End
res=res '*error*' /*processed the whole list, not valid */
End
Return strip(res) /* elide superfluous leading blank. */
</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 4,281 ⟶ 4,364:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import encoding.utf8
import strconv
fn read_table(table string) ([]string, []int) {
Line 4,369 ⟶ 4,452:
{{libheader|Wren-str}}
Based on an older version of the Go entry.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./str" for Str
 
var table =
2,289

edits