# Monte Idioms Quick Reference¶

These examples show Monte syntax for conventional constructs as well as workhorse idioms that are somewhat novel to Monte.

## Simple Statements¶

```>>> def a := 2 + 3
... var a2 := 4
... a2 += 1
... def b := `answer: \$a`
... traceln(b)
... b
```

## Basic Flow¶

```>>> if ('a' == 'b'):
...    "match"
... else:
...    "no match"
"no match"
```
```>>> var a := 0; def b := 4
... while (a < b):
...     a += 1
... a
4
```
```>>> var resource := "reserved"
... try:
...     3 // 0
... catch err:
...     `error!`
... finally:
...     resource := "released"
... resource
"released"
```
```>>> def x := [].diverge()
... for next in (1..3):
...     x.push([next, next])
... x.snapshot()
[[1, 1], [2, 2], [3, 3]]
```
```>>> def map := ['a' => 65, 'b' => 66]
... var sum := 0
... for key => value in (map):
...     sum += value
... sum
131
```

### Function¶

```>>> def addTwoPrint(number):
...     traceln(number + 2)
...     return number + 2
...
... twoPlusThree
5
```

### Singleton Object (stateless)¶

```>>> object adder:
...         return number + 1
...         return number + 2
... result
4
```

### Objects with state¶

```>>> def makeOperator(baseNum):
...     def instanceValue := 3
...     object operator:
...             return baseNum + number
...         to multiplyBase(number):
...             return baseNum * number
...     return operator
... def threeHandler := makeOperator(3)
... def threeTimes2 := threeHandler.multiplyBase(2)
... threeTimes2
6
```

### Objects self-referencing during construction¶

```>>> def makeRadio(car):
... def makeCar(name):
...     var x := 0
...     var y := 0
...     def car # using def with no assignment
...     bind car:
...             # ....process the weather report....
...         to getX():
...             return x
...         to getY():
...             return y
...         # ....list the rest of the car methods....
...     return car
... makeCar("ferrari").getX()
0
```

### Delegation¶

```>>> def makeExtendedFile(myFile):
...     return object extendedFile extends myFile:
...         to append(text):
...             var current := myFile.getText()
...             current := current + text
...             myFile.setText(current)
...
... makeExtendedFile(object _ {})._respondsTo("append", 1)
true
```

## File I/O and Modules¶

```>>> def main(argv, => makeFileResource):
...     def fileA := makeFileResource("fileA")
...     fileA <- setContents(b`abc\ndef`)
...     def contents := fileA <- getContents()
...     when (contents) ->
...         for line in (contents.split("\n")):
...             traceln(line)
...
... main._respondsTo("run", 1)
true
```

## Web Applications¶

Access to TCP/IP networking is also given to the main entry point. The `http/server` module builds an HTTP server from a TCP/IP listener:

```import "http/server" =~ [=> makeHTTPEndpoint :DeepFrozen]
exports (main)

def hello(request) as DeepFrozen:
return [200, ["Content-Type" => "text/plain"], b`hello`]

def main(argv, => makeTCP4ServerEndpoint) as DeepFrozen:
def tcpListener := makeTCP4ServerEndpoint(8080)
def httpServer := makeHTTPEndpoint(tcpListener)
httpServer.listen(hello)
```

## Data Structures¶

### ConstList¶

```>>> var a := [8, 6, "a"]
... a[2]
"a"

>>> var a := [8, 6, "a"]
... a.size()
3

>>> var a := [8, 6, "a"]
... for i in (a):
...     traceln(i)
... a := a + ["b"]
... a.slice(0, 2)
[8, 6]
```

### ConstMap¶

```>>> def m := ["c" => 5]
... m["c"]
5

>>> ["c" => 5].size()
1

>>> def m := ["c" => 5]
... for key => value in (m):
...     traceln(value)
... def flexM := m.diverge()
... flexM["d"] := 6
... flexM.size()
2
```

### FlexList¶

```>>> def flexA := [8, 6, "a", "b"].diverge()
... flexA.extend(["b"])
... flexA.push("b")
... def constA := flexA.snapshot()
[8, 6, "a", "b", "b", "b"]
```

### FlexMap¶

```>>> def m := ["c" => 5]
... def flexM := m.diverge()
... flexM["b"] := 2
... flexM.removeKey("b")
... def constM := flexM.snapshot()
["c" => 5]
```

## Eventual Sends¶

```>>> def abacus := object mock { to add(x, y) { return x + y } }
... var out := null
...
3
```
```>>> def makeCarRcvr := fn autoMake { `shiny \$autoMake` }
...
... def carRcvr := makeCarRcvr <- ("Mercedes")
... Ref.whenBroken(carRcvr, def lost(brokenRef) {
...     traceln("Lost connection to carRcvr")
... })
... carRcvr
"shiny Mercedes"

>>> def [resultVow, resolver] := Ref.promise()
...
... when (resultVow) ->
...     traceln(resultVow)
... catch prob:
...     traceln(`oops: \$prob`)
...
... resolver.resolve("this text is the answer")
... resultVow