Created
January 22, 2017 17:10
-
-
Save amir/5d6903aa56beb31649447dcc224e8abd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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