Execute a Markov algorithm: Difference between revisions

(→‎{{header|jq}}: simplify)
Line 2,839:
end );
 
# applyRules applies $rules to . recursively,
# where $rules is the set of applicable rules in the form of an array-of-triples.
# Input and output: a string
def chainapplyRules( $rules ):
# $rules should be the array-of-triples form of the applicable rules
# The inner function has arity-0 for efficiency
def chain( $rules ):
{stop# input and output: false{stop, string: .}
def apply:
| until(.stop;
if .stringstop asthen $copy.
|else .redostring =as false$copy
| first( |foreach label$rules[] as $outc (.;
| foreach .string |= sub($rulesc[0], null) as; $c (.;[2])
| if $c[1] == null then "."
else .string |= sub($c[0]; $c[2]) then .stop=true
| if $c[1] == " elif ."string != $copy
then (apply | .,stop break= $outtrue)
elif .string != $copy else .
then .redo = true end;
| if .,stop then . else breakempty $outend))
else// .
end;
{stop: false, string: .} | apply end| .string;
if .redo or $c[1] == "." or $c == null then . else empty end)
| if .redo then . else .stop = true end)
| .string;
 
def proceed:
Line 2,867 ⟶ 2,866:
| range(0; $tests|length) as $ix
| $tests[$ix]
| " \(.)\n=>\(chainapplyRules( parseRules( $rules[$ix] ) ))\n" ;
 
proceed</lang>
2,442

edits