/* Copyright (c) 2013 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura */ #pragma once #include #include "util/lazy_list.h" #include "util/list.h" namespace lean { template lazy_list take(unsigned sz, lazy_list l) { if (sz == 0 || !l) { return lazy_list(); } else { return lazy_list(head(l), [=]() { return take(sz - 1, tail(l)); }); } } template lazy_list> zip(lazy_list const & l1, lazy_list const & l2) { if (l1 && l2) { return lazy_list>(mk_pair(head(l1), head(l2)), [=]() { return zip(tail(l1), tail(l2)); }); } else { return lazy_list>(); } } template lazy_list to_lazy(list const & l) { if (l) return lazy_list(head(l), [=]() { return to_lazy(tail(l)); }); else return lazy_list(); } template lazy_list append(lazy_list const & l1, lazy_list const & l2) { if (!l1) return l2; else if (!l2) return l1; else return lazy_list(head(l1), [=]() { return append(tail(l1), l2); }); } template lazy_list map(lazy_list const & l, F && f) { if (!l) return l; else return lazy_list(f(head(l)), [=]() { return map(tail(l), f); }); } template lazy_list interleave(lazy_list const & l1, lazy_list const & l2) { if (!l1) return l2; else if (!l2) return l1; else return lazy_list(head(l1), [=]() { return interleave(l2, tail(l1)); }); } template lazy_list filter(lazy_list const & l, P && p) { if (!l) return l; else if (p(head(l))) return lazy_list(head(l), [=]() { return filter(tail(l), p); }); else return filter(tail(l), p); } template lazy_list map_append_aux(lazy_list const & h, lazy_list const & l, F && f) { if (!l) return h; else if (h) return lazy_list(head(h), [=]() { return map_append_aux(tail(h), l, f); }); else return map_append_aux(f(head(l)), tail(l), f); } template lazy_list map_append(lazy_list const & l, F && f) { return map_append_aux(lazy_list(), l, f); } }