{"id":14966031,"url":"https://github.com/alexdaniel/raku-golf-cheatsheet","last_synced_at":"2025-10-25T13:31:02.953Z","repository":{"id":73423342,"uuid":"115153138","full_name":"AlexDaniel/raku-golf-cheatsheet","owner":"AlexDaniel","description":"Raku tips for code golf","archived":false,"fork":false,"pushed_at":"2018-12-03T22:14:05.000Z","size":38,"stargazers_count":23,"open_issues_count":1,"forks_count":4,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-10-30T01:22:16.585Z","etag":null,"topics":["codegolf","codegolfing","golf","raku"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexDaniel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-22T22:01:30.000Z","updated_at":"2023-03-19T11:00:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"8aafc4ed-d2d0-4e2c-aead-fb6e60cb3b59","html_url":"https://github.com/AlexDaniel/raku-golf-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexDaniel%2Fraku-golf-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexDaniel%2Fraku-golf-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexDaniel%2Fraku-golf-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexDaniel%2Fraku-golf-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexDaniel","download_url":"https://codeload.github.com/AlexDaniel/raku-golf-cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238147581,"owners_count":19424287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegolf","codegolfing","golf","raku"],"created_at":"2024-09-24T13:35:43.763Z","updated_at":"2025-10-25T13:31:02.618Z","avatar_url":"https://github.com/AlexDaniel.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raku/Perl 6 Golfing Cheatsheet\n\nThis repo is a place to share some of the golfing\ntricks with the world. If that document makes golfing less fun for\nyou, please feel free to close your eyes while reading it.\n\n**Please note:** This document is mostly focused on golf platforms that\ncount codepoints instead of UTF-8 bytes. That said, unicode-unrelated\ntricks are welcome too.\n\n**Also:** This document does not list *everything* and probably it\n  never will. Contributions are welcome.\n\n## Unicode\n\n### Unicode numeric literals\nIf one of the numbers that you are using is one of these:\n```\nNaN -0.5 0.00625 0.025 0.0375 0.05 0.0625 0.083333 0.1 0.111111 0.125\n0.142857 0.15 0.166667 0.1875 0.2 0.25 0.333333 0.375 0.4 0.416667 0.5\n0.583333 0.6 0.625 0.666667 0.75 0.8 0.833333 0.875 0.916667 1 1.5 2\n2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 10 11 12 13 14 15 16 17 18\n19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41\n42 43 44 45 46 47 48 49 50 60 70 80 90 100 200 300 400 500 600 700 800\n900 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 20000 30000\n40000 50000 60000 70000 80000 90000 100000 200000 216000 300000 400000\n432000 500000 600000 700000 800000 900000 1000000 100000000\n10000000000 1000000000000\n\n# perl6 -e 'for ^0x10FFFF { .unival.say }' | sort -un\n```\n\nThis can condense large numbers into single characters.\n\n```perl6\nsay ㊿\n#   50\n\nsay 𖭡\n#   1000000000000\n```\n\nThis can also be used to avoid excess whitespace before and after numbers:\n\n```perl6\nsay 2 xx 3\nsay ②xx③\n\nsay 2,4 Z+7,8;\nsay 2,④Z+7,8;\n\nsay $_/2 for ^10;\nsay $_/②for ^10;\n```\n\nAdditionally there's also some numerical constants, like `π` (pi), `τ` (tau) and `𝑒` (Euler's constant).\n\n### Unicode operators\n\nSome unicode characters double as operators, such as `²` (squared), `³` (cubed). A full list can be found at the [perl docs](https://docs.perl6.org/language/unicode_ascii). Some of the more useful ones are the set operators like `∅∈∉∪` etc, the sequence operator `…`, and the multiplication/division alternatives `×÷` to save whitespace.\n\n## Strings\n\n### Uninames\n\nUse uninames, error messages and other built-in strings instead of\nstring literals:\n```perl6\nsay (‘♔’…‘♙’)».uniname».words»[2]\n#   KING QUEEN ROOK BISHOP KNIGHT PAWN\n```\n\n### Newlines\n\nThe shortest way to write a newline is by using an actual newline in the source code:\n```perl6\nsay “X\\nY”;\nsay “X\nY”;\n```\n\n## Lists\n\n### Quote words (\u003c \u003e and « »)\n\nThis allows you to create lists of strings/numbers without using quotes and commas. The elements are separated by whitespace however, so they can't contain whitespace themselves.\n\n```perl6\nsay \u003c1 2 3\u003e\nsay \u003capple orange bannana\u003e\n```\n\nThe « » word quoting operator can be used to interpolate variables:\n```perl6\nmy ($a, $b, $c) = 42, 52, 62;\nsay «25$a$b$c»;\nsay (25,$a,$b,$c);\n```\n\nUnquoted and quoted strings can be mixed:\n```perl6\nsay «a' 'b»;\nsay (\"a\",\" \",\"b\");\n```\n\nOr `{ }` expressions:\n```perl6\nsay «8{^9}0»;\nsay (8,|^9,0);\nsay «abc{\"def\"xx 9}ghi»;\nsay (\"abc\",|(\"def\"xx 9),\"ghi\");\n```\n\n### Creating 2D lists\n\n```perl6\nsay [1,2,3;4,5,6;7,8,9]\n#   [(1 2 3) (4 5 6) (7 8 9)]\n```\n\n### Flattening 2D lists\n\n```perl6\nsay [[1,2,3],[4,5,6]][*;*]   # Can remove whitespace afterwards\nsay [[1,2,3],[4,5,6]].flat\n```\n\n## Loops\n\n### Using `xx`\nThis one is a bit tough to get into your code, but it does help sometimes:\n\n```perl6\nsay($_)for ^5\nsay($++)xx⑤\n```\n\n### Using sequences\n\nThis will produce warnings (Useless use of … in sink context), but it's OK if the golfing platform ignores stderr:\n```perl6\n.say for 1,{$_~0}…10000;\n1,{.say;$_~0}…10000;\n```\n\n### Using »\n\nNote that » is supposed to run stuff in parallel, so the order of execution is not guaranteed. In normal code you should not do ``\u003ca b c\u003e».say`` even though current Rakudo does the processing sequentially. But in code golf that's a nice trick:\n\n```perl6\n.say for \u003ca b c\u003e;\n\u003ca b c\u003e».say;\n```\n\nYou can also chain `»`s for diminishing returns.\n\n```perl6\n.uc.say for \u003ca b c\u003e;\n\u003ca b c\u003e».uc».say;\n```\n\nIf the function is more complicated, you can use `».\u0026{}`:\n\n```perl6\nsay +.comb for \u003caPpLe oraNGe Banana\u003e;\n\u003caPpLe oraNGe Banana\u003e».\u0026{say +.comb};\n```\n\nKeep in mind that `»` loops over for *all* values of multi-dimensional arrays and returns with the same structure, which could be of benefit.\n\n```perl6\nsay [[1,2,3],[4,5],8]».succ\n#   [[2 3 4] [5 6] 9]\n```\n\nThis may depend on the nodality of the function, i.e. if the method takes a list.\n\n```perl6\nsay [[1,2,3],[4,5],8]».elems\n#   [3 2 1]\n```\n\n### Autothreading\nSometimes `Junctions` cause subs to be called multiple times. This can be used to avoid for loops:\n\n```perl6\n.put for \u003ca b c d\u003e;\nput \u003ca b c d\u003e.any;\n```\n\n## Conditionals\n\n### Use junctions if possible\n\n```perl6\nsay 42 if 0||1\n```\n```perl6\nsay 42 if 0|1\n```\n\nNote that you can use junctions as sub args, and you get a junction\nback.\n\nJunctions can also be used to terminate sequences with side effects:\n\n```perl6\n.say for 1…⑽\n```\n```perl6\n1…*.say×*\u003e⑽\n```\n```perl6\n1…{.say}\u0026⑽\n```\n\nIf the block evaluates to a falsey value, a `|` or `^` juncation can be used instead.\n\n\n### 1-character if-then\n\n```perl6\n42==42\u0026\u0026say ‘hello’;\n42==42\u003esay ‘hello’;\n42==42≠say ‘hello’;\n```\n\n**Note:** this also applies to operations with sets (i.e. ``1 ∈ … ∈ .say``).\n\n\n## Variables\n\n\n### Pre-defined variables\n\n`$/` and `$!` are pre-defined variables that don't have to be declared with `my`. They're normally used for regex matches and exceptions but can receive arbitrary values. This is useful to save intermediate values for reuse:\n```perl6\nsay ($!=2*$_), ' ', 2*$! for ^10;\n```\n\nThey can also be used for helper functions:\n```perl6\nsay ($/={$_*100})(2), ' ', $/(3);\n```\n\nThough if you are using the function more than a couple of times, then consider using a defined variable for one byte calls. This has a two byte overhead for `my`, but saves a byte for each call and an extra byte if it is at the end of a statement.\n```perl6\n$/={$_*100};say [$/(2),$/(3),$/(4)]\n# Vs\nmy\u0026f={$_*100};say [f(2),f(3),f 4]\n```\n\nIf you're using `$/` to store a list, then you can use the special `$0,$1,$2 ...` variables to access specific elements without indexing.\n```perl6\n$/=(0,1,*+*...*);\nsay \"$0 $1 $5 $7 $10\";\n#  0 1 5 13 55\n```\n\n### Anonymous state variable\n\n```perl6\nsay ($++, ++$, $-=5, $×=2, $+^=1) for ^5;\n# (0 1  -5  2 1)\n# (1 2 -10  4 0)\n# (2 3 -15  8 1)\n# (3 4 -20 16 0)\n# (4 5 -25 32 1)\n\n# Factorial\nsay $×=++$ for ^10;\n```\n\n### Topic variable (`$_`)\n\nThe `$_` variable is very useful. You can call a method on the `$_` variable without using the variable name, e.g. `.lc`. There are a few ways of doing this, the most useful of which is the `.\u0026{}` operator. The smartmatch operator `~~` is shorter in some circumstances, but doesn't return the value.\n\n```perl6\nsay .uc~.lc with 'aPpLe';\n$_='aPpLe';say .uc~.lc;\nsay 'aPpLe'.\u0026{.uc~.lc};\n'aPpLe'~~say .uc~.lc;\n```\n\n#### Topic variable increment\n```perl6\nsay $_++\n```\n```perl6\nsay .++\n```\n\n#### Topic variable numification\n```perl6\nsay +$_\n```\n```perl6\nsay .¹\n```\n\n#### Topic variable indexing\n\n```perl6\nsay .[0]\n```\n\n## Hyper Operators (« and »)\n\nWe've seen the `»` used for postfixes above, but there are some other uses for it and its counterpart `«`.\n\n### Applying prefixes\n\n```perl6\nsay -«[[0,1,2],[3,4],5]\n#  [[0 -1 -2] [-3 -4] -5]\nsay ^«[[0,1,2],[3,4],5]\n# [[^0 ^1 ^2] [^3 ^4] ^5]\n```\n\n### Zipping lists\n\nOrdinarily you would use the `Z` operator to zip lists, but using `«»` allows you to zip lists of different lengths, with the shorter list cycling.\n```perl6\nsay \u003c1 2 3 4 5\u003e «~»\u003ca b\u003e\n#  (1a 2b 3a 4b 5a)\n```\nThe direction of the hyper operators matter, with the arrows pointing at the shorter list in order to cycle:\n\n```perl6\nsay \u003c1 2 3 4 5\u003e «~«\u003ca b\u003e;\n#  (1a 2b)\n```\n\n## Other\n\n### Base 0x110000\n\nIf the golf platform counts Unicode code points instead of bytes, you can\nstore large numbers in Unicode strings, yielding about 20 bits or 6 decimal\ndigits per code point. There are 0x110000 (decimal 1114112) code points in\ntotal and you can encode a number into a base 0x110000 string like this:\n\n```perl6\nmy $n = :10('1234567890' x 10);\nsay $n.polymod(1114112 xx *).reverse.chrs;\n# Û𜭇󙐧񹂈𚷄򔁖𮸑𬩗򪮒򁻝򞩱􍵮񻗷𫀍𯇆􀰡\n```\n\nThen the string can be decoded with an overhead of 17 characters:\n\n```perl6\nsay :1114112['Û𜭇󙐧񹂈𚷄򔁖𮸑𬩗򪮒򁻝򞩱􍵮񻗷𫀍𯇆􀰡󠫒'.ords]  # 38 chars\n# 12345678901234567890...\n```\n\nIf the resulting string contains invalid code points like surrogates or causes\nproblems because of NFC normalization, simply try a different base.\nYou can even use bases larger than 0x110000 if all \"digits\" happen to stay\nbelow 0x110000. But you often get the same result or even save a character\nwith a 6-digit base like 999999.\n\n### Joined code points\n\nSimilarly you can join the code points instead for a similar compression ratio. Though the compression rate goes down with more zeroes in the number.\n\n```perl6\nmy $n = '1234567890' x 10;\nsay $n.comb(/\\d ** 1..7 \u003c!before 0\u003e \u003c?{$/ \u003c 0x110000}\u003e/).chrs;\n# 𞉀󀨔񔙎󜁲򊩒𞉀󀨔񔙎󜁲򊩒𞉀󀨔񔙎󜁲򊩒𞉀Ồ\n```\n\nFor an overhead of only 12 bytes (-1 if you don't have to numify it).\n```perl6\nsay +[~] \"𞉀󀨔񔙎󜁲򊩒𞉀󀨔񔙎󜁲򊩒𞉀󀨔񔙎󜁲򊩒𞉀Ồ\".ords     # 32 chars\n```\n\nThis has the same problem with accents and surrogate code points as the other method.\n\n### infix .\n```perl6\nsay (^50).max;\nsay ^50 .max;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdaniel%2Fraku-golf-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexdaniel%2Fraku-golf-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdaniel%2Fraku-golf-cheatsheet/lists"}