Compute as much of a list as possible in a fixed time

后端 未结 3 1657
遥遥无期
遥遥无期 2020-12-28 14:38

I want to write a function that takes a time limit (in seconds) and a list, and computes as many elements of the list as possible within the time limit.

My first att

3条回答
  •  悲&欢浪女
    2020-12-28 15:10

    You can implement timeOut with the type you gave using timeout and evaluate. It looks something like this (I've omitted the part that computes how much time is left -- use getCurrentTime or similar for that):

    timeoutPure :: Int -> a -> IO (Maybe a)
    timeoutPure t a = timeout t (evaluate a)
    

    If you want more forcing than just weak-head normal form, you can call this with an already-seq'd argument, e.g. timeoutPure (deepseq v) instead of timeoutPure v.

提交回复
热议问题