1

Is it possible?, (there is a >>magic function) to simplify this:

insertTransaction :: Day -> Int -> Int -> MyReaderT Bool
insertTransaction day amount price = ....

logTransaction :: Int -> Int -> MyReaderT Bool
logTransaction amount price = do
  day <- currentDay 
  insertTransaction day amount price

To this:

logTransaction :: Int -> Int -> MyReaderT Bool
logTransaction = currentDay `>>magic` insertTransaction

I think there should be one operator like >>magic but I can't found it. Nor <*> neither <$>.

2 Answers 2

8

If you wrote insertTransaction in the first place, it might make sense to refactor it to

insertTransaction :: Int -> Int -> Day -> MyReaderT Bool
insertTransaction amount price day = ....

Then you could say

logTransaction :: Int -> Int -> MyReaderT Bool
logTransaction amount price = currentDay >>= insertTransaction amount price
6

This isn't really possible without typeclass trickery, because you're trying to "lift" a function with an arbitrary number of arguments — which is, of course, not really a well-defined concept in Haskell, due to currying.

The best you're likely to get in standard, readable Haskell is this:

logTransaction :: Int -> Int -> MyReaderT Bool
logTransaction amount price = join $ insertTransaction
    <$> currentDay
    <*> pure amount
    <*> pure price

I think this is probably fine — the proposed operator seems quite difficult to read to me, since it's hard to tell how many arguments are being processed or where they're going.

With the Strathclyde Haskell Enhancement preprocessor, logTransaction could be written as follows, using idiom brackets:

logTransaction :: Int -> Int -> MyReaderT Bool
logTransaction amount price = (| insertTransaction currentDay ~amount ~price @ |)

Finally, it is technically possible to write logTransaction in a point-free style, but I wouldn't recommend it:

logTransaction :: Int -> Int -> MyReaderT Bool
logTransaction = ((currentDay >>=) .) . flip . flip insertTransaction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.