Skip to content

Instantly share code, notes, and snippets.

@amir
Created January 22, 2017 17:10
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 amir/5d6903aa56beb31649447dcc224e8abd to your computer and use it in GitHub Desktop.
Save amir/5d6903aa56beb31649447dcc224e8abd to your computer and use it in GitHub Desktop.
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module MyCollections where
import Java
import Java.Collections
import Control.Monad (forM_)
data {-# CLASS "java.util.Collection" #-} Collection = Collection (Object# Collection)
deriving Class
data {-# CLASS "java.util.List" #-} List = List (Object# List)
deriving Class
data {-# CLASS "java.util.ArrayList" #-} ArrayList = ArrayList (Object# ArrayList)
deriving (Class, Show)
foreign import java unsafe "@new" newArrayList :: Java c ArrayList
foreign import java unsafe "@interface add" add ::
(Extends a Object, Extends b Collection) => a -> Java b Bool
foreign import java unsafe "@interface addAll" addAll ::
(Extends b Collection) => b -> Java b Bool
type instance Inherits List = '[Collection]
type instance Inherits ArrayList = '[List]
instance Monoid ArrayList where
mempty = pureJava newArrayList
a1 `mappend` a2 = pureJava $ do
al <- newArrayList
forM_ [a1, a2] $ \x ->
al <.> addAll x
return al
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment