C-style ternary operator, by Softanza!

81 views
Skip to first unread message

Mansour Ayouni

unread,
Sep 9, 2023, 5:26:59 AM9/9/23
to The Ring Programming Language
Hello,

Last time, I showed you how Softanza enables you to embrace the compact ternary operator style of writing conditionals, in Python. A reminder:

Now, I show how the C syntax of the same feature is also supported:

In C language, the ternary operator conforms to the following syntax:
variable = (condition) ? value1 : value2;


So, let's take a snippet from the web, say:

n = -12;
sign = (n > 0) ? "positive" : "negative";
printf(sign);
#--> negative


And then, to write it in Ring, we just need to decorate the same code with vr(), b() and bv() small functions:
n = -12;
vr(:sign) '=' b(n > 0) '?' bv("positive", "negative");
printf( v(:sign) );
#--> negative


Here is the sample running in my Ring Notepad...


image.png

By the way, the same syntax is supported by many other languages like Javascript, C++, C#, Java, Javascript, PHP, to cite a few.

All the best,
Mansour

Ilir Liburn

unread,
Sep 9, 2023, 6:34:52 AM9/9/23
to The Ring Programming Language
Hello Mansour,

what small functions do?

vr - variable
b - binary
bv - ?

The reason why ternary operator is not implemented in Ring as in many other languages is because ? is used for print.
One possible solution is to use lambda syntax:

sign = (n>0) -> "positive" : "negative"

Greetings,
Ilir

Mansour Ayouni

unread,
Sep 9, 2023, 7:40:03 AM9/9/23
to Ilir Liburn, The Ring Programming Language
Hello Ilir,

This is how those small functions work:

image.png

Step 1

vr() adds one or more named variables into a temporary global list. For each variable, the name and value are stored (actually in a hash list).

Step 2

b() function takes the result of the expression (true or false) and stores it in a temporary global boolean

Step 3

bv() assigns the value of the named variable we just added (:sign in our case) depending on the value of the global boolean

Step 4

v() helps us to read the value of the :sign variable, and any other named variable stored in the temporary hash list.

By the way, there are many other ways we can use vr() to cope with several syntax structures from other external languages, like for example, when used in conjunction with the vl() small function (vl --> values) to assign multiple values to many variables at once:

vr( :name, :age, :job ) '=' vl([ "Mansour", 47, "Programmer"])
? v(:age) #--> 47

And so, I'm getting old :)

PS: Thank you for the lambda suggestion! Does Ring support it?

Best,
Mansour


--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/14671bb6-a527-4cad-85ab-a47e53470774n%40googlegroups.com.

Ilir Liburn

unread,
Sep 9, 2023, 8:20:16 AM9/9/23
to The Ring Programming Language
Hello Mansour,

No, Ring doesn't support lambda syntax yet. That's my suggestion because


sign = (n>0) ? "positive" : "negative"

is parsed in Ring as assign boolean value to sign, print "positive"+nl and then comes empty literal and string (no error thrown).
By using arrow or any other specific symbol, parser can take another route to get desired result.

OK, so small functions are:

vr - makes a variable
b - stores boolean value in global boolean
bv - assigns value to variable matching global boolean

Greetings,
Ilir

Mansour Ayouni

unread,
Sep 9, 2023, 9:36:19 AM9/9/23
to Ilir Liburn, The Ring Programming Language
Hello Ilir,

Yes! And vl() to assign many values to many variables in one statement.

Best,
Mansour

Bert Mariani

unread,
Sep 11, 2023, 3:18:44 PM9/11/23
to The Ring Programming Language
Hello Mansour

Raku  uses ...  as a Ternary Operator (current renamed  Perl )
    ??   !!    
It doubled up on the ?  and  !  because they were taken.
Same as in Ring we doubled up on the  ^^  for the ExpLog operator  because was taken bit the Bit Operator
You should do the same. It will make it easy to read.

Examples in Raku...
say rand < 0.5 ?? 'Yes'  !!  'No'

my $hours = 20;
my $day-time =
    $hours <= 6  ?? 'Night' !!
    $hours <= 12 ?? 'Morning' !!
    $hours <= 18 ?? 'Afternoon' !! 'Evening';
say $day-time;

Mansour Ayouni

unread,
Sep 11, 2023, 3:30:47 PM9/11/23
to Bert Mariani, The Ring Programming Language
Hello Bert,

Nice suggestion. And yes, this is possible, since I use them as strings with no impact at all on the instruction. And so we can write:

n = -12;
vr(:sign) '=' b(n > 0) '??' bv("positive""negative");
printf( v(:sign);
#--> negative
 
Thank you for pointing to Raku! I am reading a book about the bottom-to-top programming paradigm in Lisp: http://www.paulgraham.com/onlisp.html

I'll definitely have a look at Raku and see how it may inspire me.

Best,
Mansour

Mansour Ayouni

unread,
Sep 13, 2023, 7:01:52 PM9/13/23
to Bert Mariani, The Ring Programming Language
Hello Bert,

A small gift to you : Softanza talking in Raku...

image.png

Best,
Mansour

Mansour Ayouni

unread,
Sep 13, 2023, 7:17:23 PM9/13/23
to Bert Mariani, The Ring Programming Language
Hello Bert,

And here is a more accurate alternative...

image.png

Best,
Mansour

Bert Mariani

unread,
Sep 16, 2023, 6:35:56 PM9/16/23
to The Ring Programming Language
Hello Mansour

Softanza  can be flexible as you have shown.

Looking at these two statements,
The first one is easier to write, read,  and understand.
The effort should be made to add these two operators  ??  !!   in Ring as a ternary operator (compare if else)  

1 -- say rand < 0.5 ?? 'Yes' !! 'No';
2 -- say { b(rand < 0.5) '??' bt('Yes') 'bf('No) };


Ilir Liburn

unread,
Sep 16, 2023, 6:58:06 PM9/16/23
to The Ring Programming Language
Hello Bert,

excellent syntax ( ?? - !! ), very readable. Whatever comes next to your mind, do not hesitate to share.

Greetings,
Ilir

Mansour Ayouni

unread,
Sep 16, 2023, 7:10:53 PM9/16/23
to Bert Mariani, The Ring Programming Language
Hello Bert,

Thank you for your kind words.

And yes of course, native syntax is always better and more performant!

Best,
Mansour

Reply all
Reply to author
Forward
0 new messages