How to resolve the algorithm Extend your language step by step in the jq programming language
How to resolve the algorithm Extend your language step by step in the jq programming language
Table of Contents
Problem Statement
Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other languages go much further. Most notably in the Forth and Lisp families, programming per se is done by extending the language without any formal distinction between built-in and user-defined elements. If your language supports it, show how to introduce a new flow control mechanism. A practical and useful example is a four-way branch: Occasionally, code must be written that depends on two conditions, resulting in up to four branches (depending on whether both, only the first, only the second, or none of the conditions are "true"). In a C-like language this could look like the following: Besides being rather cluttered, the statement(s) for 'condition2isTrue' must be written down twice. If 'condition2isTrue' were a lengthy and involved expression, it would be quite unreadable, and the code generated by the compiler might be unnecessarily large. This can be improved by introducing a new keyword if2. It is similar to if, but takes two conditional statements instead of one, and up to three 'else' statements. One proposal (in pseudo-C syntax) might be: Pick the syntax which suits your language. The keywords 'else1' and 'else2' are just examples. The new conditional expression should look, nest and behave analogously to the language's built-in 'if' statement.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Extend your language step by step in the jq programming language
Source code in the jq programming language
def if2($c1; $c2; both; first; second; neither):
if $c1 and $c2 then both
elif $c1 then first
elif $c2 then second
else neither
end;
def if2(c1; c2; both; first; second; neither):
c1 as $c1
| c2 as $c2
| if $c1 and $c2 then both
elif $c1 then first
elif $c2 then second
else neither
end;
You may also check:How to resolve the algorithm Program name step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Even or odd step by step in the GAP programming language
You may also check:How to resolve the algorithm Elliptic curve arithmetic step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the Euphoria programming language