Content deleted Content added
m Correct capitalization |
m Now in Pygments, "raku" is an alias of "perl6" |
||
Line 108:
===A type system===
In Raku, the [[Dynamic type|dynamic type system]] of Perl has been augmented by the addition of [[static type]]s.<ref name="syn2">{{cite web | url=https://design.raku.org/S02.html | title=Synopsis 2: Bits and Pieces | author=Wall, Larry | date=2009-05-20 }}</ref> For example:
<syntaxhighlight lang="
my Int $i = 0;
my Rat $r = 3.142;
Line 114:
</syntaxhighlight>
However, static typing remains [[Type system#Combining static and dynamic type checking|optional]], so programmers can do most things without any explicit typing at all:
<syntaxhighlight lang="
my $i = "25" + 10; # $i is 35
</syntaxhighlight>
Line 124:
Raku introduces true formal parameters to the language.<ref name="syn6">{{cite web | url=https://design.raku.org/S06.html | title=Synopsis 6: Subroutines | author=Wall, Larry | date=2003-03-21 }}</ref> In Raku, a subroutine declaration looks like this:
<syntaxhighlight lang="
sub do_something(Str $thing, Int $other) {
...
Line 138:
Here is an example of the use of all three parameter-passing modes:
<syntaxhighlight lang="
sub somefunction($a, $b, :$c, :$d, *@e) {
...
Line 150:
====Blocks and closures====
Parameters can also be passed to arbitrary blocks, which act as [[Closure (computer science)|closures]]. This is how, for example, <code>for</code> and <code>while</code> loop iterators are named. In the following example, a list is traversed, 3 elements at a time, and passed to the loop's block as the variables, <code>$a, $b, $c</code>.<ref name="syn04">{{cite web | url=https://design.raku.org/S04.html | title=Synopsis 4: Blocks and Statements | author=Wall, Larry | date=2009-05-20 }}</ref>
<syntaxhighlight lang="
for @list -> $a, $b, $c {
...
Line 190:
This complexity has no equivalent either in common use of natural language or in other programming languages,{{dubious|date=October 2015}} and it causes high [[cognitive load]] when writing code to manipulate complex data structures. This is the same code in Raku:
<syntaxhighlight lang="
# Raku code: retrieve a list from the leaf of a hash containing hashes that contain arrays
my @trans_verbs = %dictionary<verb><transitive><>;
Line 201:
In the spirit of making the "easy things easy and hard things possible", Raku retains the blessing model and supplies a more robust object model for the common cases.<ref>{{cite web | url=https://design.raku.org/S12.html | title=Synopsis 12: Objects | author=Wall, Larry | date=2006-08-18 }}</ref> For example, a class to encapsulate a [[Cartesian coordinate system|Cartesian]] [[point (geometry)|point]] could be defined and used this way:
<syntaxhighlight lang="
class Point is rw {
has $.x;
Line 247:
For example, a [[Dog]] is a [[Mammal]] because dogs inherit certain characteristics from Mammals, such as [[mammary gland]]s and (through Mammal's parent, [[Vertebrate]]) a [[Vertebral column|backbone]]. On the other hand, dogs also may have one of several distinct types of behavior, and these behaviours may change over time. For example, a Dog may be a [[Pet]], a [[feral|Stray]] (an abandoned pet will acquire behaviours to survive not associated with a pet), or a [[guide dog|Guide]] for the blind (guide dogs are trained, so they do not start life as guide dogs). However, these are sets of additional behaviors that can be added to a Dog. It is also possible to describe these behaviors in such a way that they can be usefully applied to other animals, for example, a [[Cat]] can equally be a Pet or Stray. Hence, Dog and Cat are distinct from each other, while both remain within the more general category Mammal. So Mammal is a Class and Dog and Cat are classes that inherit from Mammal. But the behaviours associated with Pet, Stray, and Guide are Roles that can be added to Classes, or objects instantiated from Classes.
<syntaxhighlight lang="
class Mammal is Vertebrate {
...
Line 265:
</syntaxhighlight>
Roles are added to a class or object with the <code>does</code> keyword. In order to show inheritance from a class, there is a different keyword <code>is</code>. The keywords reflect the differing meanings of the two features: role composition gives a class the ''behavior'' of the role, but doesn't indicate that it is truly the ''same thing'' as the role.
<syntaxhighlight lang="
class GuideDog is Dog does Guide {
...
Line 274:
</syntaxhighlight>
Although roles are distinct from classes, both are types, so a role can appear in a variable declaration where one would normally put a class. For example, a Blind role for a Human could include an attribute of type Guide; this attribute could contain a Guide Dog, a [[guide horse|Guide Horse]], a Guide Human, or even a Guide Machine.
<syntaxhighlight lang="
class Human {
has Dog $dog; # Can contain any kind of Dog, whether it does the
Line 295:
===Syntactic simplification===<!-- This section is linked from [[Ellipsis]] -->
Some Perl constructs have been changed in Raku, optimized for different syntactic cues for the most common cases. For example, the parentheses (round [[bracket]]s) required in [[control flow]] constructs in Perl are now optional:<ref name="syn04"/>
<syntaxhighlight lang="
if is-true() {
for @array {
Line 303:
</syntaxhighlight>
Also, the <code>,</code> (comma) operator is now a list constructor, so enclosing parentheses are no longer required around lists. The code
<syntaxhighlight lang="
@array = 1, 2, 3, 4;
</syntaxhighlight>
Line 310:
===Chained comparisons===
Raku allows comparisons to "chain". That is, a sequence of comparisons such as the following is allowed:
<syntaxhighlight lang="
if 20 <= $temperature <= 25 {
say "Room temperature is between 20 and 25!"
Line 319:
===Lazy evaluation===
Raku uses the technique of [[lazy evaluation]] of lists that has been a feature of some [[functional programming]] languages such as [[Haskell (programming language)|Haskell]]:<ref name="syn9">{{cite web | url=https://design.raku.org/S09.html | title=Synopsis 9: Data Structures | author=Wall, Larry | date=2004-09-13 }}</ref>
<syntaxhighlight lang="
@integers = 0..Inf; # integers from 0 to infinity
</syntaxhighlight>
Line 328:
===Gather===
Related to lazy evaluation is the construction of lazy lists using <code>gather</code> and <code>take</code>, behaving somewhat like generators in languages like [[Icon (programming language)|Icon]] or [[Python (programming language)|Python]].
<syntaxhighlight lang="
my $squares = lazy gather for 0..Inf {
take $_ * $_;
Line 337:
===Junctions===
Raku introduces the concept of ''junctions'': values that are composites of other values.<ref name="syn9"/> In their simplest form, junctions are created by combining a set of values with junctive [[Operator (programming)|operator]]s:
<syntaxhighlight lang="
# Example for | ("any") Junction:
my $color = 'white';
Line 353:
Junctions can also be used to more richly augment the type system by introducing a style of [[generic programming]] that is constrained to junctions of types:
<syntaxhighlight lang="
subset Color of Any where RGB_Color | CMYK_Color;
sub get_tint(Color $color, Num $opacity) {
Line 365:
A Raku macro definition will look like a subroutine or method definition, and it can operate on unparsed strings, an [[Abstract syntax tree|AST]] representing pre-parsed code, or a combination of the two. A macro definition would look like this:<ref name="macros">{{cite web |url=https://design.raku.org/S06.html#Macros |title=Macros}}</ref>
<syntaxhighlight lang="
macro hello($what) {
quasi { say "Hello { {{{$what}}} }" };
Line 379:
===Hello world===
The [[hello world program]] is a common program used to introduce a language. In Raku, hello world is:
<syntaxhighlight lang="
say 'Hello, world';
</syntaxhighlight>
Line 386:
===Factorial===
The [[factorial]] function in Raku, defined in a few different ways:
<syntaxhighlight lang="
# Using recursion (with `if\else` construct)
sub fact( UInt $n --> UInt ) {
Line 435:
===Quicksort===
[[Quicksort]] is a well-known sorting algorithm. A working implementation{{efn|Unless the implementation does something fancy and mysterious behind the scenes, the maximal possible recursion depth is the length of the list, making this implementation unsuitable for big data. Recursion depth can be limited to <code>log2(list_length)</code> by iterating into the larger of partitions <code>before</code> and <code>after</code>, and only recursing into the smaller partition.}} using the functional programming paradigm can be succinctly written in Raku:
<syntaxhighlight lang="
# Empty list sorts to the empty list
multi quicksort([]) { () }
Line 454:
===Tower of Hanoi===
[[Tower of Hanoi]] is often used to introduce recursive programming in computer science. This implementation uses Raku's multi-dispatch mechanism and parametric constraints:
<syntaxhighlight lang="
multi sub hanoi(0, $, $, $) { } # No disk, so do not do anything
multi sub hanoi($n, $a = 'A', $b = 'B', $c = 'C') { # Start with $n disks and three pegs A, B, C
|