Skip to content

Commit

Permalink
Add an Any(iterable).infer method (#4626)
Browse files Browse the repository at this point in the history
Given an iterable thing of values, will return the type / role that
is tightest for the values given.

As proposed by jnthn on https://logs.liz.nl/moarvm/2021-11-09.html#10:25-0001
  • Loading branch information
lizmat committed Jan 6, 2022
1 parent de605e8 commit 4d26377
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/core.c/Any-iterable-methods.pm6
Expand Up @@ -2358,6 +2358,47 @@ Consider using a block if any of these are necessary for your mapping code."
Seq.new(Rakudo::Iterator.Rotor(self.iterator,@cycle,$partial))
}

proto method infer(|) {*}
multi method infer(List:D:) {
my $iterator := self.iterator;
nqp::if(
nqp::eqaddr((my $pulled := $iterator.pull-one),IterationEnd),
Nil, # nothing to check
nqp::stmts(
(my $type := $pulled.WHAT), # initial type
nqp::until(
nqp::eqaddr(($pulled := $iterator.pull-one),IterationEnd)
|| nqp::not_i(nqp::eqaddr($pulled.WHAT,$type)),
nqp::null
),
nqp::if(
nqp::eqaddr($pulled,IterationEnd),
$type, # all the same
self!slow-infer($iterator, $type, $pulled) # find out what
)
)
)
}

method !slow-infer($iterator, Mu $type is copy, Mu $pulled is copy) {
# set up types to check
my $mro :=
nqp::clone(nqp::getattr($type.^mro(:roles),List,'$!reified'));

nqp::repeat_until(
nqp::eqaddr(($pulled := $iterator.pull-one),IterationEnd)
|| nqp::eqaddr($type,Mu),
nqp::until(
nqp::istype($pulled,nqp::atpos($mro,0)),
nqp::stmts( # not the same base type
nqp::shift($mro),
($type := nqp::atpos($mro,0)), # assume next type for now
)
)
);
$type
}

proto method nodemap(|) is nodal {*}
multi method nodemap(Associative:D: &op) {
self.new.STORE: self.keys, self.values.nodemap(&op), :INITIALIZE
Expand Down

0 comments on commit 4d26377

Please sign in to comment.