lifting

Lift instance of class with a `MonadIO` type-variable to the transformed monad

ⅰ亾dé卋堺 提交于 2021-02-11 06:37:12
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

Lift instance of class with a `MonadIO` type-variable to the transformed monad

雨燕双飞 提交于 2021-02-11 06:36:49
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

Combining predicates in a functional way

扶醉桌前 提交于 2021-02-10 17:53:14
问题 Does Boost Hana provide a way to combine predicates with logical operators? I'm referring to something roughly like this constexpr auto both = [](auto&& f, auto&& g){ return [&f,&g](auto&& x){ return f(x) && g(x); }; }; that could be used like this: int main() { std::vector<int> v{1,2,3,4,5,6,7,8,9,10}; auto less_than_7 = hana::reverse_partial(std::less_equal<int>{}, 7); auto more_than_3 = hana::reverse_partial(std::greater_equal<int>{}, 3); auto r = ranges::views::remove_if(v, both(less_than

Can't wrap my head around “lift” in Ramda.js

社会主义新天地 提交于 2020-01-19 07:05:07
问题 Looking at the source for Ramda.js, specifically at the "lift" function. lift liftN Here's the given example: var madd3 = R.lift(R.curry((a, b, c) => a + b + c)); madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7] So the first number of the result is easy, a , b , and c , are all the first elements of each array. The second one isn't as easy for me to understand. Are the arguments the second value of each array (2, 2, undefined) or is it the second value of the first array and the

Can't wrap my head around “lift” in Ramda.js

♀尐吖头ヾ 提交于 2020-01-19 07:03:06
问题 Looking at the source for Ramda.js, specifically at the "lift" function. lift liftN Here's the given example: var madd3 = R.lift(R.curry((a, b, c) => a + b + c)); madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7] So the first number of the result is easy, a , b , and c , are all the first elements of each array. The second one isn't as easy for me to understand. Are the arguments the second value of each array (2, 2, undefined) or is it the second value of the first array and the

Flatten monad stack

£可爱£侵袭症+ 提交于 2019-12-23 12:33:50
问题 So I have this sort of code all over my first serious haskell project: f :: (MonadTrans t) => ExceptT () (t (StateT A B)) C f = do mapExceptT lift $ do lift $ do ... lift $ do ... r <- ... ... return r >>= \r -> ... There definitely may be something wrong about how I try to achieve my goals (there might be simpler ways how to do it) but currently I am interested in learning how to handle a stack of monad transformers in some nicer way, if there is one. This is the only way I figured out how

Difference between lifting and higher order functions

我与影子孤独终老i 提交于 2019-12-22 17:11:24
问题 I usually hear the term lifting, when people are talking about map , fold , or bind , but isn't basically every higher order function some kind of lifting? Why can't filter be a lift from a -> Bool to [a] -> [a] , heck even the bool function (which models an if statement) can be considered a lift from a -> a to Bool -> a . And if they are not, then why is ap from the Applicative type class considered a lift? If the important thing is going from ... a ... to ... f a ... , then ap wouldn't fit

Using a pure function in a Haskell monad / left-lifting?

*爱你&永不变心* 提交于 2019-12-11 06:28:02
问题 Consider the following function: foo = [1,2,3] >>= return . (*2) . (+1) For better readability and logic, I would like to move my pure functions (*2) and (+1) to the left of the return. I could achieve this like this: infixr 9 <. (<.) :: (a -> b) -> (b -> c) -> (a -> c) (<.) f g = g . f bar = [1,2,3] >>= (+1) <. (*2) <. return However, I don't like the right-associativity of (<.) . Let's introduce a function leftLift : leftLift :: Monad m => (a -> b) -> a -> m b leftLift f = return . f baz =

Scala strange implicit boxing conversion error

不羁的心 提交于 2019-12-11 02:18:59
问题 Can someone tell me why the following does not work? object TestObject { def map(f: (Double, Double) => Double, x2: Array[Double]) = { val y = x2.zip( x2 ) val z = y.map(f) z } } Produces this error: type mismatch; found : (Double, Double) => Double required: ((Double, Double)) => ? 回答1: In this snippet, f is a function taking two Double parameters and returning a Double . You are attempting to call f by passing a single argument of type Tuple2[Double,Double] . You can fix this by changing

Lifting a value in the State monad in Haskell

大城市里の小女人 提交于 2019-12-10 20:33:41
问题 I am writing a Sudoku generator/solver in Haskell as a learning exercise. My solve function takes in a UArray but returns a State Int (UArray ...) so that it can also return the maximum difficulty level that it found while solving. This is my function so far (still in the very experimental early stage): import Control.Monad.State (State, put) import Control.Monad.Trans.Class (lift) import Data.Array.MArray (thaw) import Data.Array.ST (runSTUArray) import Data.Array.Unboxed (UArray) -- ...