Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE GADTs #-}
- module Lib where
- import qualified Data.HashMap.Lazy as M
- import Data.Hashable
- import Data.Typeable
- -- this is what we know about all things from Java
- data Val where
- Val :: (Eq a, Hashable a, Typeable a) => a -> Val
- instance Hashable Val where
- hashWithSalt salt (Val a) = hashWithSalt salt a
- instance Eq Val where
- Val a == Val b =
- case cast b of
- Nothing -> False
- Just bb -> a == bb
- examine :: (Typeable a, Hashable a) => Val -> Maybe a
- examine (Val a) = cast a
- -- simplified, slightly, we won't do variadic inputs
- assoc :: Val -> Val -> Val -> Val
- assoc map key val =
- case examine map of
- Nothing -> error "not bothering with failure semantics"
- Just mapS -> Val (M.insert key val mapS)
- get :: Val -> Val -> Val
- get map key =
- case examine map of
- Nothing -> error "not bothering with failure semantics"
- Just mapS -> mapS M.! key
- {-
- (defn assoc-in
- "Associates a value in a nested associative structure, where ks is a
- sequence of keys and v is the new value and returns a new nested structure.
- If any levels do not exist, hash-maps will be created."
- {:added "1.0"
- :static true}
- [m [k & ks] v]
- (if ks
- (assoc m k (assoc-in (get m k) ks v))
- (assoc m k v)))
- -}
- assocIn :: Val -> [Val] -> Val -> Val
- assocIn _ [] _ = error "more unhandled errors"
- assocIn m [k] v = assoc m k v
- assocIn m (k:ks) v = assoc m k (assocIn (get m k) ks v)
Add Comment
Please, Sign In to add comment