Eta Cheatsheet

A quick reference for Eta

Input and Output


      module Main where
      main :: IO ()
      main = putStrLn "Hello World!"
  

Hello World

Main.eta

                
                module Main where
                main :: IO ()
                main = putStrLn "Hello World!"
            

$ etlas build

Or try it on Tour of Eta.

Bindings

Binding declaration

            myName :: String
            myName = "Eta"

            main :: IO ()
            main = putStrLn $ myName
            

Lists

List declaration

users :: [String]
users = [ "Tom", "Dick", "Harry" ]

main :: IO ()
main = print $ users

List operations

main :: IO ()
main = do
 print $ head users
 print $ tail users

Functions

Basic structure

function :: type -> type
function x = expr

function1 :: type -> [type] -> type
function1 x xs = expr

main = do code
       code
       ...

Control Flow

if-then-else expression

main = do
   if 36 `rem` 2 == 0
      then putStrLn "Number is Even"
   else putStrLn "Number is Odd"

Pattern Matching

This sets username to "Harry".

data User = User { name :: String, age :: Int }
  deriving Show

user :: User
user = User "Harry" 42

main :: IO ()
main = do
  let User { name = userName } = user
  print userName

Types

Primitive Types

nil null
true / false Boolean
72 Integer
3.14(32 bits) Float
3.14(64 bits) Double
'hello' Character
"hello" String
['a','b','c']same as "abc" List
{a, b} Tuple

Modules

Declaring Module

New module named Main

              module Main where
              main :: IO ()
              main = putStrLn "Eta's first module!"
 

Importing

Importing Data.Time Module

import Data.Time

main = do
    now <- getCurrentTime
    print now

Qualified Imports

Qualified is similar to aliases

import qualified Text.Printf as T

main :: IO ()
main = T.printf "Hello %s! Your score is 100." "Harry"

Foreign Function Interface

Java Wrapper Type

Referencing a Java class inside of Eta

data JInteger = JInteger @java.lang.Integer
  deriving Class

Java Foreign Import Declarations

Import a Java method as an Eta monadic action

foreign import java unsafe coalesce :: Int -> Java JavaDoubleRDD JavaDoubleRDD

Marshalling Between Java and Eta Types

Java Type Eta Type
boolean Bool
byte Byte
short Short
char JChar
int Int
long Int64
float Float
double Double

Primitive Arrays in Eta

Java Type Array Type Element Type
boolean[] JBooleanArray Bool
byte[] JByteArray Byte
short[] JShortArray Short
char[] JCharArray JChar
int[] JIntArray Int
long[] JLongArray Int64
float JFloatArray Float
double JDoubleArray Double

Instance Methods

Import boolean canExecute() from the java.io.File class.

foreign import java unsafe canExecute :: Java File Bool

Static Methods

Import File createTempFile(String, String) from the java.io.File class.

foreign import java unsafe "@static java.io.File.createTempFile"
  createTempFile  :: String -> String -> Java a File

Constructors

Import File(String) from the java.io.File class.

foreign import java unsafe "@new" newFile  :: String -> Java a File

Instance Fields

Import private String path from the java.io.File class.

foreign import java unsafe "@field path" getFilePath  :: Java File String

Static Fields

Import String pathSeparator from the java.io.File class.

foreign import java unsafe "@static @field java.io.File.pathSeparator"
  getPathSeparator  :: Java a String

Concurrency

forkIO

Use all the cores of your CPU

import Control.Concurrent

main = do
  forkIO $ do
    putStrLn "Yay! i'm in thread!"
  putStrLn "I'm important i'm in Main thread!"

Channels

Channels help communicating with threads

import Control.Concurrent.Chan (newChan, writeChan, readChan)

main = do
    messages <- newChan
    writeChan messages "unbounded"
    writeChan messages "channels"

    msg <- readChan messages
    putStrLn msg
    putStrLn =<< readChan messages

MVar

MVar is a single-element Channel

import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)

p :: IO Int
p = do
  num <- newEmptyMVar
  forkIO $ putMVar num 7
  forkIO $ putMVar num 9
  takeMVar num

main = do
  v <- p
  putStr $ show v

putMVarblocks if MVar has a value.

takeMVarblocks if MVar is empty.