-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
133 changed files
with
1,244 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
code-samples/aliasing-multiple-references-to-an-iso-object.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 29 additions & 13 deletions
42
code-samples/appendices-annotations-likely-and-unlikely-annotations.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
code-samples/appendices-examples-create-arrays-with-values.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
19 changes: 18 additions & 1 deletion
19
code-samples/appendices-examples-enumeration-with-values-with-namespace.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
code-samples/appendices-examples-enumeration-with-values.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
18 changes: 16 additions & 2 deletions
18
code-samples/appendices-whitespace-do-a-then-do-a-unary-negation-of-b.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
code-samples/arithmetic-partial-and-check-arithmetic.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
code-samples/as-operator-more-specific-interface-with-reference-capability.pony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.