This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c1644a
commit 1d86d5c
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 23,6 @@ | |
|
||
#include "lambda/dollar.hpp" | ||
|
||
#include "lambda/stream.hpp" | ||
|
||
#endif // LAMBDA_LAMBDA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,69 @@ | ||
|
||
#pragma once | ||
|
||
#include "maybe.hpp" | ||
|
||
namespace lambda { | ||
|
||
// pub trait Iterator { | ||
// type Item; | ||
// fn next(&mut self)->Option<Self::Item>; | ||
// | ||
// fn map<B, F>(self, f: F) -> Map<Self, F> where Self: Sized, F: FnMut(Self::Item) -> B { ... } | ||
// | ||
// | ||
// pub struct Map<I, F> { | ||
// iter: I, | ||
// f: F, | ||
// } | ||
// | ||
|
||
template <class T> | ||
class Stream : public std::enable_shared_from_this<Stream<T>> { | ||
public: | ||
using RawStream = std::shared_ptr<Stream>; | ||
|
||
Stream() {} | ||
|
||
virtual Maybe<T> next() = 0; | ||
|
||
template <class F> | ||
RawStream map(F &&f); | ||
}; | ||
|
||
template <class T> | ||
using RawStream = typename Stream<T>::RawStream; | ||
|
||
template <class T, class F> | ||
class Map : public Stream<T> { | ||
RawStream<T> stream; | ||
F f; | ||
|
||
public: | ||
Map(RawStream<T> stream, F f) : stream(stream), f(f) {} | ||
|
||
virtual Maybe<T> next() { return stream->next() >> f; } | ||
}; | ||
|
||
template <class T, class C> | ||
class IteratorStream : public Stream<T> { | ||
typename C::const_iterator begin, end; | ||
|
||
public: | ||
IteratorStream(C &collection) : begin(collection.cbegin()), end(collection.cend()) {} | ||
|
||
virtual Maybe<T> next() { return begin != end ? some(*begin ) : none; } | ||
}; | ||
|
||
template <class T, class C> | ||
static auto from(C &collection) { | ||
return RawStream<T>(new IteratorStream<T, C>{collection}); | ||
} | ||
|
||
template <class T> | ||
template <class F> | ||
RawStream<T> Stream<T>::map(F &&f) { | ||
return RawStream(new Map<T, F>{Stream<T>::shared_from_this(), f}); | ||
} | ||
|
||
} /* lambda */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters