Posts

Showing posts with the label raku

Implementation of a function object “power” operator in Raku

20 2 In APL there is the power operator ⍣ , which if applied to a function f superimposes the application of f . How to implement that operator in Raku? For example, with this definition of f : sub f(Int:D $i){ $i + 1 } the command say (f ⍣ 4)(10); should be equivalent to say f(f(f(f(10)))); . My implementation below is for a function with one argument. Questions How one should extend or replace it with a better implementation that works on multiple (or any) signatures? How to define "high precedence" of that new power operator? Is there are better way to define the "identity function" result for f ⍣ 0 ? Reference links Here is a description of APL's ⍣ : "Power Operator". ( ⍣ is a "star with two dots", or mo...

How to loop on sorted (with custom sort) keys of a hash in Raku?

10 Trying to progressively convert some Perl scripts to Raku. I am quite stuck with the following one, even after browsing quite a lot here and reading Learning Perl 6 more deeply. The part on which I can't make progress is the last loop (converted to for ); getting keys and sorting them by month name and day number looks impossible, but I am sure it is doable. Any hints on how to achieve this with "idiomatic" syntax would be really welcome. #!/usr/bin/perl use strict; my %totals; while (<>) { if (/redis/ and /Partial/) { my($f1, $f2) = split(' '); my $w = $f1 . ' ' . $f2; $totals{$w}++; } } my %m = ("jan" => 1, "feb" => 2, "mar" => 3, "apr" =>...

Is there a convenient way to replicate R's concept of 'named vectors' in Raku, possibly using Mixins?

8 Recent questions on StackOverflow pertaining to Mixins in Raku have piqued my interest as to whether Mixins can be applied to replicate features present in other programming languages. For example, in the R-programming language, elements of a vector can be given a name (i.e. an attribute), which is very convenient for data analysis. For an excellent example see: "How to Name the Values in Your Vectors in R" by Andrie de Vries and Joris Meys, who illustrate this feature using R 's built-in islands dataset. Below is a more prosaic example (code run in the R-REPL): > #R-code > x <- 1:4 > names(x) <- LETTERS[1:4] > str(x) Named int [1:4] 1 2 3 4 - attr(*, "names")= chr [1:4] "A" "B" "C" "D...

Is there a straightforward way to check if something is a mixin?

5 Raku mixins have two (or more) natures, combining several values in the same container, or values along with roles. However, there is not, as far as I can tell, a straightforward way to check for "mixinity" in a variable that has not been created by you. This might be a trick my $foo = 3 but Stringy; say $foo.^name ~~ /\+/;# OUTPUT: «「+」␤» But is there any other property I'm missing that would allow to look this up directly? mixins raku Share Improve this question ...

What persistent data structures does Raku/Rakudo include?

14 1 Raku provides many types that are immutable and thus cannot be modified after they are created. Until I started looking into this area recently, my understanding was that these Types were not persistent data structures – that is, unlike the core types in Clojure or Haskell, my belief was that Raku's immutable types did not take advantage of structural sharing to allow for inexpensive copies. I thought that statement my List $new = (|$old-list, 42); literally copied the values in $old-list , without the data-sharing features of persistent data structures. That description of my understanding is in the past tense, however, due to the following code: my Array $a = do { $_ = [rand xx 10_000_000]; say "Initialized an Array in $((now - ENTER now).r...