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

Changed lambda literal syntax to be more concise (issue #1391) #1400

Merged
merged 2 commits into from
Nov 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/lambda/lambda.pony
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Simple example of creating and calling a lambda function.
actor Main
new create(env: Env) =>
// First let's pass an add function.
let x = f(lambda(a: U32, b: U32): U32 => a + b end, 6)
let x = f({(a: U32, b: U32): U32 => a + b }, 6)
env.out.print("Add: " + x.string())

// Now a multiply.
let y = f(lambda(a: U32, b: U32): U32 => a * b end, 6)
let y = f({(a: U32, b: U32): U32 => a * b }, 6)
env.out.print("Mult: " + y.string())

// And finally a lambda that raises an error.
let z = f(lambda(a: U32, b: U32): U32 ? => error end, 6)
let z = f({(a: U32, b: U32): U32 ? => error }, 6)
env.out.print("Error: " + z.string())

fun f(fn: {(U32, U32): U32 ?} val, x: U32): U32 =>
Expand Down
6 changes: 3 additions & 3 deletions packages/builtin/array.pony
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class Array[A] is Seq[A]

fun find(value: A!, offset: USize = 0, nth: USize = 0,
predicate: {(box->A!, box->A!): Bool} val =
lambda(l: box->A!, r: box->A!): Bool => l is r end): USize ?
{(l: box->A!, r: box->A!): Bool => l is r }): USize ?
=>
"""
Find the `nth` appearance of `value` from the beginning of the array,
Expand Down Expand Up @@ -403,7 +403,7 @@ class Array[A] is Seq[A]
error

fun contains(value: A!, predicate: {(box->A!, box->A!): Bool} val =
lambda(l: box->A!, r: box->A!): Bool => l is r end): Bool =>
{(l: box->A!, r: box->A!): Bool => l is r }): Bool =>
"""
Returns true if the array contains `value`, false otherwise.
"""
Expand All @@ -420,7 +420,7 @@ class Array[A] is Seq[A]

fun rfind(value: A!, offset: USize = -1, nth: USize = 0,
predicate: {(box->A!, box->A!): Bool} val =
lambda(l: box->A!, r: box->A!): Bool => l is r end): USize ?
{(l: box->A!, r: box->A!): Bool => l is r }): USize ?
=>
"""
Find the `nth` appearance of `value` from the end of the array, starting at
Expand Down
98 changes: 49 additions & 49 deletions packages/builtin_test/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class iso _TestStringToBool is UnitTest
h.assert_eq[Bool](true, "true".bool())
h.assert_eq[Bool](true, "TRUE".bool())

h.assert_error(lambda()? => "bogus".bool() end)
h.assert_error({()? => "bogus".bool() })


class iso _TestStringToFloat is UnitTest
Expand Down Expand Up @@ -191,9 +191,9 @@ class iso _TestStringToU8 is UnitTest
h.assert_eq[U8](123, "0123".u8())
h.assert_eq[U8](89, "089".u8())

h.assert_error(lambda()? => "300".u8() end, "U8 300")
h.assert_error(lambda()? => "30L".u8() end, "U8 30L")
h.assert_error(lambda()? => "-10".u8() end, "U8 -10")
h.assert_error({()? => "300".u8() }, "U8 300")
h.assert_error({()? => "30L".u8() }, "U8 30L")
h.assert_error({()? => "-10".u8() }, "U8 -10")

h.assert_eq[U8](16, "0x10".u8())
h.assert_eq[U8](31, "0x1F".u8())
Expand All @@ -203,10 +203,10 @@ class iso _TestStringToU8 is UnitTest
h.assert_eq[U8](2, "0B10".u8())
h.assert_eq[U8](0x8A, "0b1000_1010".u8())

h.assert_error(lambda()? => "1F".u8() end, "U8 1F")
h.assert_error(lambda()? => "0x".u8() end, "U8 0x")
h.assert_error(lambda()? => "0b3".u8() end, "U8 0b3")
h.assert_error(lambda()? => "0d4".u8() end, "U8 0d4")
h.assert_error({()? => "1F".u8() }, "U8 1F")
h.assert_error({()? => "0x".u8() }, "U8 0x")
h.assert_error({()? => "0b3".u8() }, "U8 0b3")
h.assert_error({()? => "0d4".u8() }, "U8 0d4")


class iso _TestStringToI8 is UnitTest
Expand All @@ -222,8 +222,8 @@ class iso _TestStringToI8 is UnitTest
h.assert_eq[I8](89, "089".i8())
h.assert_eq[I8](-10, "-10".i8())

