Comments (1)
Hi
With 3 minor modifications I can reproduce what you want:
using ResumableFunctions
@resumable function g(x)
if isa(x,Number)
return x # If x is a number the @resumable function is only called once
else
for i in x
for j in g(i)
j == nothing || @yield j # No explicit return is defined, so we have to eliminate the default returns
end
end
end
end
a = [[1,2,3],[4,5,6]]
for i in g(a)
i==nothing || println(i) # Eliminate the default return
end
A @resumable function
will stop when a return
is encountered or when the function hits the final end
statement. This is also the default behaviour in C# and is a logical consequence of the finite-state-machine implementation using macros. The magic in Python is possible because the compiler can remove the default returns. In PyGen it is also the compiler that handles the tasks, the context switching and the channels which are more expensive than simple function calls as used in ResumableFunctions.
FYI PyGen for Julia v0.5 base on the deprecated produce
and consume
functions has the same problem with your example.
To eliminate the default nothing
return you have to return explicitly the last value:
using ResumableFunctions
@resumable function fibonnaci(n::Int)
a = 0
b = 1
for i in 1:n-1
@yield a
a, b = b, a+b
end
a
end
for fib in fibonnaci(10)
println(fib)
end
from resumablefunctions.jl.
Related Issues (20)
- Recursive parametric functions troubles?
- Resumable function has stopped HOT 2
- `continue` results in infinite loops HOT 3
- macro clashing with ProgressLogging.jl HOT 1
- Error `@yield macro outside a @resumable function!` when using @yield inside a macro HOT 3
- is this package being maintained? HOT 5
- The CI/CD build is broken HOT 1
- Broken on Julia 1.10 (fix included) HOT 17
- Release v1.0.0? HOT 3
- CI, TagBot, Documenter fixes HOT 1
- a resumable function as a struct field var causes an error "struct expression not at top level"
- How to build fast iterators? HOT 4
- `@resumable` breaks blocks scope HOT 2
- let blocks without variable assignement like `let i; f(i); end` are not supported HOT 1
- significant regression in benchmarks HOT 4
- Functionals that refer to themselves are not supported (because they are not saved to the finite state machine)
- ResumableFunctions v0.6.6 StackOverflow with julia-1.10 HOT 8
- tests for downstream breakage HOT 1
- 0.6.7 breaks resumables with multiple fors over same variable HOT 6
- enable inference in self-referencing resumable functions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from resumablefunctions.jl.