{"id":17252043,"url":"https://github.com/ppetr/timeout-with-results","last_synced_at":"2025-04-14T05:34:33.982Z","repository":{"id":5766014,"uuid":"6979136","full_name":"ppetr/timeout-with-results","owner":"ppetr","description":"Allows timeouting a computation while allowing it to return partial results. Useful for making AI-like algorithms that should return the best result found within a time limit.","archived":false,"fork":false,"pushed_at":"2012-12-15T18:29:54.000Z","size":148,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T19:21:56.659Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ppetr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-03T08:45:41.000Z","updated_at":"2019-12-05T06:32:36.000Z","dependencies_parsed_at":"2022-08-20T23:40:15.514Z","dependency_job_id":null,"html_url":"https://github.com/ppetr/timeout-with-results","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppetr%2Ftimeout-with-results","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppetr%2Ftimeout-with-results/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppetr%2Ftimeout-with-results/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppetr%2Ftimeout-with-results/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ppetr","download_url":"https://codeload.github.com/ppetr/timeout-with-results/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248766732,"owners_count":21158301,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T06:52:53.484Z","updated_at":"2025-04-14T05:34:33.954Z","avatar_url":"https://github.com/ppetr.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# timeout-with-results\n\nA Haskell library that allows timeouting a computation while allowing it to\nreturn partial results.  Useful for AI-like algorithms that should\nreturn the best result found within a time limit.\n\nIt comes in two variants:\n\n1. Simple, which only allows computations to save partial results, not\nretrieve what has been written already. If a computation times out, the last\nsaved partial result is returned. It comes in two flavours: One that converts\nsaved values to WHNF, the other to NF. (This is required so that the producing\nthread performs the computations, not the consuming thread.)\n2. Based on MonadWriter. The types of partial results have to be monoids.\nSaving a partial result combines it with the saved value using `mappend`. It\nalso adds the ability to run a contained computation within another one,\nwithout disturbing its output.\n\n\n## Examples\n\n### Computing pairs of prime twins\n\n```haskell\n{-# LANGUAGE FlexibleContexts #-}\nimport Control.Monad\nimport Data.Numbers.Primes -- package 'primes'\nimport System.Timeout.Returning\n\n-- | Loop forever, computing prime twins.\nprimeTwins :: MonadTimeout (Integer, Integer) m =\u003e [Integer] -\u003e m (Maybe (Integer,Integer))\nprimeTwins (p : ps@(p' : _))\n    | p' == p + 2   = partialResult (p, p') \u003e\u003e primeTwins ps\n    | otherwise     = primeTwins ps\n\n-- | Print the largest pair of prime twins we were able to compute in 100ms.\nmain :: IO ()\nmain = runTimeoutNF 100000 (primeTwins primes) \u003e\u003e= print\n```\n\n### Number guessing game\n\n```haskell\n{-# LANGUAGE FlexibleContexts #-}\nimport Control.Monad\nimport Control.Monad.IO.Class\nimport System.Random\nimport System.Timeout.Returning.Writer\n\n-- | Let the user guess until she hits the number.\nguess :: (MonadIO m, MonadWriter [Int] m)\n      =\u003e Int            -- ^ The number to be guessed.\n      -\u003e m ()\nguess n = loop\n  where\n    loop = do\n        is \u003c- liftIO $ putStr \"Guess: \" \u003e\u003e liftM reads getLine\n        case is of\n            ((i,_) : _) -\u003e do\n                tell [i]\n                case i `compare` n of\n                    EQ  -\u003e return ()\n                    LT  -\u003e liftIO (putStrLn \"Guess larger.\")  \u003e\u003e loop\n                    GT  -\u003e liftIO (putStrLn \"Guess smaller.\") \u003e\u003e loop\n            _ -\u003e liftIO (putStrLn \"Invalid number.\") \u003e\u003e loop\n\n\n-- | Guess a number.\nmain :: IO ()\nmain = do\n    let limit = 20\n    putStrLn \"Guess a number from 1 to 100.\"\n    putStrLn $ \"You have \" ++ show limit ++ \" seconds.\"\n    n \u003c- randomRIO (1, 100)\n    (r, w) \u003c- runTimeout (limit * (10^6)) (guess n)\n    putStrLn \"\"\n    putStr \"The number was: \" \u003e\u003e print n\n    case r of\n        Just _      -\u003e putStrLn \"You win!\"\n        otherwise   -\u003e putStr \"Time's up, you lose. Your guesses: \" \u003e\u003e print w\n```\n\n# Copyright\n\nCopyright 2012, Petr Pudlák\n\nContact: [petr.pudlak.name](http://petr.pudlak.name/).\n\n![LGPLv3](https://www.gnu.org/graphics/lgplv3-88x31.png)\n\nThis program is free software: you can redistribute it and/or modify it under\nthe terms of the GNU Lesser General Public License as published by the Free\nSoftware Foundation, either version 3 of the License, or (at your option) any\nlater version.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY\nWARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A\nPARTICULAR PURPOSE.  See the GNU Lesser General Public License for more\ndetails.\n\nYou should have received a copy of the GNU Lesser General Public License along\nwith this program.  If not, see \u003chttp://www.gnu.org/licenses/\u003e.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppetr%2Ftimeout-with-results","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fppetr%2Ftimeout-with-results","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppetr%2Ftimeout-with-results/lists"}