Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2/?] Add ability to run code examples in the playground: Extend code samples to be runnable #545

Open
wants to merge 60 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
765384b
feat(config): Activate mkdocs snippets extension
shaedrich Apr 21, 2024
c1a8a22
refactor(code-samples): Turn code samples into snippets
shaedrich Apr 21, 2024
58e6599
fix(config): Add missing colon
shaedrich Apr 21, 2024
62a001d
feat(config): Enable check_paths option for snippets
shaedrich Apr 21, 2024
c6b9959
fix(code-samples): :pencil2: Fix snippet file embed path
shaedrich Apr 21, 2024
dc86c15
Merge branch 'code-samples-separate-files' of https://github.com/shae…
shaedrich Apr 21, 2024
70dad88
refactor(code-samples): :memo: Turn code samples into snippets for "O…
shaedrich Apr 21, 2024
2464d02
refactor(code-samples): :memo: Turn code samples into snippets for "P…
shaedrich Apr 21, 2024
50e60d2
refactor(code-samples): :memo: Turn code samples into snippets for "T…
shaedrich Apr 21, 2024
676f5ee
fix(code-samples): :pencil2: Fix snippet file embed path
shaedrich Apr 21, 2024
c0a6b17
refactor(code-samples): :memo: Turn code samples into snippets for "G…
shaedrich Apr 21, 2024
003722b
fix(code-samples): :pencil2: Shorten snippet line syntax
shaedrich Apr 21, 2024
fd48fe3
refactor(code-samples): :memo: Turn code samples into snippets for "T…
shaedrich Apr 21, 2024
f983690
fix(code-samples): :pencil2: Fix snippet file embed path
shaedrich Apr 21, 2024
27e0623
fix(code-samples): :pencil2: Fix snippet file name
shaedrich Apr 21, 2024
154c7dc
cont'd
shaedrich Apr 21, 2024
cc8c939
refactor(code-samples): :memo: Turn code samples into snippets for "E…
shaedrich Apr 21, 2024
999c6c4
fix(code-samples): :pencil2: Fix snippet file name
shaedrich Apr 21, 2024
706c0d0
refactor(code-samples): :memo: Turn code samples into snippets for "R…
shaedrich Apr 22, 2024
cc1f53a
refactor(code-samples): :memo: Turn code samples into snippets for "G…
shaedrich Apr 22, 2024
bb74be7
fix(code-samples): :pencil2: Fix snippet file embed path
shaedrich Apr 22, 2024
2e6bc8a
fix(code-samples): :pencil2: Shorten snippet line syntax
shaedrich Apr 22, 2024
57bbbeb
refactor(code-samples): :memo: Turn code samples into snippets for "C…
shaedrich Apr 22, 2024
5b35814
refactor(code-samples): :memo: Turn code samples into snippets for "A…
shaedrich Apr 22, 2024
420d05a
fix(code-samples): :pencil2: Fix snippet file name
shaedrich Apr 22, 2024
1a22c1a
fix(code-samples): :pencil2: Fix snippet file embed path
shaedrich Apr 22, 2024
45180f5
.gitignore: + /.vscode
shaedrich Apr 22, 2024
db1eb11
refactor(code-samples): 📝 Revert non-pony code samples
shaedrich May 15, 2024
bbfe5a3
Make "classes" and "traits and interfaces" runnable
shaedrich May 20, 2024
61d9745
Make "type aliases" runnable
shaedrich May 21, 2024
03bc5e6
refactor(config): Enable code snippet dedention
shaedrich May 21, 2024
b59449e
feat(code-samples): Make "derived authority" runnable
shaedrich May 21, 2024
896f8cd
Merge branch 'main' into code-samples-extended-to-be-runnable
shaedrich May 21, 2024
d22204a
refactor(code-samples): Make "Reference capabilities" runnable
shaedrich May 21, 2024
c148739
fix(code-snippets): Typo
shaedrich May 21, 2024
a56e55d
feat(code-samples): Add latin-1 sample
shaedrich May 22, 2024
a3b8b67
Make "Literals" runnable
shaedrich May 23, 2024
88998a4
Make "literals" and "variables" runnable
shaedrich May 23, 2024
24fe11e
Make "ops" and "arithmetic" runnable
shaedrich May 24, 2024
8306f56
Make "control structures" runnable
shaedrich May 24, 2024
9e7d7f3
Make "match" runnable
shaedrich May 24, 2024
9c80361
Make "as" runnable
shaedrich May 24, 2024
6e71c4a
Make "methods" runnable
shaedrich May 25, 2024
d7302cd
Make "errors" runnable
shaedrich May 26, 2024
77ccaad
Make "sugar" runnable
shaedrich May 26, 2024
cdb59e6
Make "object literals" runnable
shaedrich May 26, 2024
9efb8ea
Make "examples" runnable
shaedrich May 27, 2024
c4cdf7d
Make "whitespace" runnable
shaedrich May 27, 2024
00bc87f
Make "annotations" runnable
shaedrich May 27, 2024
3c597a2
Make "Calling C" runnable
shaedrich May 27, 2024
191fc09
Making "C ABI" runnable
shaedrich May 27, 2024
0908c13
Make "Generics Overview" runnable
shaedrich May 29, 2024
ed0ca98
Move error message back into markdown file
shaedrich May 29, 2024
60308f4
Make "ponycheck" runnable
shaedrich May 29, 2024
93c3572
Make "gotchas" runnable
shaedrich May 29, 2024
f554f80
Fix whitespace
shaedrich May 29, 2024
0f8e225
Use stdlib examples directly from ponylang/ponyc repository
shaedrich May 29, 2024
ee5490b
Enable url_download option for snippets from stdlib
shaedrich Jun 4, 2024
51dca91
Revert "Enable url_download option for snippets from stdlib"
shaedrich Jun 4, 2024
f3c5552
Revert "Use stdlib examples directly from ponylang/ponyc repository"
shaedrich Jun 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions code-samples/aliasing-iso-to-tag.pony
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
fun test(a: Wombat iso) =>
var b: Wombat tag = a // Allowed!
actor Main
new create(env: Env) =>
test(Wombat)

