Jump to content

Topic variable: Difference between revisions

New post.
m (→‎{{header|Wren}}: Changed to Wren S/H)
(New post.)
Line 289:
 
And if we were to insist on leaving out functions, it's not clear that there would be much of anything left of the language to be doing anything with. See also Henry Rich's writeup on [http://www.jsoftware.com/docs/help701/jforc/tacit_programs.htm Tacit Programs].
 
=={{header|Java}}==
Java allows the use of topic variables starting from version 22.
<syntaxhighlight lang="java">
import java.awt.Point;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
 
public final class TopicVariables {
 
public static void main(String[] args) {
List<Integer> numbers = List.of( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
// Example 1:
// We only want to count the number of elements in a list,
// so a topic variable can be used for the identify of each element of the list.
// An artificial example since we could just use 'total = numbers.size()'.
int total = 0;
for ( Integer _ : numbers ) { // Topic variable
total += 1;
}
System.out.println("The list contains " + total + " numbers");
// Example 2:
// A topic variable is suitable for the unused identifier of the NumberFormatException.
String text = "123456:@~";
int i = 0;
try {
i = Integer.parseInt(text);
} catch(NumberFormatException _) { // Topic variable
System.out.println("Variable i is incorrectly defined with value: " + i);
}
// Example 3:
// We only require the first two out of every three items in a queue.
// A topic variable can be used for the third unused item.
Queue<Integer> queue = new ArrayDeque<Integer>(numbers);
while ( queue.size() >= 3 ) {
int x = queue.poll();
int y = queue.poll();
int _ = queue.poll(); // Topic variable
System.out.println( new Point(x, y) );
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
The list contains 9 numbers
Variable i is incorrectly defined with value: 0
java.awt.Point[x=1,y=2]
java.awt.Point[x=4,y=5]
java.awt.Point[x=7,y=8]
</pre>
 
=={{header|jq}}==
908

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.