我使用流过滤器从请求对象中获取两个日期。在那里我必须比较这些对象,然后将它们收集到列表中。但现在我得到这个错误。请帮我解决。
错误:
类型不匹配:无法从 int 转换为 boolean
代码:
Date checkIn = req.getCheckIn();
Date checkOut = req.getCheckOut();
List<PlaceBook> filtered = checkInVal.stream().filter(string ->
string.getCheckInDt().compareTo(checkIn)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 假设我想对(\x -> x+1)
列表中的所有元素应用一个简单的函数[1,2,3]
。
我做map (\x -> x+1) [1,2,3]
并得到了预期的结果[2,3,4]
。返回类型是Num a => [a]
。
现在,如果我的函数返回 Monad 类型并定义为 ,会发生 \x -> do return x+1
什么?我想以某种方式将此函数应用于列表中的所有元素并返回一个 type (Monad m, Num a) => m [a]
。
并且值将相同[2,3,4]
,只是用 Monad 包裹。
我已经为此苦苦挣扎了一段时间,但没有太大进展。知道如何将此功能映射到我的列表吗?
我写了一个这样的函数:
\n\nconstGrid :: a -> [[a]]\nconstGrid c = take 3 [take 3 [i,i ..] | i <- [c,c ..]]\n
Run Code Online (Sandbox Code Playgroud)\n\n我用以下方式调用它:
\n\nprint(constGrid \'a\')\n
Run Code Online (Sandbox Code Playgroud)\n\n它应该打印
\n\n["aaa","aaa","aaa"]\n
Run Code Online (Sandbox Code Playgroud)\n\n或任何整数或布尔值替换\'a\'
。
当我评论第一行时它可以工作,但是当我打开它时,它会给出如下错误:
\n\n[1 of 1] Compiling Main ( test.hs, test.o )\n\ntest.hs:17:46: error:\n\xe2\x80\xa2 No instance for (Enum a)\n arising from the arithmetic sequence \xe2\x80\x98c, c .. \xe2\x80\x99\n Possible fix:\n add (Enum a) to the context of\n the type signature for:\n constGrid :: a -> [[a]]\n\xe2\x80\xa2 In the …
Run Code Online (Sandbox Code Playgroud) 目前,要将a转换List<List<Foo>>
为a Stream<Foo>
,您必须使用以下内容:
Stream<Foo> stream = list.stream().flatMap(fs -> fs.stream());
//or
Stream<Foo> stream = list.stream().flatMap(Collection::stream);
Run Code Online (Sandbox Code Playgroud)
我认为这正是方法引用的设计目标,它确实提高了可读性.现在考虑一下:
Stream<Bar> stream = list.stream().flatMap(fs -> fs.getBarList().stream());
Run Code Online (Sandbox Code Playgroud)
有两个链式方法调用,没有方法参考是可能的,我已经发生了几次这种情况.虽然这不是一个大问题,但它似乎偏离了方法参考简洁性.
在使用JavaFX 8之后,我注意到它们的API常量是方便的方法.Java是一种非常冗长的语言,在我看来,简单的方法重载是JavaFX的一大卖点.
所以我的问题是,我想知道为什么没有Stream.flatMap(Collection)
可以被称为的方便方法:
Stream<Bar> stream = list.stream().flatMap(Foo::getBarList);
Run Code Online (Sandbox Code Playgroud)
这是甲骨文人员故意遗漏的吗?或者这会引起任何混淆吗?
注意:我知道"不基于意见的问题政策",我不是在寻找意见,我只是想知道是否有理由不采用这种方法.
mainDiag :: [[a]] -> [a]
mainDiag x = zipWith (!!) x [0..]
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这段代码(特别是zipWith (!!)
)以及它如何返回矩阵的对角线?