在Perl 6中找到上周五的日期? [英] Find last Friday’s Date in Perl 6?

查看:60
本文介绍了在Perl 6中找到上周五的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个序列,该序列在上周五从星期一到星期四结束,如果前一星期的星期五在周六和周日开始,则结束。也就是说,假设今天是 2018-05-09 ,那么上周五是 2018-05-04
如果今天是 2018-05-12 ,那么上周五也是也是 2018-05-04 。所以我写:

 (Date.today,* .earlier(:1day)...(* .day-of- week == 5&& * .week [1] + 1 == Date.today.week [1]))。tail#输出:2018-05-06 

但是结果是 2018-05-06 而不是 2018-05 -04



然后我使用了结点

 (今天的日期,*。earlier(:1day)... all(* .day-of-week == 5,* .week [1] + 1 == Date.today.week [1]))。tail#输出:2018-05-04 

为什么在第一种情况下& 是错误的? ... 运算符表示:


右侧将有一个端点,对于无限列表(其元素仅按需产生),可以为Inf或*,


有什么问题吗?


&& 运算符?

解决方案

问题出在您的结束条件

  *。day-of-week == 5&& * .week [1] + 1 == Date.today.week [1] 

那是两个分别接受1个参数的WhateverCode lambda。

  *。day-of-week == 5 





  *。week [1] + 1 == Date.today.week [1] 

代码对象为真值,&& 运算符移至第二个。因此,序列在到达上周的星期日时停止。



即使代码是单个lambda,也不会像您期望的那样工作,因为它将是lambda需要两个参数。



执行此检查的正确方法是使用某种块。

  {。day-of-week == 5&& .week-number + 1 == Date.today.week-number} 

将其包装在子例程中以便可以对其进行测试的想法。

  sub上周五( Date:D $ date){
#缓存它,这样就不必在每次迭代中都查找它。
my $ week-number = $ date.week-number-1;


$ date,
*。更早(:1day)

...

{
。一周中的天== 5
&&
.week-number == $ week-number
}
).tail
}

说最后一个星期五Date.new::year(2018),:month(5),:day(9); #2018-05-04
说最后一个星期五Date.new::year(2018),:month(5),:day(12); #2018-05-04

说Date.today。& last-friday; #2018-05-04






您也可以计算正确的日期。

  sub上周五(Date:D $ date){
$ date.earlier:
天=> (
$ date.day-of-week#重置为上一个星期天
+ 2#两天前返回以获取星期五

}

再说上周五的Date.new::year(2018),:month(5),:day(9); #2018-05-04
说最后一个星期五Date.new::year(2018),:month(5),:day(12); #2018-05-04

说Date.today。& last-friday; #2018-05-04


I want to generate a sequence that ends last Friday from Monday to Thursday, and the Friday of the previous week if the sequence starts on Saturday and Sunday. That is, assuming that today is 2018-05-09, then last Friday is 2018-05-04, If today is 2018-05-12, then last Friday is also 2018-05-04. So I write:

(Date.today, *.earlier(:1day) ... ( *.day-of-week==5 && *.week[1]+1==Date.today.week[1] )).tail # Output: 2018-05-06

But the result is 2018-05-06 instead of 2018-05-04.

Then I used a Junction:

(Date.today, *.earlier(:1day) ... all( *.day-of-week==5,  *.week[1]+1==Date.today.week[1] )).tail # Output: 2018-05-04

Why && in the first situation is wrong? The ... operator says that:

The right-hand side will have an endpoint, which can be Inf or * for "infinite" lists (whose elements are only produced on demand), an expression which will end the sequence when True, or other elements such as Junctions.

What's wrong with the && operator?

解决方案

The problem is your ending condition

*.day-of-week==5 && *.week[1]+1==Date.today.week[1]

That is two WhateverCode lambdas that each take 1 argument.

*.day-of-week==5

*.week[1]+1==Date.today.week[1]

Since a code object is a true value, the && operator moves onto the second one. So the sequence stops when it reaches sunday of the previous week.

Even if the code was a single lambda it wouldn't work like you expect as it would be a lambda that takes two arguments.

The right way to do this check is to use some sort of block.

{.day-of-week==5 && .week-number+1 == Date.today.week-number}

It might be a good idea to wrap it in a subroutine so that you can test it.

sub last-friday ( Date:D $date ) {
  # cache it so that it doesn't have to be looked up on each iteration
  my $week-number = $date.week-number - 1;

  (
    $date,
    *.earlier( :1day )

    ...

    {
          .day-of-week == 5
      &&
          .week-number == $week-number
    }
  ).tail
}

say last-friday Date.new: :year(2018), :month( 5), :day( 9); # 2018-05-04
say last-friday Date.new: :year(2018), :month( 5), :day(12); # 2018-05-04

say Date.today.&last-friday; # 2018-05-04


You could also just calculate the proper date.

sub last-friday ( Date:D $date ) {
  $date.earlier:
    days => (
      $date.day-of-week # reset to previous sunday
      + 2               # go two days earlier to get friday
    )
}

say last-friday Date.new: :year(2018), :month( 5), :day( 9); # 2018-05-04
say last-friday Date.new: :year(2018), :month( 5), :day(12); # 2018-05-04

say Date.today.&last-friday; # 2018-05-04

这篇关于在Perl 6中找到上周五的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