Replies: 7 comments 4 replies
-
You can use lambdas: main:
lambdas := [
:: print "hello",
:: print "world",
]
lambdas.do: | fun/Lambda |
fun.call |
Beta Was this translation helpful? Give feedback.
0 replies
-
What if the function body includes multiple lines or actions, not just
print?
…On Tue, Jan 7, 2025, 11:21 Florian Loitsch ***@***.***> wrote:
You can use lambdas:
main:
lambdas := [
:: print "hello",
:: print "world",
]
lambdas.do: | fun/Lambda |
fun.call
—
Reply to this email directly, view it on GitHub
<#2689 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A54VSXOGX3NPECGP2JHPBA32JOMC3AVCNFSM6AAAAABUXJCJVKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZVHA4TMOI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
-
I like this better:
a1 -> none :
print "A1"
a2 -> none :
print "A2"
a3 -> none :
print "A3"
main :
lambdas := [
:: a1, :: a2, :: a3
]
lambdas.do : | f/Lambda |
f.call
But in any case thanks a lot!
…On Tue, Jan 7, 2025, 11:28 Florian Loitsch ***@***.***> wrote:
You can either use indentation, or call a function outside.
I also added (as example) parameters to the lambdas.
foo x y:
print x
print y
main:
lambdas := [
:: | x y | foo x y, // Call other function. :: | x y | // Inline with indentation. print x
print y
]
lambdas.do: | fun/Lambda |
fun.call "hello" "world"
—
Reply to this email directly, view it on GitHub
<#2689 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A54VSXORBOGTA3VSOXVWE3T2JOM47AVCNFSM6AAAAABUXJCJVKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZVHEYDMMA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
-
Ok
…On Tue, Jan 7, 2025, 12:01 Florian Loitsch ***@***.***> wrote:
You can also assign the lambdas to local variables first:
main:
fun1 := :: foo
fun2 := :: bar
...
lambdas := [ fun1, fun2, ... ]
—
Reply to this email directly, view it on GitHub
<#2689 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A54VSXO3XLT2ASYWGSWFJS32JOQWPAVCNFSM6AAAAABUXJCJVKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZVHE2DKMA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Will this whole construction work if both functions and lambdas list are
inside some class?
…On Tue, Jan 7, 2025, 12:01 michael sure8 ***@***.***> wrote:
Ok
On Tue, Jan 7, 2025, 12:01 Florian Loitsch ***@***.***>
wrote:
> You can also assign the lambdas to local variables first:
>
> main:
> fun1 := :: foo
> fun2 := :: bar
> ...
> lambdas := [ fun1, fun2, ... ]
>
> —
> Reply to this email directly, view it on GitHub
> <#2689 (reply in thread)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/A54VSXO3XLT2ASYWGSWFJS32JOQWPAVCNFSM6AAAAABUXJCJVKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZVHE2DKMA>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
|
Beta Was this translation helpful? Give feedback.
1 reply
-
I've found inside a class, the vector of lambda functions cannot be used
due initialization error.
```
// define some class
class Some :
ab1 -> none :
print "Ab1"
ab2 -> none :
print "Ab2"
ab3 -> none :
print "Ab3"
list_/List := [
:: ab1, :: ab2, :: ab3
]
done :
list_.do : | fun/Lambda |
fun.call
/*
test_lambdas.toit:15:9: error: Can't access instance members in field
initializers.
:: ab1, :: ab2, :: ab3
^~~
test_lambdas.toit:15:17: error: Can't access instance members in field
initializers.
:: ab1, :: ab2, :: ab3
^~~
test_lambdas.toit:15:25: error: Can't access instance members in field
initializers.
:: ab1, :: ab2, :: ab3
^~~
Compilation failed.
*/
a1 -> none :
print "A1"
a2 -> none :
print "A2"
a3 -> none :
print "A3"
main :
lambdas := [
:: a1, :: a2, :: a3
]
lambdas.do : | fun/Lambda |
fun.call
// Works!
```
…On Tue, Jan 7, 2025, 18:28 Florian Loitsch ***@***.***> wrote:
Yes.
—
Reply to this email directly, view it on GitHub
<#2689 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A54VSXONGWW7A7N2QZXLCYD2JP6EPAVCNFSM6AAAAABUXJCJVKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZWGMYTAOA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
All works if you rewrite the class as follows:
class Some :
list_/List := []
ab1 -> none :
print "Ab1"
ab2 -> none :
print "Ab2"
ab3 -> none :
print "Ab3"
size :
print "size->$list_.size"
create :
list_.add ::ab1
list_.add ::ab2
list_.add ::ab3
done :
list_.do : | fun/Lambda |
fun.call
main :
some := Some
some.create
some.size
some.done
// Works!
Sorry for the trouble.
…On Wed, Jan 8, 2025, 08:10 michael sure8 ***@***.***> wrote:
I've found inside a class, the vector of lambda functions cannot be used
due initialization error.
// define some class
class Some :
ab1 -> none :
print "Ab1"
ab2 -> none :
print "Ab2"
ab3 -> none :
print "Ab3"
list_/List := [
:: ab1, :: ab2, :: ab3
]
done :
list_.do : | fun/Lambda |
fun.call
/*
test_lambdas.toit:15:9: error: Can't access instance members in field
initializers.
:: ab1, :: ab2, :: ab3
^~~
test_lambdas.toit:15:17: error: Can't access instance members in field
initializers.
:: ab1, :: ab2, :: ab3
^~~
test_lambdas.toit:15:25: error: Can't access instance members in field
initializers.
:: ab1, :: ab2, :: ab3
^~~
Compilation failed.
*/
a1 -> none :
print "A1"
a2 -> none :
print "A2"
a3 -> none :
print "A3"
main :
lambdas := [
:: a1, :: a2, :: a3
]
lambdas.do : | fun/Lambda |
fun.call
// Works!
On Tue, Jan 7, 2025, 18:28 Florian Loitsch ***@***.***>
wrote:
> Yes.
>
> —
> Reply to this email directly, view it on GitHub
> <#2689 (reply in thread)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/A54VSXONGWW7A7N2QZXLCYD2JP6EPAVCNFSM6AAAAABUXJCJVKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZWGMYTAOA>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible in toit to describe several similar functions, place references to them in a list, and then call these functions one after another in a loop (through the list, naturally)?
Beta Was this translation helpful? Give feedback.
All reactions