我注意到方法combinations(从这里)返回Iterator。该方法应该是“懒惰的”以避免提前生成所有组合,这看起来很合理。现在我想知道,为什么它返回Iterator而不是Stream(这是 Scala 中的惰性列表)。
那么,为什么combinationsreturnIterator而不是Stream?
首先尝试使用 django.utils.functional.lazy 装饰器。我的函数返回两个列表,所以我用@lazy(list, list) 装饰它。这是一个简单的 jane 函数,而不是类上的方法/属性。
当我尝试调用生成的装饰函数时,我得到一个“'代理'对象不可调用”。
目前,我有这个代码main:
import Control.Monad
import Control.Applicative
binSearch :: Ord a => [a] -> a -> Maybe Int
main = do
xs <- lines <$> readFile "Cars1.txt"
x <- getLine <* putStr "Registration: " -- Right?
putStrLn $ case binSearch xs x of
Just n -> "Found at position " ++ show n
Nothing -> "Not found"
Run Code Online (Sandbox Code Playgroud)
我希望打印"Registration:",然后让程序等待输入x.我所写的内容是否意味着会出现这种情况?我是否需要<*,或者将putStr表达放在上面的行中以使其工作正常?
PS:我知道我必须转换binSearch为使用数组而不是列表(否则它可能不值得进行二分查找),但这是另一天的问题.
编辑:由于我用错误的例子写了这个问题并没有描述我的问题,我会再做一次!
在我看来,#flat_map,即使是Enumerator :: Lazy类的一部分,也不是很可枚举的.
这个例子正确地工作:
(1..Float::INFINITY).flat_map { |s| [s,s] }.take(4).to_a
Run Code Online (Sandbox Code Playgroud)
懒惰的实现也将起作用:
(1..Float::INFINITY).flat_map { |s| [s,s] }.take(4).to_a
Run Code Online (Sandbox Code Playgroud)
这只会考虑到块内生成的数组是有限的.但是在take(4)调用发生之前,它们也将被完全评估.哪个不是很懒惰.
因此,这将失败:
(1..Float::INFINITY).lazy.flat_map { |i| (i..Float::INFINITY).map(&:to_i) }.take(4).force
Run Code Online (Sandbox Code Playgroud)
因为在懒惰调用发生之前将完全评估"无限范围到数组".不过,我希望它"默认为懒惰".我的意思是,我确实理解这个难题在哪里,但我希望它以这种方式发生:flat_map评估每个实例是懒惰的,知道结果将是一个数组(或者至少是可枚举的),并将应用惰性机制在上面.因此,(i..Float :: INFINITY).map(&:to_i)将被lazified(这似乎不太兼容,因为map(&:to_i)调用将"强制"计算它).
Haskell 文档解释了这个evaluate函数:
在执行生成的IO操作时,强制将其参数计算为弱头正常形式.
Prelude Control.Exception> let xs = [1..100] :: [Int] Prelude Control.Exception> :sprint xs
xs = _
Prelude Control.Exception> let ys = evaluate xs
Prelude Control.Exception> :t ys
ys :: IO [Int]
Prelude Control.Exception> ys
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,6Prelu2,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
Prelude Control.Exception> :sprint xs
xs = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,
90,91,92,93,94,95,96,97,98,99,100]
Prelude Control.Exception> :sprint ys
ys = _
Run Code Online (Sandbox Code Playgroud)
为什么ys不是弱头正常形式,即:sprint ys不相等_ : _?
我得到了这个代码
public static class Logger
{
public static Func<ILogger> LoggerFactory;
private static readonly Lazy<ILogger> _log = new Lazy<ILogger>(LoggerFactory);
public static ILogger Instance
{
get
{
return _log.Value;
}
public static ILogger ConfigureLogging(string AppName, Version AppVersion)
{
// stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个静态类在应用程序中使用:
Logger.LoggerFactory = () => Logger.ConfigureLogging(AppName, AppVersion);
Logger.Instance.Information("Starting application");
Run Code Online (Sandbox Code Playgroud)
我希望第一行设置 LoggerFactory;但是,在第一次尝试写入日志时,由于尚未设置静态 Func LoggerFactory,因此引发了异常。
这段代码有什么问题?
谢谢
是map()和filter()的Optional懒样Stream?
我如何确认它们的类型?
为什么说不可变对象使用惰性哈希码初始化呢?对于可变对象也是如此,我们只能在需要时才计算哈希码,这会导致延迟初始化吗?
我具有相同功能的三个定义:
prompt :: String -> IO String
prompt = (getLine <*) . (hFlush stdout <*) . putStrLn
prompt' :: String -> IO String
prompt' str = do
putStrLn str
hFlush stdout
getLine
prompt'' :: String -> IO String
prompt'' str = putStrLn str >> hFlush stdout >> getLine
Run Code Online (Sandbox Code Playgroud)
prompt'并且prompt''都在运行前刷新标准输出getLine,但是不能prompt。为什么是这样?
在本地 Scala REPL 中,可以定义流和惰性列表,而无需关键字lazy。
scala> val fibo: LazyList[BigInt] = (0: BigInt) #:: (1: BigInt) #:: fibo.zip(fibo.tail).map { n => n._1 + n._2 }
fibo: LazyList[BigInt] = LazyList(<not computed>)
scala> fibo(100)
res17: BigInt = 354224848179261915075
scala> val prime: LazyList[Int] = 2 #:: LazyList.from(3).filter(i => prime.takeWhile {
| j => j * j <= i
| }.forall {
| k => i % k != 0
| })
prime: LazyList[Int] = LazyList(<not computed>)
scala> prime(100)
res18: Int = 547
Run Code Online (Sandbox Code Playgroud)
这与 …