urbit/pkg/hs-urbit/lib/Data/Noun/Lens.hs

33 lines
937 B
Haskell
Raw Normal View History

{-# LANGUAGE MagicHash #-}
module Data.Noun.Lens where
import ClassyPrelude
import Data.Noun.Pill
import Data.Noun
import Data.Noun.Atom
import Control.Lens
2019-07-02 05:43:10 +03:00
import Data.Noun.Jam.Fast (jam, jamBS)
import Data.Noun.Jam (cue)
--------------------------------------------------------------------------------
_CueBytes :: Prism' ByteString Noun
_CueBytes = prism' jamBS unJamBS
where unJamBS = preview (from pillBS . from pill . _Cue)
_Cue :: Prism' Atom Noun
_Cue = prism' jam cue
loadNoun :: FilePath -> IO (Maybe Noun)
loadNoun = fmap (preview $ from pillBS . from pill . _Cue) . readFile
dumpJam :: FilePath -> Noun -> IO ()
dumpJam fp = writeFile fp . view (re _Cue . pill . pillBS)
tryCuePill :: PillFile -> IO ()
tryCuePill pill =
loadNoun (show pill) >>= \case Nothing -> print "nil"
Just (Atom _) -> print "atom"
_ -> print "cell"