h.assert_error(lambda()? => "200".i8() end, "I8 200")
h.assert_error(lambda()? => "30L".i8() end, "I8 30L")
h.assert_error({()? => "200".i8() }, "I8 200")
h.assert_error({()? => "30L".i8() }, "I8 30L")

h.assert_eq[I8](16, "0x10".i8())
h.assert_eq[I8](31, "0x1F".i8())
Expand All @@ -234,10 +234,10 @@ class iso _TestStringToI8 is UnitTest
h.assert_eq[I8](0x4A, "0b100_1010".i8())
h.assert_eq[I8](-0x4A, "-0b100_1010".i8())

h.assert_error(lambda()? => "1F".i8() end, "U8 1F")
h.assert_error(lambda()? => "0x".i8() end, "U8 0x")
h.assert_error(lambda()? => "0b3".i8() end, "U8 0b3")
h.assert_error(lambda()? => "0d4".i8() end, "U8 0d4")
h.assert_error({()? => "1F".i8() }, "U8 1F")
h.assert_error({()? => "0x".i8() }, "U8 0x")
h.assert_error({()? => "0b3".i8() }, "U8 0b3")
h.assert_error({()? => "0d4".i8() }, "U8 0d4")


class iso _TestStringToIntLarge is UnitTest
Expand All @@ -249,45 +249,45 @@ class iso _TestStringToIntLarge is UnitTest
fun apply(h: TestHelper) ? =>
h.assert_eq[U16](0, "0".u16())
h.assert_eq[U16](123, "123".u16())
h.assert_error(lambda()? => "-10".u16() end, "U16 -10")
h.assert_error(lambda()? => "65536".u16() end, "U16 65536")
h.assert_error(lambda()? => "30L".u16() end, "U16 30L")
h.assert_error({()? => "-10".u16() }, "U16 -10")
h.assert_error({()? => "65536".u16() }, "U16 65536")
h.assert_error({()? => "30L".u16() }, "U16 30L")

h.assert_eq[I16](0, "0".i16())
h.assert_eq[I16](123, "123".i16())
h.assert_eq[I16](-10, "-10".i16())
h.assert_error(lambda()? => "65536".i16() end, "I16 65536")
h.assert_error(lambda()? => "30L".i16() end, "I16 30L")
h.assert_error({()? => "65536".i16() }, "I16 65536")
h.assert_error({()? => "30L".i16() }, "I16 30L")

h.assert_eq[U32](0, "0".u32())
h.assert_eq[U32](123, "123".u32())
h.assert_error(lambda()? => "-10".u32() end, "U32 -10")
h.assert_error(lambda()? => "30L".u32() end, "U32 30L")
h.assert_error({()? => "-10".u32() }, "U32 -10")
h.assert_error({()? => "30L".u32() }, "U32 30L")

h.assert_eq[I32](0, "0".i32())
h.assert_eq[I32](123, "123".i32())
h.assert_eq[I32](-10, "-10".i32())
h.assert_error(lambda()? => "30L".i32() end, "I32 30L")
h.assert_error({()? => "30L".i32() }, "I32 30L")

h.assert_eq[U64](0, "0".u64())
h.assert_eq[U64](123, "123".u64())
h.assert_error(lambda()? => "-10".u64() end, "U64 -10")
h.assert_error(lambda()? => "30L".u64() end, "U64 30L")
h.assert_error({()? => "-10".u64() }, "U64 -10")
h.assert_error({()? => "30L".u64() }, "U64 30L")

h.assert_eq[I64](0, "0".i64())
h.assert_eq[I64](123, "123".i64())
h.assert_eq[I64](-10, "-10".i64())
h.assert_error(lambda()? => "30L".i64() end, "I64 30L")
h.assert_error({()? => "30L".i64() }, "I64 30L")

h.assert_eq[U128](0, "0".u128())
h.assert_eq[U128](123, "123".u128())
h.assert_error(lambda()? => "-10".u128() end, "U128 -10")
h.assert_error(lambda()? => "30L".u128() end, "U128 30L")
h.assert_error({()? => "-10".u128() }, "U128 -10")
h.assert_error({()? => "30L".u128() }, "U128 30L")

h.assert_eq[I128](0, "0".i128())
h.assert_eq[I128](123, "123".i128())
h.assert_eq[I128](-10, "-10".i128())
h.assert_error(lambda()? => "30L".i128() end, "I128 30L")
h.assert_error({()? => "30L".i128() }, "I128 30L")


class iso _TestStringLstrip is UnitTest
Expand Down Expand Up @@ -933,7 +933,7 @@ class iso _TestArrayInsert is UnitTest
c.insert(2, "four")
h.assert_array_eq[String](["one", "three", "four"], c)

