Skip to content

Commit 9a5d225

Browse files
committed
chore: rewrites to top polyglot 101 page
Signed-off-by: Sam Gammon <[email protected]>
1 parent 783f871 commit 9a5d225

File tree

1 file changed

+69
-47
lines changed

1 file changed

+69
-47
lines changed

Writerside/topics/Polyglot.md

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,44 @@ does that mean? This guide explains how multiple languages can work in tandem to
99
## Starting Imperatively
1010

1111
Let's start with a simple imperative example of how multiple languages might work together. We could begin with some
12-
JavaScript:
12+
Python:
1313

14-
```Javascript
15-
export function sayHello(name) {
16-
console.log(`Hello, ${name}!`);
17-
}
14+
```Python
15+
# sample.py
16+
17+
def say_hello(name = "Python"):
18+
"""Say hello to someone or something."""
19+
return f"Hello, {name}!"
1820
```
1921

20-
We've defined a function here, which takes a `name` parameter; we format that and print it. Easy. Now let's imagine our
21-
magic runtime can shift to Python to call that code:
22+
Now let's save that file and create a new one. This time, in TypeScript:
2223

23-
```Python
24-
from "./my-js.mjs" import sayHello as say_hello
24+
```TypeScript
25+
// sample.ts
26+
import py from "./sample.py"
2527

26-
def call_hello():
27-
"""Call the `sayHello` function in JavaScript, from Python."""
28-
say_hello("%product%")
28+
console.log(`${py.say_hello()} + TypeScript!`)
2929
```
3030

31-
> This demo does not work yet; please bear with us while our features and docs catch up to each other.
32-
> {style="warning"}
31+
We've defined some code in Python, and now we are **importing it into TypeScript**. This code is runnable with Elide:
3332

34-
This is pretty easy to follow so far. We've defined that `sayHello` function in JavaScript, and now we are calling that
35-
same function from a new function, called `callHello`, in Python.
33+
```Console
34+
> elide sample.ts
35+
```
36+
```Console
37+
Hello, Python + TypeScript!
38+
```
3639

3740
## But why?
3841

39-
Have you ever wanted to write a Python app which could host a React UI? How about an application that's built in Ruby;
40-
maybe you want to do some image processing and you miss your PIL?
42+
Have you ever wanted to write a Python app which could host a React UI?
4143

4244
There are tons of cases where one language is good at something, another is good at something else, and your app has to
4345
do both. %product% is what bridges this gap.
4446

4547
## Isn't that inefficient?
4648

47-
That depends! %product% certainly behaves differently from, say, Node, but %product% handily beats Node and even Bun on
48-
several key benchmarks. Just like Bun vs. Node, or CPython vs. PyPy, performance isn't necessarily apples-to-apples.
49-
50-
But, since you may be wondering, in the code sample above 👆
49+
%product% outperforms Node, Deno, _and_ CPython. But, since you may be wondering, in the code sample above 👆
5150

5251
- **There is just one process.** No, %product% isn't calling out to Node with your source code, and then to Python, and
5352
stitching between with JSON or something. That _would_ be gross. There are systems that do this; Elide is not one
@@ -63,26 +62,46 @@ But, since you may be wondering, in the code sample above 👆
6362

6463
Skeptical? Good. The best engineers often are. Let's beat the serialization allegations:
6564

66-
```Javascript
67-
export function sayHello(nameGetter) {
68-
console.log(`Hello, ${nameGetter()}!`);
69-
}
70-
```
65+
```Python
66+
# sample.py
7167

72-
And:
68+
def say_hello(name_getter):
69+
return f"Hello, {name_getter()}!"
7370

74-
```Python
75-
from "./my-js.mjs" import sayHelloWithGetter as say_hello
71+
def say_goodbye(name_getter):
72+
return f"Goodbye, {name_getter()}!"
7673

77-
def get_name():
78-
return "Elide"
74+
def render_message(leaving = False):
75+
if leaving: return say_goodbye
76+
return say_hello
77+
```
7978

80-
def call_hello_look_ma_try_this_with_serialization():
81-
say_hello(get_name)
79+
And:
80+
81+
```TypeScript
82+
// sample.ts
83+
import py from "./sample.py"
84+
85+
const helloName = () => "Elide"
86+
const goodbyeName = () => "confining runtimes"
87+
88+
console.log(
89+
// will render a hello message, passing functions between langs
90+
py.render_message()(helloName)
91+
)
92+
console.log(
93+
// will render a goodbye message
94+
py.render_message(true)(goodbyeName)
95+
)
8296
```
8397

84-
> This demo does not work yet; please bear with us while our features and docs catch up to each other.
85-
> {style="warning"}
98+
```Console
99+
> elide sample.ts
100+
```
101+
```Console
102+
Hello, Elide!
103+
Goodbye, confining runtimes!
104+
```
86105

87106
%product% will happily run this code. This is impossible with JSON, or Protobuf, or any other trickery on top of Node.js
88107
or CPython.
@@ -105,22 +124,25 @@ Now that we are thinking in a roughly-polyglot way, let's test the waters with s
105124
[Exercise: Filesystem Basics](101-Filesystem.md)
106125
: Read a file using Node.js built-ins ([`node:fs`](https://nodejs.org/api/fs.html)).
107126

127+
[Exercise: Debugging](101-Debugging.md)
128+
: Step across language boundaries in a debugger
129+
130+
[Exercise: Interop](101-Polyglot-Context.md)
131+
: Get familiar with cross-lang interop
132+
133+
[Exercise: Servers](101-Servers.md)
134+
: Writing polyglot servers with %product%
135+
108136
## Benchmarking
109137

110138
%product% makes use of language runtimes from the [GraalVM](https://graalvm.org) team which are benchmarked extensively
111139
and continuously. People often ask if they can replicate these benchmarks. The answer is yes, you are encouraged to!
112140

113141
There are some tools we can recommend:
114142

115-
| Tool | Great for | Example invocation |
116-
|-----------------------------------------------------|--------------------------------|-----------------------------------------------------------------------|
117-
| [`hyperfine`](https://github.com/sharkdp/hyperfine) | CLI-invocable micro-benchmarks | [Using Hyperfine for benchmarks](#benchmarking-with-hyperfine) |
118-
| [`wrk2`]( https://github.com/giltene/wrk2) | Server benchmarking | [Using wrk2 to benchmark %product%'s server](#benchmarking-with-wrk2) |
119-
120-
### Benchmarking with Hyperfine
121-
122-
Coming soon.
123-
124-
### Benchmarking with wrk2
143+
| Tool | Great for |
144+
|-----------------------------------------------------|--------------------------------|
145+
| [`hyperfine`](https://github.com/sharkdp/hyperfine) | CLI-invocable micro-benchmarks |
146+
| [`wrk2`]( https://github.com/giltene/wrk2) | Server benchmarking |
125147

126-
Coming soon.
148+
Read more about %product%'s [](Performance.md)

0 commit comments

Comments
 (0)