feat(util/lazy_list_fn): handle the 'is_nil' case more efficiently
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
c9cfb844f1
commit
db0ef64c04
1 changed files with 79 additions and 57 deletions
|
@ -30,7 +30,7 @@ void for_each(lazy_list<T> l, F && f) {
|
|||
*/
|
||||
template<typename T>
|
||||
lazy_list<T> take(unsigned sz, lazy_list<T> const & l) {
|
||||
if (sz == 0) {
|
||||
if (sz == 0 || l.is_nil()) {
|
||||
return lazy_list<T>();
|
||||
} else {
|
||||
return mk_lazy_list<T>([=]() {
|
||||
|
@ -62,6 +62,11 @@ lazy_list<T> to_lazy(list<T> l) {
|
|||
*/
|
||||
template<typename T>
|
||||
lazy_list<T> append(lazy_list<T> const & l1, lazy_list<T> const & l2, char const * cname = "lazy list") {
|
||||
if (l1.is_nil())
|
||||
return l2;
|
||||
else if (l2.is_nil())
|
||||
return l1;
|
||||
else
|
||||
return mk_lazy_list<T>([=]() {
|
||||
auto p = l1.pull();
|
||||
if (!p) {
|
||||
|
@ -78,6 +83,9 @@ lazy_list<T> append(lazy_list<T> const & l1, lazy_list<T> const & l2, char const
|
|||
*/
|
||||
template<typename T>
|
||||
lazy_list<T> orelse(lazy_list<T> const & l1, lazy_list<T> const & l2, char const * cname = "lazy list") {
|
||||
if (l1.is_nil())
|
||||
return l2;
|
||||
else
|
||||
return mk_lazy_list<T>([=]() {
|
||||
auto p = l1.pull();
|
||||
if (!p) {
|
||||
|
@ -95,6 +103,11 @@ lazy_list<T> orelse(lazy_list<T> const & l1, lazy_list<T> const & l2, char const
|
|||
*/
|
||||
template<typename T>
|
||||
lazy_list<T> interleave(lazy_list<T> const & l1, lazy_list<T> const & l2, char const * cname = "lazy list") {
|
||||
if (l1.is_nil())
|
||||
return l2;
|
||||
else if (l2.is_nil())
|
||||
return l1;
|
||||
else
|
||||
return mk_lazy_list<T>([=]() {
|
||||
auto p = l1.pull();
|
||||
if (!p) {
|
||||
|
@ -111,6 +124,9 @@ lazy_list<T> interleave(lazy_list<T> const & l1, lazy_list<T> const & l2, char c
|
|||
*/
|
||||
template<typename T, typename F>
|
||||
lazy_list<T> map(lazy_list<T> const & l, F && f, char const * cname = "lazy list") {
|
||||
if (l.is_nil())
|
||||
return l;
|
||||
else
|
||||
return mk_lazy_list<T>([=]() {
|
||||
auto p = l.pull();
|
||||
if (!p) {
|
||||
|
@ -127,6 +143,9 @@ lazy_list<T> map(lazy_list<T> const & l, F && f, char const * cname = "lazy list
|
|||
*/
|
||||
template<typename To, typename From, typename F>
|
||||
lazy_list<To> map2(lazy_list<From> const & l, F && f, char const * cname = "lazy list") {
|
||||
if (l.is_nil())
|
||||
return lazy_list<To>();
|
||||
else
|
||||
return mk_lazy_list<To>([=]() {
|
||||
typename lazy_list<From>::maybe_pair p = l.pull();
|
||||
if (!p) {
|
||||
|
@ -147,6 +166,9 @@ lazy_list<To> map2(lazy_list<From> const & l, F && f, char const * cname = "lazy
|
|||
*/
|
||||
template<typename T, typename P>
|
||||
lazy_list<T> filter(lazy_list<T> const & l, P && pred, char const * cname = "lazy list") {
|
||||
if (l.is_nil())
|
||||
return l;
|
||||
else
|
||||
return mk_lazy_list<T>([=]() {
|
||||
auto p = l.pull();
|
||||
if (!p) {
|
||||
|
|
Loading…
Reference in a new issue