What does this code do?

Could anyone enlighten me what this code actually does with the data in the array?

powHashToTargetWords :: Digest Blake2s_256 -> IO TargetWords
powHashToTargetWords h = BA.withByteArray h $ \ptr -> TargetWords
    <$> peekWord64OffLe ptr 0
    <*> peekWord64OffLe ptr 8
    <*> peekWord64OffLe ptr 16
    <*> peekWord64OffLe ptr 24
{-# INLINE powHashToTargetWords #-}

It reads four consecutive (little-endian) Word64 elements out of the array and stores them into a TargetWords structure.


Thanks. What are those <$> and <*> actually?

Applicative functor, a very very handy abstraction to add effects to your pure functions. Here is a short and to the point introduction to applicative functors, but any learning material plus reimplementing them your own will do.

λ> "foo " ++ "bar"
"foo bar"
λ> :t (++) <$> getLine <*> getLine
(++) <$> getLine <*> getLine :: IO [Char]
λ> (++) <$> getLine <*> getLine
"Hello, Oliver!"
1 Like

Nifty. Thanks for the explanation.