Inverted syntax: Difference between revisions

m
 
(6 intermediate revisions by 5 users not shown)
Line 447:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Inverted_syntax}}
 
'''Inverted syntax with conditional expressions'''
 
The following is how is visualized the common IF expression:
 
[[File:Fōrmulæ - Inverted syntax 01.png]]
 
The following is the visualization of the inverted IF expression. Notice that they are different expressions, in order that they can be visualized in different form, but they reduce the same way.
 
[[File:Fōrmulæ - Inverted syntax 02.png]]
 
'''Inverted syntax with assignment'''
 
The assignment expression does not have an inverted form.
 
=={{header|Go}}==
Line 592 ⟶ 606:
 
@inv println("Wow! Lucky Guess!") if true else println("Not!") end</syntaxhighlight>
 
=={{header|Koka}}==
Using the byref function and infix operators we can get both inverted assignments as well as inverted conditionals in Koka! However, the branch to the conditional is evaluated prior to the condition unless you explicitly delay it or accept a closure.
 
<syntaxhighlight lang="koka">
fun (=:)(c: c, v: local-var<h, c>): <local<h>> ()
v := c
 
fun (?)(c: c, b: bool): e maybe<c>
if b then Just(c) else Nothing
 
fun main()
var x := 4
6 =: std/core/types/byref(x)
x.println
val res = "Yes" ? True
match res
Just(a) -> println(a)
Nothing -> throw("Nothing")
</syntaxhighlight>
 
{{out}}
<pre>
6
Yes
</pre>
 
=={{header|Kotlin}}==
Line 1,064 ⟶ 1,104:
<syntaxhighlight lang="python">with open("file.txt") as f:
something(f)</syntaxhighlight>
 
=={{header|Quackery}}==
 
A Quackery program consists of numbers, words, and nests, collectively referred to as items. A nest is a sequence of zero or more items. Items are evaluated sequentially.
 
Some words can alter this sequential behaviour. <code>if</code> for example, causes the item following it to be skipped if the number on the top of data stack (henceforth just "the stack") is zero (i.e. false.)
 
So, given that the word <code>raining</code> returns a boolean (i.e. 0 or 1) indicating whether it is raining or not, and the word <code>use-umbrella</code> deploys an umbrella, the usual syntax would be <code>raining if use-umbrella</code>.
 
The word <code>'</code> alters the sequential flow by causing the item following it to be placed on the stack rather than being evaluated. The word <code>do</code> causes the item on the top of the stack to be evaluated. So we could define a new word, <code>if.1</code> with
 
<pre>[ swap do if do ] is if.1</pre>
 
which would allow us to use the syntax <code>' raining ' use-umbrella if.1</code>.
 
<code>'</code> is defined using the meta-control flow operator <code>]'[</code> thus: <code>[ ]'[ ] is '</code> Meta-control flow operators grant their behaviour to the word that calls them. So if we wanted the syntax <code>' use-umbrella if.2 raining</code> we could define <code>if.2</code> with
 
<pre>[ ]'[ do iff do else drop ] is if.2</pre>
 
(<code>iff</code> conditionally skips over two items, <code>else</code> unconditionally skips over one item.)
 
Other syntaxes can be achieved by the judicious use of <code>'</code> and <code>]'[</code>. To demonstrate just one that avoids the use of <code>if ...</code> or <code>iff ... else ...</code> entirely, one could define a word <code>ifelse</code> as
 
<pre>[ not ]'[ nested ]'[ nested join swap peek do ] is ifelse</code></pre>
 
This would have the syntax <code>raining ifelse use-umbrella use-sunscreen</code> (for an appropriate definition of <code>use-sunscreen</code>.)
 
Quackery does not have variables, so there is not exact equivalent to assignment, but it does have ancillary stacks which can be used in a variable-like manner. <code>temp</code> is a predefined ancillary stack, and an item can be moved from the stack to it with the phrase <code>temp put</code>. As with the previous examples, <code>]'[</code> can be used to vary the syntax.
 
=={{header|Qi}}==
Line 1,123 ⟶ 1,135:
A -> (set needumbrella A))
</syntaxhighlight>
 
=={{header|Quackery}}==
 
A Quackery program consists of numbers, words, and nests, collectively referred to as items. A nest is a sequence of zero or more items. Items are evaluated sequentially.
 
Some words can alter this sequential behaviour. <code>if</code> for example, causes the item following it to be skipped if the number on the top of data stack (henceforth just "the stack") is zero (i.e. false.)
 
So, given that the word <code>raining</code> returns a boolean (i.e. 0 or 1) indicating whether it is raining or not, and the word <code>use-umbrella</code> deploys an umbrella, the usual syntax would be <code>raining if use-umbrella</code>.
 
The word <code>'</code> alters the sequential flow by causing the item following it to be placed on the stack rather than being evaluated. The word <code>do</code> causes the item on the top of the stack to be evaluated. So we could define a new word, <code>if.1</code> with
 
<pre>[ swap do iff do else drop ] is if.1</pre>
 
which would allow us to use the syntax <code>' raining ' use-umbrella if.1</code>. (<code>iff</code> conditionally skips over two items, <code>else</code> unconditionally skips over one item.)
 
<code>'</code> is defined using the meta-control flow operator <code>]'[</code> thus: <code>[ ]'[ ] is '</code> Meta-control flow operators grant their behaviour to the word that calls them. So if we wanted the syntax <code>' use-umbrella if.2 raining</code> we could define <code>if.2</code> with
 
<pre>[ ]'[ do iff do else drop ] is if.2</pre>
 
Other syntaxes can be achieved by the judicious use of <code>'</code> and <code>]'[</code>. To demonstrate just one that avoids the use of <code>if ...</code> or <code>iff ... else ...</code> entirely, one could define a word <code>ifelse</code> as
 
<pre>[ not ]'[ nested ]'[ nested join swap peek do ] is ifelse</code></pre>
 
This would have the syntax <code>raining ifelse use-umbrella use-sunscreen</code> (for an appropriate definition of <code>use-sunscreen</code>.)
 
Quackery does not have variables, so there is not exact equivalent to assignment, but it does have ancillary stacks which can be used in a variable-like manner. <code>temp</code> is a predefined ancillary stack, and an item can be moved from the stack to it with the phrase <code>temp put</code>. As with the previous examples, <code>]'[</code> can be used to vary the syntax.
 
=={{header|R}}==
Line 1,228 ⟶ 1,266:
zz=444 / (7-a)
</pre>
 
=={{header|RPL}}==
Assigning values to variables using <code>→</code> or <code>STO</code> commands presents an inverted syntax in RPL. It's also possible to invert syntax for conditional expressions, by pushing the action in the stack as a lambda expression before doing the test, but this isn't idiomatic.
≪ 1 → raining <span style="color:grey">@ Set raining to true</span>
≪ ≪ NEEDUMBRELLA ≫
'''IF''' raining '''THEN''' EVAL '''ELSE''' DROP '''END''' <span style="color:grey">@ Execute above action if true, else pop it from stack</span>
≫ ≫ '<span style="color:blue">TASK</span>' STO <span style="color:grey">@ Store above program in TASK variable</span>
 
=={{header|Ruby}}==
Line 1,432 ⟶ 1,477:
 
Again, simulating inverted syntax with assignment is not possible.
<syntaxhighlight lang="ecmascriptwren">class IBool {
construct new(b) {
if (!(b is Bool)) Fiber.abort("B must be a boolean")
2,120

edits