fun test(a: Wombat iso) =>
var b: Wombat tag = a // Allowed!

class Wombat
10 changes: 8 additions & 2 deletions code-samples/aliasing-multiple-references-to-an-iso-object.pony
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
fun test(a: Wombat iso) =>
var b: Wombat iso = a // Not allowed!
actor Main
new create(env: Env) =>
test(Wombat)

fun test(a: Wombat iso) =>
var b: Wombat iso = a // Not allowed!

class Wombat
10 changes: 8 additions & 2 deletions code-samples/aliasing-trn-to-box.pony
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
fun test(a: Wombat trn) =>
var b: Wombat box = a // Allowed!
actor Main
new create(env: Env) =>
test(Wombat)

fun test(a: Wombat trn) =>
var b: Wombat box = a // Allowed!

class Wombat
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ class Foo
fun foo[A: Any](a: (A | Empty val)) =>
match consume a
| let a': A => None
end
end

actor Main
new create(env: Env) =>
let foo: Foo = Foo
env.out.print(foo.foo[Any]("Something").string())
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
if \likely\ cond then
foo
end
type T is (U32|U8)

while \unlikely\ cond then
bar
end
actor Main
new create(env: Env) =>
let foo = "foo"
let bar = "bar"
let baz = "baz"
var cond = true
let obj: U32 = 42
let expr: U32 = 42

if \likely\ cond then
foo
end

repeat
baz
until \likely\ cond end
cond = false
while \unlikely\ cond do
bar
end

match obj
| \likely\ expr => foo
| \unlikely\ let capt: T => bar
end
cond = true
repeat
baz
until \likely\ cond end

let res =
match obj
| \likely\ expr => foo
| \unlikely\ let capt: T => bar
end

env.out.print("res = " + res)
10 changes: 9 additions & 1 deletion code-samples/appendices-annotations-packed-annotation.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
struct \packed\ MyPackedStruct
var x: U8
var y: U32
var y: U32

new create() =>
x = 0
y = 1

actor Main
new create(env: Env) =>
env.out.print("{\n\t\"x\": " + MyPackedStruct.x.string() + ",\n\t\"y\": " + MyPackedStruct.y.string() + "\n}")
30 changes: 25 additions & 5 deletions code-samples/appendices-examples-create-arrays-with-values.pony
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
let dice: Array[U32] = [1; 2; 3
4
5
6
]
use "random"

actor Main
new create(env: Env) =>
let dice: Array[U32] = [1; 2; 3
4
5
6
]
Rand.shuffle[U32](dice)
for numberOfSpots in dice.values() do
env.out.print("You rolled a " + _ordinal(numberOfSpots))
end

fun _ordinal(number: U32): String =>
match number
| 1 => "one"
| 2 => "two"
| 3 => "three"
| 4 => "four"
| 5 => "five"
| 6 => "six"
else
"out of range"
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
use "format"
use "collections/persistent"

primitive Colours
fun black(): U32 => 0xFF000000
fun red(): U32 => 0xFFFF0000
fun red(): U32 => 0xFFFF0000

interface val Applyable
fun apply(): U32

actor Main
new create(env: Env) =>
let colorMap: Map[String, Applyable] = Map[String, Applyable].concat([
("red", Colours~red())
("black", Colours~black())
].values())

for (colorName, color) in colorMap.pairs() do
env.out.print(colorName + ": #" + Format.int[U32](color(), FormatHexBare))
end
19 changes: 18 additions & 1 deletion code-samples/appendices-examples-enumeration-with-values.pony
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
use "format"
use "collections/persistent"

primitive Black fun apply(): U32 => 0xFF000000
primitive Red fun apply(): U32 => 0xFFFF0000
primitive Red fun apply(): U32 => 0xFFFF0000

type Color is (Red | Black)

actor Main
new create(env: Env) =>

let colorMap: Map[String, Color] = Map[String, Color].concat([
("red", Red)
("black", Black)
].values())