h.assert_error(lambda()? => ["one", "three"].insert(3, "invalid") end)
h.assert_error({()? => ["one", "three"].insert(3, "invalid") })

class iso _TestArrayValuesRewind is UnitTest
"""
Expand Down Expand Up @@ -975,41 +975,41 @@ class iso _TestArrayFind is UnitTest
h.assert_eq[USize](1, a.find(1))
h.assert_eq[USize](5, a.find(1 where offset = 3))
h.assert_eq[USize](5, a.find(1 where nth = 1))
h.assert_error(lambda()(a)? => a.find(6) end)
h.assert_error({()(a)? => a.find(6) })
h.assert_eq[USize](2, a.find(1 where
predicate = lambda(l: ISize, r: ISize): Bool => l > r end))
predicate = {(l: ISize, r: ISize): Bool => l > r }))
h.assert_eq[USize](0, a.find(0 where
predicate = lambda(l: ISize, r: ISize): Bool => (l % 3) == r end))
predicate = {(l: ISize, r: ISize): Bool => (l % 3) == r }))
h.assert_eq[USize](3, a.find(0 where
predicate = lambda(l: ISize, r: ISize): Bool => (l % 3) == r end,
nth = 1))
h.assert_error(lambda()(a)? => a.find(0 where
predicate = lambda(l: ISize, r: ISize): Bool => (l % 3) == r end,
nth = 2) end)
predicate = {(l: ISize, r: ISize): Bool => (l % 3) == r }, nth = 1))
h.assert_error({()(a)? =>
a.find(0 where
predicate = {(l: ISize, r: ISize): Bool => (l % 3) == r }, nth = 2)
})

h.assert_eq[USize](5, a.rfind(1))
h.assert_eq[USize](1, a.rfind(1 where offset = 3))
h.assert_eq[USize](1, a.rfind(1 where nth = 1))
h.assert_error(lambda()(a)? => a.rfind(6) end)
h.assert_error({()(a)? => a.rfind(6) })
h.assert_eq[USize](4, a.rfind(1 where
predicate = lambda(l: ISize, r: ISize): Bool => l > r end))
predicate = {(l: ISize, r: ISize): Bool => l > r }))
h.assert_eq[USize](3, a.rfind(0 where
predicate = lambda(l: ISize, r: ISize): Bool => (l % 3) == r end))
predicate = {(l: ISize, r: ISize): Bool => (l % 3) == r }))
h.assert_eq[USize](0, a.rfind(0 where
predicate = lambda(l: ISize, r: ISize): Bool => (l % 3) == r end,
nth = 1))
h.assert_error(lambda()(a)? => a.rfind(0 where
predicate = lambda(l: ISize, r: ISize): Bool => (l % 3) == r end,
nth = 2) end)
predicate = {(l: ISize, r: ISize): Bool => (l % 3) == r }, nth = 1))
h.assert_error({()(a)? =>
a.rfind(0 where
predicate = {(l: ISize, r: ISize): Bool => (l % 3) == r }, nth = 2)
})

var b = Array[_FindTestCls]
let c = _FindTestCls
b.push(c)
h.assert_error(lambda()(b)? => b.find(_FindTestCls) end)
h.assert_error({()(b)? => b.find(_FindTestCls) })
h.assert_eq[USize](0, b.find(c))
h.assert_eq[USize](0, b.find(_FindTestCls where
predicate = lambda(l: _FindTestCls box, r: _FindTestCls box): Bool =>
l == r end))
predicate = {(l: _FindTestCls box, r: _FindTestCls box): Bool => l == r }
))


class iso _TestMath128 is UnitTest
Expand Down Expand Up @@ -1236,7 +1236,7 @@ class iso _TestMaybePointer is UnitTest
let a = MaybePointer[_TestStruct].none()
h.assert_true(a.is_none())

h.assert_error(lambda()(a)? => let from_a = a() end)
h.assert_error({()(a)? => let from_a = a() })

let s = _TestStruct
s.i = 7
Expand Down
9 changes: 3 additions & 6 deletions packages/bureaucracy/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ class iso _TestRegistrar is UnitTest
let r = Registrar
r("test") = _TestDisposable(h)

r[_TestDisposable]("test").next[None](
recover
lambda(value: _TestDisposable)(h) =>
value.dispose()
end
end)
r[_TestDisposable]("test").next[None]({(value: _TestDisposable)(h) =>
value.dispose()
} iso)

actor _TestDisposable
let _h: TestHelper
Expand Down
Loading