uribo / zipangu

Japanese utility functions and data
https://uribo.github.io/zipangu
Other
56 stars 7 forks source link

Vectorize jholiday-related functions #17

Closed yutannihilation closed 4 years ago

yutannihilation commented 4 years ago

Related: #15

以下の関数に複数のyearを渡せるようになります。

使用例

library(zipangu)

find_date_by_wday(2011:2020, 1, 2, 3)
#>  [1] "2011-01-17" "2012-01-16" "2013-01-21" "2014-01-20" "2015-01-19"
#>  [6] "2016-01-18" "2017-01-16" "2018-01-15" "2019-01-21" "2020-01-20"

jholiday_spec(2011:2020, "Vernal Equinox Day")
#>  [1] "2011-03-21" "2012-03-20" "2013-03-20" "2014-03-21" "2015-03-21"
#>  [6] "2016-03-20" "2017-03-20" "2018-03-21" "2019-03-21" "2020-03-20"

jholiday(2011:2020)
#> $`New Year's Day`
#>  [1] "2011-01-01" "2012-01-01" "2013-01-01" "2014-01-01" "2015-01-01"
#>  [6] "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01"
#> 
#> $`Coming of Age Day`
#>  [1] "2011-01-10" "2012-01-09" "2013-01-14" "2014-01-13" "2015-01-12"
#>  [6] "2016-01-11" "2017-01-09" "2018-01-08" "2019-01-14" "2020-01-13"
#> 
#> $`Foundation Day`
#>  [1] "2011-02-11" "2012-02-11" "2013-02-11" "2014-02-11" "2015-02-11"
#>  [6] "2016-02-11" "2017-02-11" "2018-02-11" "2019-02-11" "2020-02-11"
#> 
#> $`Vernal Equinox Day`
#>  [1] "2011-03-21" "2012-03-20" "2013-03-20" "2014-03-21" "2015-03-21"
#>  [6] "2016-03-20" "2017-03-20" "2018-03-21" "2019-03-21" "2020-03-20"
#> 
#> $`Showa Day`
#>  [1] "2011-04-29" "2012-04-29" "2013-04-29" "2014-04-29" "2015-04-29"
#>  [6] "2016-04-29" "2017-04-29" "2018-04-29" "2019-04-29" "2020-04-29"
#> 
#> $`Greenery Day`
#>  [1] "2011-05-04" "2012-05-04" "2013-05-04" "2014-05-04" "2015-05-04"
#>  [6] "2016-05-04" "2017-05-04" "2018-05-04" "2019-05-04" "2020-05-04"
#> 
#> $`The Emperor's Birthday`
#>  [1] "2011-12-23" "2012-12-23" "2013-12-23" "2014-12-23" "2015-12-23"
#>  [6] "2016-12-23" "2017-12-23" "2018-12-23" NA           "2020-02-23"
#> 
#> $`Constitution Memorial Day`
#>  [1] "2011-05-03" "2012-05-03" "2013-05-03" "2014-05-03" "2015-05-03"
#>  [6] "2016-05-03" "2017-05-03" "2018-05-03" "2019-05-03" "2020-05-03"
#> 
#> $`Children's Day`
#>  [1] "2011-05-05" "2012-05-05" "2013-05-05" "2014-05-05" "2015-05-05"
#>  [6] "2016-05-05" "2017-05-05" "2018-05-05" "2019-05-05" "2020-05-05"
#> 
#> $`Marine Day`
#>  [1] "2011-07-18" "2012-07-16" "2013-07-15" "2014-07-21" "2015-07-20"
#>  [6] "2016-07-18" "2017-07-17" "2018-07-16" "2019-07-15" "2020-07-23"
#> 
#> $`Mountain Day`
#>  [1] NA           NA           NA           NA           NA          
#>  [6] "2016-08-11" "2017-08-11" "2018-08-11" "2019-08-11" "2020-08-10"
#> 
#> $`Respect for the Aged Day`
#>  [1] "2011-09-19" "2012-09-17" "2013-09-16" "2014-09-15" "2015-09-21"
#>  [6] "2016-09-19" "2017-09-18" "2018-09-17" "2019-09-16" "2020-09-21"
#> 
#> $`Autumnal Equinox Day`
#>  [1] "2011-09-23" "2012-09-22" "2013-09-23" "2014-09-23" "2015-09-23"
#>  [6] "2016-09-22" "2017-09-23" "2018-09-23" "2019-09-23" "2020-09-22"
#> 
#> $`Sports Day`
#>  [1] "2011-10-10" "2012-10-08" "2013-10-14" "2014-10-13" "2015-10-12"
#>  [6] "2016-10-10" "2017-10-09" "2018-10-08" "2019-10-14" "2020-07-24"
#> 
#> $`Culture Day`
#>  [1] "2011-11-03" "2012-11-03" "2013-11-03" "2014-11-03" "2015-11-03"
#>  [6] "2016-11-03" "2017-11-03" "2018-11-03" "2019-11-03" "2020-11-03"
#> 
#> $`Labour Thanksgiving Day`
#>  [1] "2011-11-23" "2012-11-23" "2013-11-23" "2014-11-23" "2015-11-23"
#>  [6] "2016-11-23" "2017-11-23" "2018-11-23" "2019-11-23" "2020-11-23"

Created on 2020-04-05 by the reprex package (v0.3.0)

yutannihilation commented 4 years ago

速度比較しようと思ったけど今のmasterだとbench::mark()が終わらなかった...。ということでたぶんめっちゃ速くなっているはずです。

library(zipangu)

set.seed(100)
d <- as.Date(sample(1:50000, 100, replace = TRUE), origin = "1970-01-01")

bench::mark(
  is_jholiday(d),
  min_iterations = 20
)
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
#> # A tibble: 1 x 6
#>   expression          min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>     <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 is_jholiday(d)    248ms    256ms      3.90        NA     11.1

Created on 2020-04-05 by the reprex package (v0.3.0)

yutannihilation commented 4 years ago

@uribo レビュー可能になったのでお手すきのとき見ていただければ...

uribo commented 4 years ago

@yutannihilation ありがとうございます。素晴らしいです! NEWSの追加とDESCRIPTIONにコントリビュータとしての追記をお願いできますか?

yutannihilation commented 4 years ago

お、了解です。やります。

uribo commented 4 years ago

Thanks a lot :)

yutannihilation commented 4 years ago

Thanks!

ちなみに year が複数もあるようになったことで、 #16 の実装は若干ややこしくなる気がしています。今日は手がつけられなかったけど近々やります...