for (colorName, color) in colorMap.pairs() do
env.out.print(colorName + ": #" + Format.int[U32](color(), FormatHexBare))
end
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
a
-b
use "format"
use "collections/persistent"

primitive Black fun apply(): U32 => 0xFF000000
primitive Red fun apply(): U32 => 0xFFFF0000

type Color is (Red | Black)

actor Main
new create(env: Env) =>
let a: I8 = 1
let b: I8 = 3
let c: I8 =
a
-b
env.out.print(c.string())
16 changes: 15 additions & 1 deletion code-samples/appendices-whitespace-subtract-b-from-a.pony
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
a - b
use "format"
use "collections/persistent"

primitive Black fun apply(): U32 => 0xFF000000
primitive Red fun apply(): U32 => 0xFFFF0000

type Color is (Red | Black)

actor Main
new create(env: Env) =>
let a: I8 = 1
let b: I8 = 3
let c: I8 =
a - b
env.out.print(c.string())
41 changes: 25 additions & 16 deletions code-samples/arithmetic-partial-and-check-arithmetic.pony
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// partial arithmetic
let result =
try
USize.max_value() +? env.args.size()
else
env.out.print("overflow detected")
end
actor Main
new create(env: Env) =>
partial(env)
checked(env)

fun partial(env: Env) =>
// partial arithmetic
let result =
try
USize.max_value() +? env.args.size()
else
env.out.print("overflow detected")
end

// checked arithmetic
let result =
match USize.max_value().addc(env.args.size())
| (let result: USize, false) =>
// use result
...
| (_, true) =>
env.out.print("overflow detected")
end
fun checked(env: Env) =>
// checked arithmetic
let result =
match USize.max_value().addc(env.args.size())
| (let result: USize, false) =>
// use result
env.out.print(result.string())/*
...
*/
| (_, true) =>
env.out.print("overflow detected")
end
10 changes: 10 additions & 0 deletions code-samples/as-operator-match-statement-comparison.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
interface Critter
fun wash(): String

class Wombat is Critter
fun wash(): String => "I'm a clean wombat!"

class Capybara is Critter
fun wash(): String => "I feel squeaky clean!"
fun swim(): String => "I'm swimming like a fish!"

actor Main
new create(env: Env) =>
let anys = Array[Any ref].>push(Wombat).>push(Capybara)
Expand Down
10 changes: 10 additions & 0 deletions code-samples/as-operator-match-statement-without-try.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
interface Critter
fun wash(): String

class Wombat is Critter
fun wash(): String => "I'm a clean wombat!"

class Capybara is Critter
fun wash(): String => "I feel squeaky clean!"
fun swim(): String => "I'm swimming like a fish!"

actor Main
new create(env: Env) =>
let anys = Array[Any ref].>push(Wombat).>push(Capybara)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
interface Critter
fun wash(): String

class Wombat is Critter
fun wash(): String => "I'm a clean wombat!"

class Capybara is Critter
fun wash(): String => "I feel squeaky clean!"
fun swim(): String => "I'm swimming like a fish!"

actor Main
new create(env: Env) =>
let anys = Array[Any ref].>push(Wombat).>push(Capybara)
Expand Down
29 changes: 22 additions & 7 deletions code-samples/as-operator-unrelated-type.pony
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
trait Alive
trait Alive

trait Well
trait Well

class Person is (Alive & Well)
class Person is (Alive & Well)

class LifeSigns
fun is_all_good(alive: Alive)? =>
// if the instance 'alive' is also of type 'Well' (such as a Person instance). raises error if not possible
let well: Well = alive as Well
class LifeSigns
fun is_all_good(alive: Alive)? =>
// if the instance 'alive' is also of type 'Well' (such as a Person instance). raises error if not possible
let well: Well = alive as Well

class Dog is Alive

actor Main
new create(env: Env) =>
try
LifeSigns.is_all_good(Person)?
else
env.err.print("Person is alive but not well")
end
try
LifeSigns.is_all_good(Dog)?
else
env.err.print("Dog is alive but not well")
end
30 changes: 17 additions & 13 deletions code-samples/c-abi-jump-consistent-hashing.pony
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Jump consistent hashing in Pony, with an inline pseudo random generator
// https://arxiv.org/abs/1406.2294
actor Main
new create(env: Env) =>
env.out.print("jch: " + jch(U64(10), U32(20)).string())

fun jch(key: U64, buckets: U32): I32 =>
var k = key
var b = I64(0)
var j = I64(0)
// Jump consistent hashing in Pony, with an inline pseudo random generator
// https://arxiv.org/abs/1406.2294

while j < buckets.i64() do
b = j
k = (k * 2862933555777941757) + 1
j = ((b + 1).f64() * (I64(1 << 31).f64() / ((k >> 33) + 1).f64())).i64()
end

b.i32()
fun jch(key: U64, buckets: U32): I32 =>
var k = key
var b = I64(0)
var j = I64(0)

while j < buckets.i64() do
b = j
k = (k * 2862933555777941757) + 1
j = ((b + 1).f64() * (I64(1 << 31).f64() / ((k >> 33) + 1).f64())).i64()
end

b.i32()