Define a primitive data type: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: don't pretend the closure is the most general form)
Line 855: Line 855:
# dies, too small. string is 0 interpreted numerically</lang>
# dies, too small. string is 0 interpreted numerically</lang>
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo Star|2010-08}}


<lang perl6>subset OneToTen of Int where { 1 <= $^n <= 10 }
<lang perl6>subset OneToTen of Int where 1..1


my OneToTen $n = 5;
my OneToTen $n = 5;
$n += 6;</lang>
$n += 6;</lang>


<code>{ 1 <= $^n <= 10 }(11)</code> returns a false value, so the second assignment throws an exception. You can use any unary function in place of <code>{ 1 <= $^n <= 10 }</code>, so this, for example, is also legal:
Here the result 11 fails to smartmatch against the range <code>1..10</code>, so the second assignment throws an exception. You may use any valid smartmatch predicate within a <code>where</code> clause, so the following one-argument closure is also legal:


<lang perl6>subset Prime of Int where { $^n > 1 and ?($^n %% none 2 .. ($^n.sqrt)) }</lang>
<lang perl6>subset Prime of Int where -> $n { $n > 1 and so $n %% none 2 .. $n.sqrt }</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==