Skip to content

Commit

Permalink
Extend code samples to be runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
shaedrich committed Jun 25, 2024
1 parent caae6e2 commit 1adc931
Show file tree
Hide file tree
Showing 133 changed files with 1,244 additions and 449 deletions.
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()
Loading

0 comments on commit 1adc931

Please sign in to comment.