keyboard_backspace

Eta Playground

File Bug Report

Welcome to the Eta Playground!

You can type Eta code into the editor and click the run button to run the code.

Examples:

Click on the examples below to load them into the editor.

  • Lens Example

    Shows an example of how to use lens.

    import Control.Lens import Data.Char main :: IO () main = do print $ ("hello","world")^._2 print $ set _2 42 ("hello","world") print $ ("hello",("world","!!!"))^._2._1 print $ set (_2._1) 42 ("hello",("world","!!!")) print $ "hello"^.to length print $ ("hello",("world","!!!"))^._2._2.to length print $ _1 .~ "hello" $ ((),"world") print $ ((), "world") & _1 .~ "hello" print $ view _2 (10,20) print $ over mapped succ [1,2,3] print $ over (mapped._2) succ [(1,2),(3,4)] print $ _1.mapped._2.mapped %~ succ $ ([(42, "hello")],"world") print $ both *~ 2 $ (1,2) res <- mapMOf (traverse._2) (\xs -> length xs <$ putStrLn xs) [(42,"hello"),(56,"world")] print res

  • Hello World!

    The standard hello world example for Eta.

    main :: IO () -- Note that this line is optional. Written for clarity. main = putStrLn "Hello World!"

  • Java Generics

    Shows how to interface with Java APIs that use Generics.

    -- These language extensions are currently required to support -- Java Generics. {-# LANGUAGE MagicHash, FlexibleContexts, TypeFamilies, DataKinds #-} -- This imports all the standard library functionality that helps -- you deal with importing Java methods into Eta. import Java import Control.Monad main :: IO () main = java $ do list <- newArrayList list <.> populateArray 10 populateArray :: Int -> Java (ArrayList JInteger) () populateArray n = do forM_ range $ \i -> add (newInteger i) forM_ range $ \i -> do jint <- get i io $ print $ intValue jint * 5 where range = [0..n] -- The following a declarations of Java wrapper types. These types let you -- interact directly with the corresponding Java objects. -- This will not be the final syntax for Java wrapper types, see: -- https://github.com/typelead/eta/issues/140 data {-# CLASS "java.util.Collection" #-} Collection a = Collection (Object# (Collection a)) deriving Class data {-# CLASS "java.util.List" #-} List a = List (Object# (List a)) deriving Class -- The `Inherits` type family specifies parent classes and interfaces -- so that the Eta typechecker can statically check inheritance relationships. type instance Inherits (List a) = '[Collection a] data {-# CLASS "java.util.ArrayList" #-} ArrayList a = ArrayList (Object# (ArrayList a)) deriving Class type instance Inherits (ArrayList a) = '[List a] data {-# CLASS "java.lang.Integer" #-} JInteger = JInteger (Object# JInteger) deriving Class foreign import java unsafe "@new" newInteger :: Int -> JInteger foreign import java unsafe "intValue" intValue :: JInteger -> Int foreign import java unsafe "@new" newArrayList :: Java c (ArrayList a) -- The `Extends` multi-parameter typeclass checks whether the first type -- is a descendant of the second. This static check is facilitated by -- the `Inherits` type family above. foreign import java unsafe "add" add :: (Extends a Object, Extends b (Collection a)) => a -> Java b Bool foreign import java unsafe "get" get :: (Extends a Object, Extends b (List a)) => Int -> Java b a

Packages:

You have the following packages available to you. Click on a package to go to the documentation.

Output:

block