Perl6中的Sub vs Blocks-又名罗马数字挑战

时间:2019-05-30 17:42:41

标签: perl6

Perl Weekly Challenge Number 10要求我们编写一个解析罗马数字的实现。

我的解决方案使用reduce。我对Subroutine的尝试有效,但对Block却不起作用。导致失败的区别是什么?

use Test;
my %roman2num =
    'I'  => 1,
    'V'  => 5,
    'X'  => 10,
    'L'  => 50,
    'C'  => 100,
    'D'  => 500,
    'M'  => 1000;

for test-data().kv -> $roman, $number {
    my $r;
    $r = $roman.comb().reverse.map({%roman2num{$_}}).reduce(
        sub ($sum, $b) {
            state $last = $sum;
            LEAVE { $last = $b };
            return $sum + (($last <= $b)*2-1) * $b;
        }
    );
    is $r, $number, "Roman $roman $r- way 1";

    $r = $roman.comb().reverse.map({%roman2num{$_}}).reduce({
        state $last = $^sum;
        $^b;
        LEAVE { $last = $b };
        $sum + (($last <= $b)*2-1) * $b;
    });
    is $r, $number, "Roman $roman $r- way 2";
}

done-testing;

sub test-data {
    return (
I => 1,
II => 2,
III => 3,
IV => 4,
V => 5,
VI => 6,
VII => 7,
VIII => 8,
IX => 9,
X => 10,
XI => 11,
XII => 12,
XIII => 13,
XIV => 14,
XV => 15,
XVI => 16,
XVII => 17,
XVIII => 18,
XIX => 19,
XX => 20,
XXI => 21,
XXII => 22,
XXIII => 23,
XXIV => 24,
XXV => 25,
XXVI => 26,
XXVII => 27,
XXVIII => 28,
XXIX => 29,
XXX => 30,
XXXI => 31,
XXXII => 32,
XXXIII => 33,
XXXIV => 34,
XXXV => 35,
XXXVI => 36,
XXXVII => 37,
XXXVIII => 38,
XXXIX => 39,
XL => 40,
XLI => 41,
XLII => 42,
XLIII => 43,
XLIV => 44,
XLV => 45,
XLVI => 46,
XLVII => 47,
XLVIII => 48,
XLIX => 49,
L => 50,
LI => 51,
LII => 52,
LIII => 53,
LIV => 54,
LV => 55,
LVI => 56,
LVII => 57,
LVIII => 58,
LIX => 59,
LX => 60,
LXI => 61,
LXII => 62,
LXIII => 63,
LXIV => 64,
LXV => 65,
LXVI => 66,
LXVII => 67,
LXVIII => 68,
LXIX => 69,
LXX => 70,
LXXI => 71,
LXXII => 72,
LXXIII => 73,
LXXIV => 74,
LXXV => 75,
LXXVI => 76,
LXXVII => 77,
LXXVIII => 78,
LXXIX => 79,
LXXX => 80,
LXXXI => 81,
LXXXII => 82,
LXXXIII => 83,
LXXXIV => 84,
LXXXV => 85,
LXXXVI => 86,
LXXXVII => 87,
LXXXVIII => 88,
LXXXIX => 89,
XC => 90,
XCI => 91,
XCII => 92,
XCIII => 93,
XCIV => 94,
XCV => 95,
XCVI => 96,
XCVII => 97,
XCVIII => 98,
XCIX => 99,
C => 100,
CI => 101,
CII => 102,
CIII => 103,
CIV => 104,
CV => 105,
CVI => 106,
CVII => 107,
CVIII => 108,
CIX => 109,
CX => 110,
CXI => 111,
CXII => 112,
CXIII => 113,
CXIV => 114,
CXV => 115,
CXVI => 116,
CXVII => 117,
CXVIII => 118,
CXIX => 119,
CXX => 120,
CXXI => 121,
CXXII => 122,
CXXIII => 123,
CXXIV => 124,
CXXV => 125,
CXXVI => 126,
CXXVII => 127,
CXXVIII => 128,
CXXIX => 129,
CXXX => 130,
CXXXI => 131,
CXXXII => 132,
CXXXIII => 133,
CXXXIV => 134,
CXXXV => 135,
CXXXVI => 136,
CXXXVII => 137,
CXXXVIII => 138,
CXXXIX => 139,
CXL => 140,
CXLI => 141,
CXLII => 142,
CXLIII => 143,
CXLIV => 144,
CXLV => 145,
CXLVI => 146,
CXLVII => 147,
CXLVIII => 148,
CXLIX => 149,
CL => 150,
CLI => 151,
CLII => 152,
CLIII => 153,
CLIV => 154,
CLV => 155,
CLVI => 156,
CLVII => 157,
CLVIII => 158,
CLIX => 159,
CLX => 160,
CLXI => 161,
CLXII => 162,
CLXIII => 163,
CLXIV => 164,
CLXV => 165,
CLXVI => 166,
CLXVII => 167,
CLXVIII => 168,
CLXIX => 169,
CLXX => 170,
CLXXI => 171,
CLXXII => 172,
CLXXIII => 173,
CLXXIV => 174,
CLXXV => 175,
CLXXVI => 176,
CLXXVII => 177,
CLXXVIII => 178,
CLXXIX => 179,
CLXXX => 180,
CLXXXI => 181,
CLXXXII => 182,
CLXXXIII => 183,
CLXXXIV => 184,
CLXXXV => 185,
CLXXXVI => 186,
CLXXXVII => 187,
CLXXXVIII => 188,
CLXXXIX => 189,
CXC => 190,
CXCI => 191,
CXCII => 192,
CXCIII => 193,
CXCIV => 194,
CXCV => 195,
CXCVI => 196,
CXCVII => 197,
CXCVIII => 198,
CXCIX => 199,
CC => 200,
CCI => 201,
CCII => 202,
CCIII => 203,
CCIV => 204,
CCV => 205,
CCVI => 206,
CCVII => 207,
CCVIII => 208,
CCIX => 209,
CCX => 210,
CCXI => 211,
CCXII => 212,
CCXIII => 213,
CCXIV => 214,
CCXV => 215,
CCXVI => 216,
CCXVII => 217,
CCXVIII => 218,
CCXIX => 219,
CCXX => 220,
CCXXI => 221,
CCXXII => 222,
CCXXIII => 223,
CCXXIV => 224,
CCXXV => 225,
CCXXVI => 226,
CCXXVII => 227,
CCXXVIII => 228,
CCXXIX => 229,
CCXXX => 230,
CCXXXI => 231,
CCXXXII => 232,
CCXXXIII => 233,
CCXXXIV => 234,
CCXXXV => 235,
CCXXXVI => 236,
CCXXXVII => 237,
CCXXXVIII => 238,
CCXXXIX => 239,
CCXL => 240,
CCXLI => 241,
CCXLII => 242,
CCXLIII => 243,
CCXLIV => 244,
CCXLV => 245,
CCXLVI => 246,
CCXLVII => 247,
CCXLVIII => 248,
CCXLIX => 249,
CCL => 250,
CCLI => 251,
CCLII => 252,
CCLIII => 253,
CCLIV => 254,
CCLV => 255,
CCLVI => 256,
CCLVII => 257,
CCLVIII => 258,
CCLIX => 259,
CCLX => 260,
CCLXI => 261,
CCLXII => 262,
CCLXIII => 263,
CCLXIV => 264,
CCLXV => 265,
CCLXVI => 266,
CCLXVII => 267,
CCLXVIII => 268,
CCLXIX => 269,
CCLXX => 270,
CCLXXI => 271,
CCLXXII => 272,
CCLXXIII => 273,
CCLXXIV => 274,
CCLXXV => 275,
CCLXXVI => 276,
CCLXXVII => 277,
CCLXXVIII => 278,
CCLXXIX => 279,
CCLXXX => 280,
CCLXXXI => 281,
CCLXXXII => 282,
CCLXXXIII => 283,
CCLXXXIV => 284,
CCLXXXV => 285,
CCLXXXVI => 286,
CCLXXXVII => 287,
CCLXXXVIII => 288,
CCLXXXIX => 289,
CCXC => 290,
CCXCI => 291,
CCXCII => 292,
CCXCIII => 293,
CCXCIV => 294,
CCXCV => 295,
CCXCVI => 296,
CCXCVII => 297,
CCXCVIII => 298,
CCXCIX => 299,
CCC => 300,
CCCI => 301,
CCCII => 302,
CCCIII => 303,
CCCIV => 304,
CCCV => 305,
CCCVI => 306,
CCCVII => 307,
CCCVIII => 308,
CCCIX => 309,
CCCX => 310,
CCCXI => 311,
CCCXII => 312,
CCCXIII => 313,
CCCXIV => 314,
CCCXV => 315,
CCCXVI => 316,
CCCXVII => 317,
CCCXVIII => 318,
CCCXIX => 319,
CCCXX => 320,
CCCXXI => 321,
CCCXXII => 322,
CCCXXIII => 323,
CCCXXIV => 324,
CCCXXV => 325,
CCCXXVI => 326,
CCCXXVII => 327,
CCCXXVIII => 328,
CCCXXIX => 329,
CCCXXX => 330,
CCCXXXI => 331,
CCCXXXII => 332,
CCCXXXIII => 333,
CCCXXXIV => 334,
CCCXXXV => 335,
CCCXXXVI => 336,
CCCXXXVII => 337,
CCCXXXVIII => 338,
CCCXXXIX => 339,
CCCXL => 340,
CCCXLI => 341,
CCCXLII => 342,
CCCXLIII => 343,
CCCXLIV => 344,
CCCXLV => 345,
CCCXLVI => 346,
CCCXLVII => 347,
CCCXLVIII => 348,
CCCXLIX => 349,
CCCL => 350,
CCCLI => 351,
CCCLII => 352,
CCCLIII => 353,
CCCLIV => 354,
CCCLV => 355,
CCCLVI => 356,
CCCLVII => 357,
CCCLVIII => 358,
CCCLIX => 359,
CCCLX => 360,
CCCLXI => 361,
CCCLXII => 362,
CCCLXIII => 363,
CCCLXIV => 364,
CCCLXV => 365,
CCCLXVI => 366,
CCCLXVII => 367,
CCCLXVIII => 368,
CCCLXIX => 369,
CCCLXX => 370,
CCCLXXI => 371,
CCCLXXII => 372,
CCCLXXIII => 373,
CCCLXXIV => 374,
CCCLXXV => 375,
CCCLXXVI => 376,
CCCLXXVII => 377,
CCCLXXVIII => 378,
CCCLXXIX => 379,
CCCLXXX => 380,
CCCLXXXI => 381,
CCCLXXXII => 382,
CCCLXXXIII => 383,
CCCLXXXIV => 384,
CCCLXXXV => 385,
CCCLXXXVI => 386,
CCCLXXXVII => 387,
CCCLXXXVIII => 388,
CCCLXXXIX => 389,
CCCXC => 390,
CCCXCI => 391,
CCCXCII => 392,
CCCXCIII => 393,
CCCXCIV => 394,
CCCXCV => 395,
CCCXCVI => 396,
CCCXCVII => 397,
CCCXCVIII => 398,
CCCXCIX => 399,
CD => 400,
CDI => 401,
CDII => 402,
CDIII => 403,
CDIV => 404,
CDV => 405,
CDVI => 406,
CDVII => 407,
CDVIII => 408,
CDIX => 409,
CDX => 410,
CDXI => 411,
CDXII => 412,
CDXIII => 413,
CDXIV => 414,
CDXV => 415,
CDXVI => 416,
CDXVII => 417,
CDXVIII => 418,
CDXIX => 419,
CDXX => 420,
CDXXI => 421,
CDXXII => 422,
CDXXIII => 423,
CDXXIV => 424,
CDXXV => 425,
CDXXVI => 426,
CDXXVII => 427,
CDXXVIII => 428,
CDXXIX => 429,
CDXXX => 430,
CDXXXI => 431,
CDXXXII => 432,
CDXXXIII => 433,
CDXXXIV => 434,
CDXXXV => 435,
CDXXXVI => 436,
CDXXXVII => 437,
CDXXXVIII => 438,
CDXXXIX => 439,
CDXL => 440,
CDXLI => 441,
CDXLII => 442,
CDXLIII => 443,
CDXLIV => 444,
CDXLV => 445,
CDXLVI => 446,
CDXLVII => 447,
CDXLVIII => 448,
CDXLIX => 449,
CDL => 450,
CDLI => 451,
CDLII => 452,
CDLIII => 453,
CDLIV => 454,
CDLV => 455,
CDLVI => 456,
CDLVII => 457,
CDLVIII => 458,
CDLIX => 459,
CDLX => 460,
CDLXI => 461,
CDLXII => 462,
CDLXIII => 463,
CDLXIV => 464,
CDLXV => 465,
CDLXVI => 466,
CDLXVII => 467,
CDLXVIII => 468,
CDLXIX => 469,
CDLXX => 470,
CDLXXI => 471,
CDLXXII => 472,
CDLXXIII => 473,
CDLXXIV => 474,
CDLXXV => 475,
CDLXXVI => 476,
CDLXXVII => 477,
CDLXXVIII => 478,
CDLXXIX => 479,
CDLXXX => 480,
CDLXXXI => 481,
CDLXXXII => 482,
CDLXXXIII => 483,
CDLXXXIV => 484,
CDLXXXV => 485,
CDLXXXVI => 486,
CDLXXXVII => 487,
CDLXXXVIII => 488,
CDLXXXIX => 489,
CDXC => 490,
CDXCI => 491,
CDXCII => 492,
CDXCIII => 493,
CDXCIV => 494,
CDXCV => 495,
CDXCVI => 496,
CDXCVII => 497,
CDXCVIII => 498,
CDXCIX => 499,
).Hash
}

1 个答案:

答案 0 :(得分:9)

占位符变量按Unicode顺序^ twigil 排序。重命名第二个变量:

    dataDF.write
      .partitionBy("update_database_time")
      .format("avro")
      .option(
        "avroSchema",
        SchemaRegistry.getSchema(
          schemaRegistryConfig.url,
          schemaRegistryConfig.dataSchemaSubject,
          schemaRegistryConfig.dataSchemaVersion))
  .save(s"${hdfsURL}${pathToSave}")

或在块签名中定义变量:

    $r = $roman.comb().reverse.map({%roman2num{$_}}).reduce({
        state $last = $^sum;
        $^x;
       LEAVE { $last = $x };
       $sum + (($last <= $x)*2-1) * $x;
    });
    is $r, $number, "Roman $roman $r- way 2";