Commit aac47db4 authored by Kirk Shoop's avatar Kirk Shoop Committed by Facebook Github Bot

adding example for things that require set_done (#31)

fbshipit-source-id: a79daf051453f6b189330891fa290be4e9cd6549
parent 5ab9aa60
...@@ -51,6 +51,7 @@ set(header_files ...@@ -51,6 +51,7 @@ set(header_files
"${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/on.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/on.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/tap.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/tap.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/transform.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/transform.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/filter.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/via.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/via.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/request_via.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/request_via.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/share.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/pushmi/o/share.h"
......
...@@ -9,3 +9,4 @@ add_subdirectory(then_execute) ...@@ -9,3 +9,4 @@ add_subdirectory(then_execute)
add_subdirectory(composition) add_subdirectory(composition)
add_subdirectory(for_each) add_subdirectory(for_each)
add_subdirectory(reduce) add_subdirectory(reduce)
add_subdirectory(set_done)
add_executable(set_done_2 set_done_2.cpp)
target_link_libraries(set_done_2
pushmi
examples
Threads::Threads)
#include <vector>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <pushmi/o/just.h>
#include <pushmi/o/tap.h>
#include <pushmi/o/filter.h>
#include <pushmi/o/transform.h>
#include <pushmi/o/empty.h>
using namespace pushmi::aliases;
const bool setting_exists = false;
auto get_setting() {
return mi::make_single_deferred(
[](auto out){
if(setting_exists) {
op::just(42) | op::submit(out);
} else {
op::empty<int>() | op::submit(out);
}
}
);
}
auto println = [](auto v){std::cout << v << std::endl;};
// concat not yet implemented
auto concat =
[](auto in){
return mi::make_single_deferred(
[in](auto out) mutable {
::pushmi::submit(in, mi::make_single(out,
[](auto out, auto v){
// ::pushmi::submit(v, out);
}));
});
};
int main()
{
get_setting() |
op::transform([](int i){ return std::to_string(i); }) |
op::submit(println);
op::just(42) |
op::filter([](int i){ return i < 42; }) |
op::transform([](int i){ return std::to_string(i); }) |
op::submit(println);
op::just(42) |
op::transform([](int i) {
if (i < 42) {
return mi::any_single_deferred<std::string>{op::empty<std::string>()};
}
return mi::any_single_deferred<std::string>{op::just(std::to_string(i))};
}) |
concat |
op::submit(println);
std::cout << "OK" << std::endl;
}
...@@ -922,7 +922,6 @@ PUSHMI_CONCEPT_DEF( ...@@ -922,7 +922,6 @@ PUSHMI_CONCEPT_DEF(
#if __cpp_lib_invoke >= 201411 #if __cpp_lib_invoke >= 201411
using std::invoke; using std::invoke;
using std::invoke_result_t;
#else #else
PUSHMI_TEMPLATE (class F, class...As) PUSHMI_TEMPLATE (class F, class...As)
(requires requires ( (requires requires (
...@@ -940,10 +939,10 @@ decltype(auto) invoke(F f, As&&...as) ...@@ -940,10 +939,10 @@ decltype(auto) invoke(F f, As&&...as)
noexcept(noexcept(std::declval<decltype(std::mem_fn(f))>()((As&&) as...))) { noexcept(noexcept(std::declval<decltype(std::mem_fn(f))>()((As&&) as...))) {
return std::mem_fn(f)((As&&) as...); return std::mem_fn(f)((As&&) as...);
} }
#endif
template <class F, class...As> template <class F, class...As>
using invoke_result_t = using invoke_result_t =
decltype(pushmi::invoke(std::declval<F>(), std::declval<As>()...)); decltype(pushmi::invoke(std::declval<F>(), std::declval<As>()...));
#endif
PUSHMI_CONCEPT_DEF( PUSHMI_CONCEPT_DEF(
template (class F, class... Args) template (class F, class... Args)
...@@ -6113,6 +6112,55 @@ PUSHMI_INLINE_VAR constexpr detail::transform_fn transform{}; ...@@ -6113,6 +6112,55 @@ PUSHMI_INLINE_VAR constexpr detail::transform_fn transform{};
// This source code is licensed under the MIT license found in the // This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree. // LICENSE file in the root directory of this source tree.
//#include "../piping.h"
//#include "extension_operators.h"
namespace pushmi {
namespace detail {
struct filter_fn {
PUSHMI_TEMPLATE(class Predicate)
(requires SemiMovable<Predicate>)
auto operator()(Predicate p) const {
return constrain(lazy::Sender<_1>, [p = std::move(p)](auto in) {
using In = decltype(in);
return ::pushmi::detail::deferred_from<In, single<>>(
std::move(in),
::pushmi::detail::submit_transform_out<In>(
constrain(lazy::Receiver<_1>, [p](auto out) {
using Out = decltype(out);
return ::pushmi::detail::out_from_fn<In>()(
std::move(out),
// copy 'p' to allow multiple calls to submit
::pushmi::on_value([p](auto& out, auto&& v) {
if (p(as_const(v))) {
::pushmi::set_value(out, std::move(v));
} else {
::pushmi::set_done(out);
}
})
);
})
)
);
});
}
};
} // namespace detail
namespace operators {
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter{};
} // namespace operators
} // namespace pushmi
//#pragma once
// Copyright (c) 2018-present, Facebook, Inc.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//#include "../piping.h" //#include "../piping.h"
//#include "../executor.h" //#include "../executor.h"
//#include "extension_operators.h" //#include "extension_operators.h"
......
#pragma once
// Copyright (c) 2018-present, Facebook, Inc.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include "../piping.h"
#include "extension_operators.h"
namespace pushmi {
namespace detail {
struct filter_fn {
PUSHMI_TEMPLATE(class Predicate)
(requires SemiMovable<Predicate>)
auto operator()(Predicate p) const {
return constrain(lazy::Sender<_1>, [p = std::move(p)](auto in) {
using In = decltype(in);
return ::pushmi::detail::deferred_from<In, single<>>(
std::move(in),
::pushmi::detail::submit_transform_out<In>(
constrain(lazy::Receiver<_1>, [p](auto out) {
using Out = decltype(out);
return ::pushmi::detail::out_from_fn<In>()(
std::move(out),
// copy 'p' to allow multiple calls to submit
::pushmi::on_value([p](auto& out, auto&& v) {
if (p(as_const(v))) {
::pushmi::set_value(out, std::move(v));
} else {
::pushmi::set_done(out);
}
})
);
})
)
);
});
}
};
} // namespace detail
namespace operators {
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter{};
} // namespace operators
} // namespace pushmi
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment