小编hyt*_*thm的帖子

原始令牌候选者排序

perl6如何确定proto token首先匹配哪个?

下面的代码按预期方式工作,它与string匹配1234,并Grammar::Tracer显示与之匹配的第一个标记是s:sym<d>,这是有意义的,因为它是最长的标记。

但是,如果我将文字更改为令牌,例如,将token threeform 更改'3'<digit>,它将无法匹配,并Grammar::Tracer显示s:sym<b>与first匹配。

移动s:sym<d>到顶部,在两种情况下都匹配字符串,但是对该行为的解释是什么?

#!/usr/bin/env perl6
no precompilation;
use Grammar::Tracer;

grammar G {

  token TOP { <s> }

  proto token s { * }

  token s:sym<a> { <one> }
  token s:sym<b> { <one> <two> }
  token s:sym<c> { <one> <two> <three> }
  token s:sym<d> { <one> <two> <three> <four> }

  token one   { '1' }
  token two …
Run Code Online (Sandbox Code Playgroud)

regex grammar perl6 raku

13
推荐指数
1
解决办法
99
查看次数

Grammar to parse module specification

Raku modules can be specified in different ways, for example:

MyModule

MyModule:ver<1.0.3>

MyModule:ver<1.0.3>:auth<Name (email@example.com)>;

MyModule:ver<1.0.3>:auth<Name <email@example.com>>;

I wrote the below grammar to parse the module spec which works fine for most of specs but it fails if the auth field contains < or >. How can I fix the grammar to match in this case as well?

I can't figure out how to say match everything in between < and > including any < and > as well.

#!/usr/bin/env …
Run Code Online (Sandbox Code Playgroud)

perl6 raku

9
推荐指数
1
解决办法
84
查看次数

对象之间的通讯

如果我拥有同时具有Player对象和Board对象的Game Class,Game会询问Player坐标是什么,Player会做出响应,然后游戏会检查Board的坐标以及结果Hit或Miss。

游戏如何将结果转发回玩家?以便Player使用结果来设置新坐标。

我在下面创建了代码示例,以解释更多我想做的事情

以及此处的项目链接:https : //github.com/hythm7/Battleship/tree/master


#!/usr/bin/env perl6

enum Result < Miss Hit >;

class Player {

  method fire ( ) {
    (^10).pick, (^10).pick
  }

}

class Board {
  has @.cell = [ +Bool.pick xx ^10 ] xx ^10;
}

class Game {
  has Board  $.board  = Board.new;
  has Player $!player = Player.new;

  method run ( ) {

    loop {

      my ($x, $y) = $!player.fire;

      if $!board.cell[$y][$x] {
        say Hit;
      }
      else {
        say Miss;
      }
      # How …
Run Code Online (Sandbox Code Playgroud)

perl6 raku

8
推荐指数
1
解决办法
157
查看次数

递归子程序

假设我有一个包a,b,c,...的包,每个包都有名称,版本和依赖关系.

在下面的代码中,get-cand sub获取一个包并从池中返回候选者(以及它们的依赖性递归).

所以,如果它采用名为c的包,并且池有

包c1:名称c,版本1,dep a(任何版本)和b(版本1)

包c2:名称c,版本2,dep b(版本2)

它将返回以下数据结构:

((c1 ((((a1 ()) (a2 ()))) (((b1 ()))))) (c2 ((((b2 ()))))))  # can't figure out how to get rid of empty lists here
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写将采用上述数据结构的select-cand子目标,目标是返回第一个不冲突的候选者(使用已安装的包),并递归检查它的依赖性.

所以sub应该像这样工作:

  • 检查c1,如果冲突返回False,则取c1并下降到它的deps
  • (现在在c1 deps中)检查a1,如果冲突返回True,请检查下一个候选人(a2)

最终它应该返回[c1 a2 b1]或[c2 b2]以便成功运行,如果所有候选人都发生冲突,则不返回任何内容.

但目前select-cand子返回:[c1 a1 a2 c2 b2]这是错误的,因为我只需要c1(和递归deps)或c2.

(冲突是一个占位符子,目前只是排除b1包用于测试目的)

#!/usr/bin/env perl6

my …
Run Code Online (Sandbox Code Playgroud)

rakudo perl6 raku

7
推荐指数
0
解决办法
160
查看次数

语法和Unicode字符

为什么下面的代码Grammar无法解析unicode字符?

从中删除单词边界后解析很好<sym>

#!/usr/bin/env perl6

grammar G {


  proto rule TOP { * }

  rule TOP:sym<y>  { «<.sym>» }
  rule TOP:sym<?>  { «<.sym>» }

}

say G.parse('y'); # ?y?
say G.parse('?'); # Nil
Run Code Online (Sandbox Code Playgroud)

perl6 raku

7
推荐指数
2
解决办法
113
查看次数

对象数组

假设我要连接到两个程序包存储库,查询程序包名称,合并存储库中的结果并对其进行处理(过滤器,唯一,优先级...),那么有什么好的方法呢?

不过,我要做的是创建Array两个Cro::HTTP::Client对象(base-uri特定于每个存储库),当我需要发出HTTP请求时,我将调用 @a>>.get,然后一起处理存储库的结果。

我已附上我要执行的操作的摘要。但我想看看是否有更好的方法可以做到这一点。或者如果以下链接中提到的方法适合该用例!https://perl6advent.wordpress.com/2013/12/08/day-08-array-based-objects/

use Cro::HTTP::Client;

class Repo {

  has $.name;
  has Cro::HTTP::Client $!client;
  has Cro::Uri $.uri;
  has Bool $.disable = False;

  submethod TWEAK () {
    $!client = Cro::HTTP::Client.new(base-uri => $!uri, :json);
  }

  method get (:$package) {

    my $path = <x86_64?>;
    my $resp = await $!client.get($path ~ $package);
    my $json = await $resp.body;
    return $json;
  }
}


class AllRepos {

  has Repo @.repo;

  method get (:$package) {

    # check if some repos …
Run Code Online (Sandbox Code Playgroud)

perl6 raku

6
推荐指数
1
解决办法
140
查看次数

分布.dist-id

编辑:更新,因为我是混淆的问题.dist-id.id;

我正在尝试卸载dist,但是当我将传递Distribution.uninstall它时,它似乎计算出了不同.dist-idDistribution并且没有被卸载。

我究竟做错了什么?

#!/usr/bin/env perl6

my $dist = $*REPO.candidates( 'Grid' ).head;
say $dist.WHAT, $dist.Str, $dist.dist-id;

say $*REPO.uninstall( $dist );
say $*REPO.uninstall( $dist ); # still there

# OUTPUT:
(LazyDistribution)Grid:ver<0.0.2>:auth<>:api<0>ADCD0F3EFF816F1212D5513F201B8143E2BDD6FF
["/home/hythm/.perl6/dist/157B7E1F2BB827C99A5EF3241D61853A85E57734".IO] # shouldn't this hash be the one above ? ?
["/home/hythm/.perl6/dist/157B7E1F2BB827C99A5EF3241D61853A85E57734".IO]
Run Code Online (Sandbox Code Playgroud)

perl6 raku

6
推荐指数
1
解决办法
118
查看次数

如何在Postgres中根据外键选择数据?

我有一个电影 Postgres 数据库,我想有效地列出演员播放的所有电影,并列出电影中的所有演员。

目前我正在使用这两个 SELECT 语句(我认为有更好的方法来做到这一点):

SELECT title FROM movies,castings WHERE movies.id = castings.movieid and castings.actorid  = (SELECT id FROM actors where name = $name and sname = $sname);

SELECT name, sname FROM actors,castings WHERE actors.id = castings.actorid and castings.movieid  = (SELECT id FROM movies where title = $title);
Run Code Online (Sandbox Code Playgroud)

数据库:

CREATE SEQUENCE directors_id_seq;
CREATE TABLE directors  (
    id BIGINT PRIMARY KEY DEFAULT nextval('directors_id_seq'),
    name TEXT,
    sname TEXT
);

CREATE SEQUENCE actors_id_seq;
CREATE TABLE actors (
    id BIGINT PRIMARY KEY DEFAULT …
Run Code Online (Sandbox Code Playgroud)

postgresql

1
推荐指数
1
解决办法
4801
查看次数

标签 统计

perl6 ×7

raku ×7

grammar ×1

postgresql ×1

rakudo ×1

regex ×1