Inverted syntax: Difference between revisions

m
 
(3 intermediate revisions by 3 users not shown)
Line 452:
The following is how is visualized the common IF expression:
 
[[File:Fōrmulæ - Inverted syntax 01.png|border]]
 
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|border]]
 
'''Inverted syntax with assignment'''
Line 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,240 ⟶ 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,444 ⟶ 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