Skip to content

Instantly share code, notes, and snippets.

@tel
Last active October 17, 2017 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tel/bb84fcc3f1b7488b53349156284b509a to your computer and use it in GitHub Desktop.
Save tel/bb84fcc3f1b7488b53349156284b509a to your computer and use it in GitHub Desktop.
{-# 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment