Skip to content

Instantly share code, notes, and snippets.

@mmhelloworld
Created June 17, 2013 06:23
Show Gist options
  • Save mmhelloworld/5794941 to your computer and use it in GitHub Desktop.
Save mmhelloworld/5794941 to your computer and use it in GitHub Desktop.
Java List in Frege
module hellofrege.JavaList where
data LinkedList a = native java.util.LinkedList where
native add :: Mutable s (LinkedList a) -> a -> ST s Bool
native get :: Mutable s (LinkedList a) -> Int -> ST s (Maybe a)
native new :: () -> STMutable s (LinkedList a)
fromFregeList :: [a] -> STMutable s (LinkedList a)
fromFregeList xs = LinkedList.new () >>= loop xs where
loop (x:xs) jlist = LinkedList.add jlist x >> loop xs jlist
loop [] jlist = return jlist
plusTop :: Mutable s (LinkedList Int) -> ST s (Maybe Int)
plusTop xs = do
a <- xs.get 0
b <- xs.get 1
return ((+) <$> a <*> b)
data Exception = native java.lang.Exception
derive Exceptional Exception
data NullPointerException = native java.lang.NullPointerException
derive Exceptional NullPointerException
pure native showThrowable toString :: Throwable -> String
main _ = do
javaList <- LinkedList.fromFregeList [1, 2, 3]
try (\xs -> plusTop xs >>= (println . maybe "Got a null pointer" show)) javaList
`catch` (\(npe :: NullPointerException) -> println $ showThrowable npe)
`catch` (\(exception :: Exception) -> println $ showThrowable exception)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment