lazy-evaluation

Why is the $! operator right-associative?

会有一股神秘感。 提交于 2019-12-12 12:12:15
问题 I'm just learning Haskell and I'm still not entirely clear on when and how strict evaluation is forced When I want a function to evaluate its arguments strictly I find myself writing ((f $! x) $! y ) $! z which seems weird. Shouldn't $! be left-associative so I could write f $! x $! y $! z and have it do what I want? Am I completely misunderstanding the $! operator? 回答1: Argument against I found a proposal from 2008 in haskell-prime to make the $ and $! operators left-associative: https://ghc

Are peekCString and peekCStringLen lazy?

三世轮回 提交于 2019-12-12 11:23:28
问题 I have a C function that creates a null terminated string and returns a pointer to it, there is also corresponding deallocation function. foreign import ccall unsafe "get_str" getStr :: IO CString foreign import ccall unsafe "free_str" freeStr :: CString -> IO () I want to create a Haskell String from the returned CString, and free CString as soon as possible. do cStr <- getStr str <- peekCString cStr freeStr cStr -- here str is used Is it safe to free cStr before str is used? In other words,

Test if a value has been evaluated to weak head normal form

送分小仙女□ 提交于 2019-12-12 07:32:43
问题 In Haskell, is it possible to test if a value has been evaluated to weak head normal form? If a function already exists, I would expect it to have a signature like evaluated :: a -> IO Bool There are a few places that similar functionality lives. A previous answer introduced me to the :sprint ghci command, which will print only the portion of a value that has already been forced to weak head normal form. :sprint can observe whether or not a value has been evaluated: > let l = ['a'..] >

Laziness/strictness between data and newtype

瘦欲@ 提交于 2019-12-12 07:32:02
问题 I'm struggling to understand why these two snippets produce different results under the so-called "poor man's strictness analysis". The first example uses data (assuming a correct Applicative instance): data Parser t a = Parser { getParser :: [t] -> Maybe ([t], a) } > getParser (pure (,) <*> literal ';' <*> undefined ) "abc" *** Exception: Prelude.undefined The second uses newtype . There is no other difference: newtype Parser t a = Parser { getParser :: [t] -> Maybe ([t], a) } > getParser

Lazy caching of a method (like a DB getter)

蓝咒 提交于 2019-12-12 06:09:07
问题 Just to avoid reinventing the wheel I'm wondering whether a standard C# implementation already exists to cache the results from a long-running, resource-intensive method. To my mind, the Lazy<T> would be appropriate, but unfortunately it seems to lack the input parameter to index the result. I hope the following helps to clarify: this is my custom solution. public class Cached<FromT,ToT> { private Func<FromT,ToT> _my_func; private Dictionary<FromT,ToT> funcDict; public Cached(Func<FromT,ToT>

Implement AsyncManualResetEvent using Lazy<T> to determine if the task has been awaited

假装没事ソ 提交于 2019-12-12 04:17:21
问题 I'm implementing an AsyncManualResetEvent based on Stephen Toub's example. However, I would like to know if the event, or specifically, the underlying Task<T> has been waited on. I've already investigated the Task class, and there doesn't seem to be a sensible way to determine if it has ever been 'awaited' or if a continuation has been added. In this case however, I control access to the underlying task source, so I can listen for any calls to the WaitAsync method instead. In thinking about

Scala web :: lazy val scope/lifetime

白昼怎懂夜的黑 提交于 2019-12-12 02:59:59
问题 In a Scala web application, is lazy val scoped to the lifetime of the application server, or request scoped? I assume it is per request, but have not been able to find a definitive answer, thus the question. Thanks 回答1: lazy is a Scala feature, not related to web application programming. It means: evaluate only once on first access . If the variable is part of an object created per request, it will be lazily evaluated once per every request. If it is declared inside application-wide class (or

Django 1.7: Lazy evaluation to avoid “App registry isn't ready yet” in models.py

落花浮王杯 提交于 2019-12-11 22:24:55
问题 I have a query in a model's method definition: 'content_type_id': ContentType.objects.get_for_model(model).pk, https://github.com/seperman/django-tagging/blob/develop/tagging/models.py#L107 And it raises "App registry is not ready yet". Putting the line inside another independent function doesn't help either since it still gets called. Django docs: Executing database queries with the ORM at import time in models modules will also trigger this exception. The ORM cannot function properly until

Does clojure-xml/parse return a lazy sequence?

不羁岁月 提交于 2019-12-11 20:05:03
问题 clojure-xml/parse returns a map of an xml file. (ns xml-lib.core ^{:author "Charles M. Norton", :doc "xml-lib is an xml parsing library built on clojure-xml. Created on June 26, 2012"} (:require [clojure.string :as cstr]) (:require [util.core :as utl]) (:require [clojure.xml :as cjxml])) (defn ret-xml-data "Returns a map of the supplied xml file." [xml-fnam] (let [test-file-nam (utl/open xml-fnam)] (cjxml/parse xml-fnam)) Is the returned map lazy, or should I pass the parse call into a lazy

How to make fromList lazy in this dynamic programming example?

社会主义新天地 提交于 2019-12-11 13:37:29
问题 module Main where import System.Random import Data.Foldable import Control.Monad import qualified Data.Map as M import qualified Data.Vector as V import Debug.Trace import Data.Maybe import Data.Ord -- Represents the maximal integer. maxBound is no good because it overflows. -- Ideally should be something like a billion. maxi = 1000 candies :: V.Vector Int -> Int --M.Map (Int, Int) Int candies ar = ff [l (V.length ar - 1) x | x <- [0..maxi]] where go :: Int -> Int -> Int go _ 0 = maxi go 0 j