Skip to content
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

Merged
merged 6 commits into from
Sep 7, 2023

Conversation

delta003
Copy link
Member

@delta003 delta003 commented Sep 7, 2023

No description provided.

src/fib.zig Outdated
}

// Leveraging the `@call` function to generate a tail call.
Copy link
Contributor

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 {
Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Member

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()
Copy link
Member

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)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Member

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?

Comment on lines +32 to +36
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 });
}
Copy link
Member Author

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 }});
    ```

Copy link
Member

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?

Copy link
Member

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 {
Copy link
Member Author

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;
Copy link
Member Author

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 {
Copy link
Member Author

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

@delta003 delta003 enabled auto-merge (squash) September 7, 2023 15:08
@delta003 delta003 merged commit 2eb546a into develop Sep 7, 2023
@delta003 delta003 deleted the mb/examples branch September 7, 2023 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants