Uses of Fibers
#1163
Replies: 2 comments
-
Lazy RecursionThis example shows how you could use Fibers to iterate over a recursive class Directory {
construct new() {
_childDirs = []
_files = []
}
childDirs { _childDirs }
files { _files }
filesRecurseFiber() {
for (file in files) {
Fiber.yield(file)
}
for (dir in childDirs) {
dir.filesRecurseFiber()
}
}
}
var dir1 = Directory.new()
var dir2 = Directory.new()
dir1.files.add("fileA")
dir1.files.add("fileB")
dir2.files.add("fileC")
dir2.files.add("fileD")
dir1.childDirs.add(dir2)
var fiber = Fiber.new { dir1.filesRecurseFiber() }
while (true) {
var file = fiber.call()
if (fiber.isDone) break
System.print(file)
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Faking a cooperative threads model. The idea is too yield some specific values a thread scheduler can react on, to simulate threads. |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
This is a discussion of use cases of Fibers in Wren. They're obviously used for errors, but what are other times where they can be useful?
If you have ideas, please post a new sub-thread. I'll start with one I thought about.
Beta Was this translation helpful? Give feedback.
All reactions