|
|
E
Idioms
Quick Reference
mostly by Marc Stiegler |
This is intended as a compact representation of frequently-used syntax
and frequently-encountered special characteristics of E.
The beginning E user may find
it a handy reference while experimenting and reading the book; the experienced
E programmer may find it handy
as a refresher if returning to the language after some time. This reference
does not touch quasi-literals, regular expressions, pattern matching,
parse trees, or Kernel-E at
all.
- Simple Statements: def,
var, assign, print, add, comment
- Basic Flow: if, while,
for, try
- Modules: Function, Singleton Object, Object Maker,
Delegation, Matching
- Text File I/O
- Windowed Apps
- Data Structures: Strings, ConstLists, ConstMaps,
FlexLists, FlexMaps
- Java Interface
- Asynch Sends
- Remote Comm
Simple Statements
pragma.syntax("0.9")
def a := 2 + 3
var a2 := 4
a2 += 1
def b := "answer: "
println(b + a)
Basic Flow
if (a == b) {
println("match")
} else {
println("no match")
} |
while (a < b) {
a += 1
} |
try {
3 // 0
} catch err {
println("error: " + err)
} finally {
println("always")
} |
for next in 1..3 {
println(next)
}
for key => value in map {
println("got pair")
} |
Modules
Function |
Singleton Object (stateless) |
def addTwoPrint(number) {
println(number + 2)
return number + 2
}
def twoPlusThree := addTwoPrint(3) |
def adder {
to add1(number) {return number + 1}
to add2(number) {return number + 2}
}
def result := adder.add1(3) |
Objects with state |
Objects self-referencing during construction |
def makeOperator(baseNum) {
def instanceValue := 3
def operator {
to addBase(number) {
return baseNum + number
}
to multiplyBase(number) {
return baseNum * number
}
}
return operator
}
def threeHandler := makeOperator(3)
def threeTimes2 := threeHandler.multiplyBase(2) |
def makeOp() {
def op
def myAlerter := makeAlerter(op)
bind op {
to respondToAlert() {
myAlerter("got alert")
}
}
return op
} |
Delegation |
Matching an Interface like a Java Adapter |
def makeExtendedFile(myFile) {
def extendedFile extends myFile {
to append(text) {
var current := myFile.getText()
current := current + text
myFile.setText(current)
}
}
return extendedFile
}
|
def makeUpListener(reactor) :any {
def upListener {
to mouseUp(event) {
reactor.mouseUp()
}
match [verb, args] {}
}
return upListener
}
|
Text File I/O |
Windowed Applications |
def fileA := <file:~/Desktop/text.txt>
def fileB := <file>["/home/marcs/text.txt"]
def fileC := <c:/windows/desktop/text.txt>
fileA.setText("abc")
def contents := fileA.getText()
for line in contents.split("\n") {
println(line)
} |
def panel :=
JPanel`$labelA.X $labelB
$textArea.Y >`
interp.continueAtTop()
interp.blockAtTop() |
Data Structures
ConstList |
ConstMap |
var a := [8,6,"a"]
for i in a { println(i) }
a := a + ["b"]
def flexA := a.diverge() |
def m := ["c" => 5]
for key => value in m {
println(value)
}
def flexM := m.diverge() |
FlexList |
FlexMap |
flexA.append(["b"])
flexA.push("b")
def constA := flexA.snapshot() |
flexM["b"] := 2
flexM.removeKey("b")
def constM := flexM.snapshot() |
Strings are ConstLists of char that have additional methods
including Java string operators.
Flex structures respond to all Const messages except the comparison operators
("<", <=", etc...).
All these structures have many methods not listed here.
Java Interface
def frame1 := <awt:Frame>()
def frame2 := <swing:JFrame>()
def panel := <swing:JPanel>()
E.call(frame2.getContentPane(), "add(Component)", [panel])
def byte := <import:java.lang.Byte>.asType()
def byteArray := byte[300]
emakers
def uiKit := <import:com.skyhunter.ex.uiKit>
def buttonFunc() { println("pressed button") }
def button := uiKit.newToolButton(iconImage, "tip", buttonFunc)
def makeDialogVow := <import:com.skyhunter.ex.swing.dialogVowMakerAuthor>(<swing:JFrame>)
def helpDialog := makeDialogVow("Help",
"<html><b>Help</b><p>This is help.</html>",
null,
["OK"])
Eventual Sends
abacus <- add(a,b) |
when (def answer := abacus <- add(a,b)) -> {
println(`computation complete:$answer`)
} catch problem {
println(`promise broken $problem`)
} finally {
println("always")
} |
def carRcvr := makeCarRcvr <- ("Mercedes")
Ref.whenBroken(carRcvr, def lost(brokenRef) {
println("Lost connection to carRcvr")
}) |
def [resultVow, resolver] := Ref.promise()
when (resultVow) -> {
println(resultVow)
} catch prob {
println(`oops: $prob`)
}
resolver.resolve("this text is the answer") |
Remote Comm
Initialization |
introducer.onTheAir()
def makeURI(obj) {
return introducer.sturdyToURI(sturdyRef.temp(obj))
}
def makeFromURI(uri) {
return introducer.sturdyFromURI(uri).getRcvr()
} |
|
|