# Code Samples

> Small runnable programs for Elide, each with its command and expected output

Each sample is one file; run it in an empty directory.

## TypeScript

```ts
// discount.ts
interface Order {
  price: number;
  quantity: number;
}

const order: Order = { price: 79.99, quantity: 2 };
console.log(`$${(order.price * order.quantity).toFixed(2)}`);
```

```bash
elide run discount.ts
```

```text
$159.98
```

## Embedded SQLite

```ts
// database.ts
import { Database } from "elide:sqlite";

const db = new Database();
db.exec("CREATE TABLE users (name TEXT)");
db.exec("INSERT INTO users VALUES ('Ada')");
console.log(db.query("SELECT name FROM users").get().name);
```

```bash
elide run database.ts
```

```text
Ada
```

## HTTP handler

```ts
// server.ts
export default {
  async fetch(request: Request): Promise<Response> {
    const url = new URL(request.url);
    return Response.json({ path: url.pathname });
  },
};
```

```bash
elide serve --no-tui server.ts
curl http://127.0.0.1:8080/hello
```

```text
{"path":"/hello"}
```

## Kotlin

```kotlin
// Inventory.kt
data class Item(val name: String, val count: Int)

fun main() {
  val items = listOf(Item("bolts", 120), Item("nuts", 80), Item("washers", 0))
  val inStock = items.filter { it.count > 0 }.joinToString { it.name }
  println("In stock: $inStock")
}
```

```bash
elide run Inventory.kt
```

```text
In stock: bolts, nuts
```

## Python calling JavaScript

Needs the Python component and `--interop`.

```python
# interop.py
from elide import js

total = js("[1, 2, 3, 4].reduce((a, b) => a + b, 0)")
print(total)
```

```bash
elide --interop python interop.py
```

```text
10
```

## JavaScript importing Python

Needs the Python component and `--interop`.

```python
# greet.py
def hello():
    return "Hello from Python"
```

```javascript
// main.mjs
import greet from "./greet.py";
console.log(greet.hello());
```

```bash
elide --interop run main.mjs
```

```text
Hello from Python
```
