日付から星座を得る方法: Notion Tips (115)

はじめに

Notion Tips の第115回目は逆引き Notion からネタを引っ張ってきました。元ネタは Red Gregory さんのポストです。

当時は Formula 1.0 の時代なので、() がすごいことになっています。せっかくなので、Formula 2.0 への書き換え方の解説としてネタにさせてもらいます。

元のテンプレート

Red Gregory さんが配布されているテンプレートページはこちらになります。

redgregory.notion.site

ここに Formula も紹介されています。書き出してみます。Formula 1.0 の時代で頑張るとするとこう書くしかなかったという典型的な例だと思います。

if(month(prop("Born")) < 1 and date(prop("Born")) < 21, "♑️ Capricorn", if(month(prop("Born")) < 1 and date(prop("Born")) > 20 or month(prop("Born")) < 2 and date(prop("Born")) < 20, "♒️ Aquarius", if(month(prop("Born")) < 2 and date(prop("Born")) > 19 or month(prop("Born")) < 3 and date(prop("Born")) < 21, "♓️ Pisces", if(month(prop("Born")) < 3 and date(prop("Born")) > 20 or month(prop("Born")) < 4 and date(prop("Born")) < 21, "♈️ Aries", if(month(prop("Born")) < 4 and date(prop("Born")) > 20 or month(prop("Born")) < 5 and date(prop("Born")) < 21, "♉️ Taurus", if(month(prop("Born")) < 5 and date(prop("Born")) > 19 or month(prop("Born")) < 6 and date(prop("Born")) < 22, "♊️ Gemini", if(month(prop("Born")) < 6 and date(prop("Born")) > 21 or month(prop("Born")) < 7 and date(prop("Born")) < 23, "♋️ Cancer", if(month(prop("Born")) < 7 and date(prop("Born")) > 22 or month(prop("Born")) < 8 and date(prop("Born")) < 24, "♌️ Leo", if(month(prop("Born")) < 8 and date(prop("Born")) > 23 or month(prop("Born")) < 9 and date(prop("Born")) < 24, "♍️ Virgo", if(month(prop("Born")) < 9 and date(prop("Born")) > 23 or month(prop("Born")) < 10 and date(prop("Born")) < 24, "♎️ Libra", if(month(prop("Born")) < 10 and date(prop("Born")) > 23 or month(prop("Born")) < 11 and date(prop("Born")) < 23, "♏️ Scorpio", if(month(prop("Born")) < 11 and date(prop("Born")) > 22 or month(prop("Born")) < 12 and date(prop("Born")) < 22, "♐️ Sagittarius", if(month(prop("Born")) < 12 and date(prop("Born")) > 21, "♑️ Capricorn", "")))))))))))))

このまま貼り付けたところ、一つずつ星座がずれていました。実は Formula 2.0 に変わった時に、0-11 だった month の返り値が 1-12 に変更になったためでした。数値を変えるのはミスりそうだったので、 <<= に変更して修正しました。

if(month(prop("Born")) <= 1 and date(prop("Born")) < 21, "♑️ Capricorn", if(month(prop("Born")) <= 1 and date(prop("Born")) > 20 or month(prop("Born")) <= 2 and date(prop("Born")) < 20, "♒️ Aquarius", if(month(prop("Born")) <= 2 and date(prop("Born")) > 19 or month(prop("Born")) <= 3 and date(prop("Born")) < 21, "♓️ Pisces", if(month(prop("Born")) <= 3 and date(prop("Born")) > 20 or month(prop("Born")) <= 4 and date(prop("Born")) < 21, "♈️ Aries", if(month(prop("Born")) <= 4 and date(prop("Born")) > 20 or month(prop("Born")) <= 5 and date(prop("Born")) < 21, "♉️ Taurus", if(month(prop("Born")) <= 5 and date(prop("Born")) > 19 or month(prop("Born")) <= 6 and date(prop("Born")) < 22, "♊️ Gemini", if(month(prop("Born")) <= 6 and date(prop("Born")) > 21 or month(prop("Born")) <= 7 and date(prop("Born")) < 23, "♋️ Cancer", if(month(prop("Born")) <= 7 and date(prop("Born")) > 22 or month(prop("Born")) <= 8 and date(prop("Born")) < 24, "♌️ Leo", if(month(prop("Born")) <= 8 and date(prop("Born")) > 23 or month(prop("Born")) <= 9 and date(prop("Born")) < 24, "♍️ Virgo", if(month(prop("Born")) <= 9 and date(prop("Born")) > 23 or month(prop("Born")) <= 10 and date(prop("Born")) < 24, "♎️ Libra", if(month(prop("Born")) <= 10 and date(prop("Born")) > 23 or month(prop("Born")) <= 11 and date(prop("Born")) < 23, "♏️ Scorpio", if(month(prop("Born")) <= 11 and date(prop("Born")) > 22 or month(prop("Born")) <= 12 and date(prop("Born")) < 22, "♐️ Sagittarius", if(month(prop("Born")) <= 12 and date(prop("Born")) > 21, "♑️ Capricorn", "")))))))))))))

Formula 2.0 対応にするために

これを Formula 2.0 で簡単化するには以下のテクニックを使います。

  • let を使って月日をユニークな数値として変数に保存
  • 作成した数値を ifs で複数判定

作成した Formula は以下の通りです。明かに読みやすいですね。

let(mmdd, prop("Born").month() * 100 + prop("Born").date(),
    ifs(
        mmdd < 121, "♑️ Capricorn",
        mmdd < 220, "♒️ Aquarius",
        mmdd < 321, "♓️ Pisces",
        mmdd < 421, "♈️ Aries",
        mmdd < 521, "♉️ Taurus",
        mmdd < 622, "♊️ Gemini",
        mmdd < 723, "♋️ Cancer",
        mmdd < 824, "♌️ Leo",
        mmdd < 924, "♍️ Virgo",
        mmdd < 1024, "♎️ Libra",
        mmdd < 1123, "♏️ Scorpio",
        mmdd < 1222, "♐️ Sagittarius",
        mmdd < 1232, "♑️ Capricorn",
        ""
    )
)

Formula 1.0 と 2.0 で同じ出力になっていることを確認しました。

Zodiac

おわりに

今回は Formula 1.0 の時代に作られた「日付から星座を得る Formula」を Formula 2.0 で再現してみました。Formula 変数では let や lets による一時変数を定義できるようになったため、非常に簡潔に書けるようになりました。

hkob.notion.site