haskell

How to throttle a producer in a producer/consumer situation on a TChan in Haskell?

旧城冷巷雨未停 提交于 2020-01-13 05:18:08
问题 We have something dumping values on a TChan, and then a consumer processing those. But the consumer can't keep up, so we're getting lots of memory usage as the producer is dumping lots of stuff on the channel, but consumer isn't keeping up. Is there a straightforward way to have the producer block if the channel queue becomes a certain size or something so we can have the producer wait for the consumer to catch up? 回答1: Like John's answer, I'd suggest just building a bounded TChan yourself.

Filling the enclosed areas with random colors - Haskell - Friday

£可爱£侵袭症+ 提交于 2020-01-13 05:11:07
问题 I am trying to perform not very complex image analysis to try and find distinct shapes and calculate some of their parameters like area and perimeter (in pixels) and I am trying to do this in Haskell (I wanted to do that to try and work with functional programming language). The first task in line is to count the amount of spoons on the image: I am using Friday Haskell package to work with images. My idea was to use the Friday's edge detection and then fill all of the enclosed areas with it's

How Type inference work in presence of Functional Dependencies

早过忘川 提交于 2020-01-12 18:46:53
问题 Consider the code below : {-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FunctionalDependencies,UndecidableInstances,FlexibleContexts #-} class Foo a c | a -> c instance Foo Int Float f :: (Foo Int a) => Int -> a f = undefined Now when I see the inferred type of f in ghci > :t f > f :: Int -> Float Now If I add the following code g :: Int -> Float g = undefined h :: (Foo Int a) => Int -> a h = g I get the error Could not deduce (a ~ Float) I am not able to understand what has happened

How Type inference work in presence of Functional Dependencies

自古美人都是妖i 提交于 2020-01-12 18:46:17
问题 Consider the code below : {-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FunctionalDependencies,UndecidableInstances,FlexibleContexts #-} class Foo a c | a -> c instance Foo Int Float f :: (Foo Int a) => Int -> a f = undefined Now when I see the inferred type of f in ghci > :t f > f :: Int -> Float Now If I add the following code g :: Int -> Float g = undefined h :: (Foo Int a) => Int -> a h = g I get the error Could not deduce (a ~ Float) I am not able to understand what has happened

How to convert a Rational into a “pretty” String?

為{幸葍}努か 提交于 2020-01-12 17:40:31
问题 I want to display some Rational values in their decimal expansion. That is, instead of displaying 3 % 4 , I would rather display 0.75 . I'd like this function to be of type Int -> Rational -> String . The first Int is to specify the maximum number of decimal places, since Rational expansions may be non-terminating. Hoogle and the haddocks for Data.Ratio didn't help me. Where can I find this function? 回答1: Here is an arbitrary precision solution that doesn't use floats: import Data.Ratio

How to convert a Rational into a “pretty” String?

自闭症网瘾萝莉.ら 提交于 2020-01-12 17:40:11
问题 I want to display some Rational values in their decimal expansion. That is, instead of displaying 3 % 4 , I would rather display 0.75 . I'd like this function to be of type Int -> Rational -> String . The first Int is to specify the maximum number of decimal places, since Rational expansions may be non-terminating. Hoogle and the haddocks for Data.Ratio didn't help me. Where can I find this function? 回答1: Here is an arbitrary precision solution that doesn't use floats: import Data.Ratio

How to convert a Rational into a “pretty” String?

爱⌒轻易说出口 提交于 2020-01-12 17:40:02
问题 I want to display some Rational values in their decimal expansion. That is, instead of displaying 3 % 4 , I would rather display 0.75 . I'd like this function to be of type Int -> Rational -> String . The first Int is to specify the maximum number of decimal places, since Rational expansions may be non-terminating. Hoogle and the haddocks for Data.Ratio didn't help me. Where can I find this function? 回答1: Here is an arbitrary precision solution that doesn't use floats: import Data.Ratio

Conduit: Multiple Stream Consumers

戏子无情 提交于 2020-01-12 17:25:30
问题 I write a program which counts the frequencies of NGrams in a corpus. I already have a function that consumes a stream of tokens and produces NGrams of one single order: ngram :: Monad m => Int -> Conduit t m [t] trigrams = ngram 3 countFreq :: (Ord t, Monad m) => Consumer [t] m (Map [t] Int) At the moment i just can connect one stream consumer to a stream source: tokens --- trigrams --- countFreq How do I connect multiple stream consumers to the same stream source? I would like to have

Error while creating test suites: “cannot satisfy -package-id”

送分小仙女□ 提交于 2020-01-12 16:07:10
问题 I'm attempting to create a test suite for my project, HaskSplit in my .cabal configuration: -- Initial HaskSplit.cabal generated by cabal init. For further -- documentation, see http://haskell.org/cabal/users-guide/ name: HaskSplit version: 0.1.0.0 synopsis: Haskell Implementation of Shamir's Secret Sharing Scheme -- description: license: MIT license-file: LICENSE author: maintainer: -- copyright: category: Security build-type: Simple -- extra-source-files: cabal-version: >=1.10 executable

Make functions an instance of vector type class

做~自己de王妃 提交于 2020-01-12 15:48:34
问题 I have a custom type class for mathematical vectors {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} class Vector v a where infixl 6 <+> (<+>) :: v -> v -> v -- vector addition infixl 6 <-> (<->) :: v -> v -> v -- vector subtraction infixl 7 *> (*>) :: a -> v -> v -- multiplication by a scalar dot :: v -> v -> a -- inner product and I want to make numbers a and functions a -> vector into an instance of the class. Numbers are easy: instance Num a => Vector a a where (<+>) = (+) (<->)