-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add more implementations using Pydust functionality #3
Conversation
src/fib.zig
Outdated
} | ||
|
||
// Leveraging the `@call` function to generate a tail call. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"generate" -> "enforce
} | ||
}); | ||
|
||
pub const FibonacciIterator = py.class("FibonacciIterator", struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you try and make this a non-pub const property inside the Fibonacci class? Just named Iterator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried this in ziggy-pydust,
/Users/mbakovic/Library/Caches/pypoetry/virtualenvs/ziggy-pydust-template-Zfa63JQ5-py3.11/lib/python3.11/site-packages/pydust/src/pydust.zig:201:5: error: Class has no associated module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, current logic expects all py.class to be top level in module
test/test_fib.py
Outdated
assert actual == expected_item | ||
|
||
# As list | ||
fibonacci_list = fibonacci.to_list() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you do list(fibonacci)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fibonacci_list = fibonacci.to_list() | |
fibonacci_list = list(fibonacci) |
You can!
src/fib.zig
Outdated
} | ||
|
||
// Get the first `self.first_n` Fibonacci numbers as a list. | ||
pub fn to_list(self: *const Self) !py.PyList { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then you can get rid of this in example?
fn fibonacci_recursive_tail(n: u64, f0: u64, f1: u64) u64 { | ||
if (n == 0) return f0; | ||
if (n == 1) return f1; | ||
return @call(.always_tail, fibonacci_recursive_tail, .{ n - 1, f1, f0 + f1 }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried not having this as a separate function but our wrapping breaks call
src/fib.zig:33:12: error: unable to perform tail call: type of function being called 'fn(fib.nth_fibonacci_recursive_tail__struct_952) u64' does not match type of calling function 'fn(*.Users.mbakovic.git.ziggy-pydust-template.zig-cache.o.c93ac25b10a99df15d24de6d938af2e6.cimport.struct__object, [*].Users.mbakovic.git.ziggy-pydust-template.zig-cache.o.c93ac25b10a99df15d24de6d938af2e6.cimport.struct__object, isize, ?*.Users.mbakovic.git.ziggy-pydust-template.zig-cache.o.c93ac25b10a99df15d24de6d938af2e6.cimport.struct__object) callconv(.C) ?*.Users.mbakovic.git.ziggy-pydust-template.zig-cache.o.c93ac25b10a99df15d24de6d938af2e6.cimport.struct__object'
return @call(.always_tail, nth_fibonacci_recursive_tail, .{.{ .n = args.n - 1, .f0 = args.f1, .f1 = args.f0 + args.f1 }});
```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess c functions can't require tall recursion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more likely that the argument is a struct
fn f(n: u64) u64 { | ||
return if (n < 2) n else f(n - 1) + f(n - 2); | ||
// A simple recursive fibonacci implementation. | ||
pub fn nth_fibonacci_recursive(args: struct { n: u64, f0: u64 = 0, f1: u64 = 1 }) u64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a nice way to show defaults in args :)
} | ||
|
||
pub fn __next__(self: *Self) !?u64 { | ||
if (self.i == self.stop) return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove stop and have just infinite iterator, but I think we can show stopping the iterator like this
} | ||
}); | ||
|
||
pub const FibonacciIterator = py.class("FibonacciIterator", struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried this in ziggy-pydust,
/Users/mbakovic/Library/Caches/pypoetry/virtualenvs/ziggy-pydust-template-Zfa63JQ5-py3.11/lib/python3.11/site-packages/pydust/src/pydust.zig:201:5: error: Class has no associated module
No description provided.