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

Alternate #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Alternate #81

wants to merge 2 commits into from

Conversation

gustaphe
Copy link
Contributor

Adds "Alternate", an iterator that cycles through a number of iterators until one of them runs out.

julia> println(alternate(1:5,"the",-(1:5)));
1
t
-1
2
h
-2
3
e
-3
4

alternate(xs...) = length(xs) == 1 ? xs[1] : Alternate(xs)
function Base.iterate(it::Alternate, state=1)
if state == 1
state=[Array{Any}(nothing,length(it.xs)),1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not type stable, so will most likely perform poorly. I'd probably do something like

function iterate(it::Interleave, state=(true, iterate(it.a), iterate(it.b)))
    if nothing nonsense
    if state[1]
        state[2][1], (false, iterate(state[2][2], state[3])
    else
        state[3][1], (true, state[2], iterate(state[3][2])
    end
end

@oxinabox
Copy link
Member

oxinabox commented Jun 7, 2023

I would like to call this interleave to match interleaveby which we added yesterday. And the microkanren paper

@gustaphe
Copy link
Contributor Author

gustaphe commented Jun 7, 2023

Sounds good to me. I don't have a strong sense of how this was implemented either because it's been a while, so make any changes you want :) (let me know if you need me to do something, I might have some time later this week)

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