Posts in this category

Fri, 08 Oct 2010

This Week's Contribution to Perl 6 Week 10: Implement samespace for Rakudo

Permanent link

For this week's contribution to Perl 6 we ask you to implement the Str.samespace method for Rakudo. This method takes a whitespace pattern from one string, and transfers it to a second string. It is used for smart substitutions.

(Introduction to this series of challenges)


The Perl 6 specification includes a variant of the substitution operator that preserves the spaces from the source string. This is activated either the :ss/:samespace adverb, or by using ss/// instead of s///.

What you can do

Implement a samespace method that can act as the backend for such an operation.

Since recompiling Rakudo takes a lot of time, you'll have a much easier time developing the method in a separate file, and simply run it with Rakudo. You can use this template for prototyping:

use v6;
use Test;
augment class Str {
    method samespace($other as Str) {
        # replace this by your implementation
        uc self;

plan *;

is "a b c d".samespace("x y\n\tz"), "a b\n\tc d";

The Specification has a few more details on the topic. (Ignore the part about the whitespace having to be matched by a <ws> rule). Your implementation doesn't have to fulfill the spec totally; a naive implementation of a small part is still much better than none at all.

Please also add a few more tests.

For extra karma you can make your submission a real patch (in which case the method goes into src/core/ More extra karma and eternal fame if you wire it up with the :ss and :samespace adverbs in the subst method (same file).


Please submit your source code to the mailing list (and put on CC, because the mailing list sometimes lack quite a bit).

Update: There have been two submissions so far, challenge closed.

If you have any questions, feel free to send them to the mailing list, or ask on our IRC channel".

[/perl-6] Permanent link