Discussion:
[dart-misc] What is equialent with this in Dart ?
Kovács Attila
2018-11-01 05:39:28 UTC
Permalink
Hi

I have a short sample code in Go :

package main

import (
"fmt"
"time"
)

func numbers() {
for i := 1; i <= 5; i++ {
time.Sleep(250 * time.Millisecond)
fmt.Printf("%d ", i)
}
}
func alphabets() {
for i := 'a'; i <= 'e'; i++ {
time.Sleep(400 * time.Millisecond)
fmt.Printf("%c ", i)
}
}
func main() {
go numbers()
go alphabets()
time.Sleep(3000 * time.Millisecond)
fmt.Println("main terminated")
}

You can try it in go playground.

I need the equialent code in Dart. (I'm learning async and await, but i
don't understand it yet.)
Could anybody help me a little bit.

Thank you Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/79b4e89e-2091-4dc4-88a5-8bef0446ea88%40dartlang.org.
'Lasse R.H. Nielsen' via Dart Misc
2018-11-01 07:26:50 UTC
Permalink
A somewhat equivalent program in Dart would be:

const ms = Duration(milliseconds: 1);
void numbers() async {
for (var i = 1; i <= 5; i++) {
await Future.delayed(ms * 250);
print(i);
}
}
void alphabets() async {
var a = "a".codeUnitAt(0);
var e = "e".codeUnitAt(0);
for (var i = a; i <= e; i++) {
await Future.delayed(ms * 400);
print(String.fromCharCode(i));
}
}
void main() async {
numbers();
alphabets();
await Future.delayed(ms * 3000);
print("main terminated");
}


That is not necessarily how I would write that program in Dart. You would
usually not make fire-and-forget functions and then wait for an equivalent
duration.
Instead I'd more likely write it as:

const ms = Duration(milliseconds: 1);
Future<void> numbers() async {
for (var i = 1; i <= 5; i++) {
await Future.delayed(ms * 250);
print(i);
}
}
Future<void> alphabets() async {
var a = "a".codeUnitAt(0);
var e = "e".codeUnitAt(0);
for (var i = a; i <= e; i++) {
await Future.delayed(ms * 400);
print(String.fromCharCode(i));
}
}
void main() async {
await Future.wait([numbers(), alphabets()]);
print("main terminated");
}


Alternatively, if you are not going to await the async functions anyway,
you can use timers instead:

import "dart:async";const ms = Duration(milliseconds: 1);
void numbers() {
int i = 1;
Timer.periodic(ms * 250, (t) {
print(i);
i++;
if (i > 5) t.cancel();
});
}
Future<void> alphabets() async {
var string = "abcde";
var i = 0;
Timer.periodic(ms * 400, (t) {
print(string[i]);
i++;
if (i >= string.length) t.cancel();
});
}
void main() {
numbers();
alphabets();
Timer(ms * 3000, () {
print("main terminated");
});
}


Hope this helps.
/L
Post by Kovács Attila
Hi
package main
import (
"fmt"
"time"
)
func numbers() {
for i := 1; i <= 5; i++ {
time.Sleep(250 * time.Millisecond)
fmt.Printf("%d ", i)
}
}
func alphabets() {
for i := 'a'; i <= 'e'; i++ {
time.Sleep(400 * time.Millisecond)
fmt.Printf("%c ", i)
}
}
func main() {
go numbers()
go alphabets()
time.Sleep(3000 * time.Millisecond)
fmt.Println("main terminated")
}
You can try it in go playground.
I need the equialent code in Dart. (I'm learning async and await, but i
don't understand it yet.)
Could anybody help me a little bit.
Thank you Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/79b4e89e-2091-4dc4-88a5-8bef0446ea88%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/79b4e89e-2091-4dc4-88a5-8bef0446ea88%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
Lasse R.H. Nielsen - ***@google.com
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 KÞbenhavn K
- Denmark - CVR nr. 28 86 69 84
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CA%2BeWuVC37_ffihE-UnPMvzEmo%3D79-yzm6AsxjDyg%3DGakzTthcw%40mail.gmail.com.
Kovács Attila
2018-11-01 07:53:20 UTC
Permalink
Thank you Sir for your abundant explanation
It help me a lot.
Attila
Post by Kovács Attila
Hi
package main
import (
"fmt"
"time"
)
func numbers() {
for i := 1; i <= 5; i++ {
time.Sleep(250 * time.Millisecond)
fmt.Printf("%d ", i)
}
}
func alphabets() {
for i := 'a'; i <= 'e'; i++ {
time.Sleep(400 * time.Millisecond)
fmt.Printf("%c ", i)
}
}
func main() {
go numbers()
go alphabets()
time.Sleep(3000 * time.Millisecond)
fmt.Println("main terminated")
}
You can try it in go playground.
I need the equialent code in Dart. (I'm learning async and await, but i
don't understand it yet.)
Could anybody help me a little bit.
Thank you Attila
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/0534dc3c-1446-41ff-afc3-520b00a3887b%40dartlang.org.
Loading...