LoginSignup
0

More than 1 year has passed since last update.

phigasui という文字列を作りたかっただけ Julia 編

Last updated at Posted at 2021-12-25

私のアカウント名は @phigasui です。

成果物は下記の通りです。

julia> ((@doc(+).content |> join)[Int(c)] for c="\x1f;\x18S\"8@\x18") |> join
"phigasui"

docstring に頼る

まずは何らかの文字列があればそこから p, h, i, g, a, s, u を取り出せれば良いなと思いました。

Julia で文字列ってどこにあったかなーと思って思い出したのが docstring

julia> @doc +
  +(x, y...)

  Addition operator. x+y+z+... calls this function with all arguments, i.e. +(x, y, z, ...).

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> 1 + 20 + 4
  25

  julia> +(1, 20, 4)
  25

  dt::Date + t::Time -> DateTime

  The addition of a Date with a Time produces a DateTime. The hour, minute, second, and millisecond parts of the Time are used along with the year, month, and day of the Date to create the new DateTime. Non-zero microseconds or nanoseconds in the Time type will
  result in an InexactError being thrown.

@doc マクロを使うと docstring が得られます。

julia> @doc(+) |> typeof
Markdown.MD

Markdown.MD という Type です。

mutable struct MD
    content::Vector{Any}
    meta::Dict{Symbol, Any}

    MD(content::AbstractVector, meta::Dict = Dict()) =
        new(content, meta)
end

content は docstring の文字列の Vector なのでこれを使います。

これを使って phigasui の各文字の index をとってきて文字列にすれば良さそうです。

julia> (findfirst(x->x==c, @doc(+).content |> join) for c="phigasui") .|> Char |>join
"\x1f;\x18S\"8@\x18"

無事、str docstring から phigasui を得るための文字列を得ました。
完成 :tada:

julia> ((@doc(+).content |> join)[Int(c)] for c="\x1f;\x18S\"8@\x18") |> join
"phigasui"

ちなみに Julia では Char の Range も使えるのでこんな感じでも

julia> (findfirst(x->x==c, ['a':'z'...] |> join) for c="phigasui") .|> Char |>join
"\x10\b\t\a\x01\x13\x15\t"
julia> (join(['a':'z'...])[Int(c)] for c="\x10\b\t\a\x01\x13\x15\t") |> join
"phigasui"

もっとおもしろいのあったらぜひ教えてください :pray:

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0