Input and Output
module Main where
main :: IO ()
main = putStrLn "Hello World!"
A quick reference for Eta
module Main where
main :: IO ()
main = putStrLn "Hello World!"
Main.eta
module Main where
main :: IO ()
main = putStrLn "Hello World!"
$ etlas build
Or try it on Tour of Eta.
Binding declaration
myName :: String
myName = "Eta"
main :: IO ()
main = putStrLn $ myName
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
Basic structure
function :: type -> type
function x = expr
function1 :: type -> [type] -> type
function1 x xs = expr
main = do code
code
...
if-then-else expression
main = do
if 36 `rem` 2 == 0
then putStrLn "Number is Even"
else putStrLn "Number is Odd"
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
True / False | Bool |
72 | Int (32 bits) |
3.14 | Float (32 bits) |
3.14 | Double (64 bits) |
'h' | Char |
"hello" | String |
['a','b','c']same as "abc" | [a] (List) |
(1, 2) | (Int,Int) (Tuple) |
New module named Main
module Main where
main :: IO ()
main = putStrLn "Eta's first module!"
Importing Data.Time Module
import Data.Time
main = do
now <- getCurrentTime
print now
Qualified is similar to aliases
import qualified Text.Printf as T
main :: IO ()
main = T.printf "Hello %s! Your score is 100." "Harry"
Referencing a Java class inside of Eta
data JInteger = JInteger @java.lang.Integer
deriving Class
Import a Java method as an Eta monadic action
foreign import java unsafe coalesce :: Int -> Java JavaDoubleRDD JavaDoubleRDD
Java Type | Eta Type |
boolean | Bool |
byte | Byte |
short | Short |
char | JChar |
int | Int |
long | Int64 |
float | Float |
double | Double |
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 |
Import boolean canExecute() from the java.io.File class.
foreign import java unsafe canExecute :: Java File Bool
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
Import File(String) from the java.io.File class.
foreign import java unsafe "@new" newFile :: String -> Java a File
Import private String path from the java.io.File class.
foreign import java unsafe "@field path" getFilePath :: Java File String
Import String pathSeparator from the java.io.File class.
foreign import java unsafe "@static @field java.io.File.pathSeparator"
getPathSeparator :: Java a String
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 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 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.