| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- using Invercargill.DataStructures;
- using Invercargill.Mapping;
- namespace Invercargill {
- public class Attempts<T> : Proxy<Attempt<T>>, Promotion<Attempt<T>> {
- public Enumerable<Attempt<T>> wrap (Enumerable<Attempt<T>> enumerable) {
- inner = enumerable;
- results = inner.where(a => a.success).select<T>(a => a.result);
- errors = inner.where(a => !a.success).select<Error>(a => a.error);
- return this;
- }
- public bool can_wrap (GLib.Type element_type) {
- return element_type.is_a (typeof(Attempt));
- }
- public Enumerable<T> results { get; private set; }
- public Enumerable<Error> errors { get; private set; }
- public bool fully_successful() {
- return inner.all(a => a.success);
- }
- public new bool iterate_if(PredicateDelegate<T> handler) throws Error {
- var tracker = get_tracker();
- while(tracker.has_next()) {
- var item = tracker.get_next();
- if(item.success) {
- if(!handler(item.result)) {
- return false;
- }
- }
- else {
- throw item.error;
- }
- }
- return true;
- }
- public new void iterate(ItemDelegate<T> handler) throws Error {
- iterate_if(i => {
- handler(i);
- return true;
- });
- }
- public new Series<T> to_series() throws Error {
- var series = new Series<T>();
- iterate(i => series.add(i));
- return series;
- }
- public new T[] to_array() throws Error {
- var array = new T[1024];
- var index = 0;
- foreach (var item in this) {
- if(!item.success) {
- throw item.error;
- }
- if(index >= array.length) {
- array.resize(array.length*2);
- }
- safely_assign_to_array<T>(array, index, item.result);
- index++;
- }
- array.resize(index);
- return array;
- }
- public new Vector<T> to_vector() throws Error {
- var vector = new Vector<T>();
- iterate(i => vector.add(i));
- return vector;
- }
- public new Dictionary<TKey, T> to_dictionary<TKey>(TransformDelegate<T, TKey> key_selecter, HashDelegate<TKey>? key_hash_func = null, EqualityDelegate<TKey>? key_equal_func = null) throws Error {
- var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);
- iterate(i => dict.set(key_selecter(i), i));
- return dict;
- }
- public new Dictionary<TKey, TValue> select_to_dictionary<TKey, TValue>(TransformDelegate<T, TKey> key_selecter, TransformDelegate<T, TValue> value_selecter, HashDelegate<TKey>? key_hash_func = null, EqualityDelegate<TKey>? key_equal_func = null) throws Error {
- var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);
- iterate(i => dict.set(key_selecter(i), value_selecter(i)));
- return dict;
- }
- public new Set<T> to_set(HashDelegate<T>? hash_func = null, EqualityDelegate<T>? equal_func = null) throws Error {
- var @set = new HashSet<T>(hash_func, equal_func);
- iterate(i => @set.add(i));
- return @set;
- }
- public new Elements to_elements() throws Error {
- var series = new ElementSeries();
- if(typeof(T).is_a(typeof(Element))) {
- iterate(i => series.add((Element)i));
- }
- else {
- iterate(i => series.add(new NativeElement<T>(i)));
- }
- return series;
- }
- public new Object[] to_object_array() throws Error {
- return to_vector().to_object_array();
- }
- // New Functions
- public new bool any(PredicateDelegate<T> predicate = (i) => true) throws Error {
- var result = false;
- var p = resolve_nullable_predicate(predicate);
- iterate_if(i => {
- if(p(i)) {
- result = true;
- return false;
- }
- return true;
- });
- return result;
- }
-
- public new bool all(PredicateDelegate<T> predicate) throws Error {
- return !any(i => !predicate(i));
- }
- public new bool no(PredicateDelegate<T>? predicate = null) throws Error {
- var p = resolve_nullable_predicate(predicate);
- return all(i => !p(i));
- }
- public new Attempts<T> where(owned PredicateDelegate<T> predicate) {
- return inner.where(i => !i.success || predicate(i)).assert_promotion<Attempts<T>>();
- }
-
- public new Attempts<TOut> select<TOut>(owned TransformDelegate<T, TOut> transform) {
- return inner.select<Attempt<TOut>>(a => a.success ? new Attempt<TOut>.successful(transform(a.result)) : new Attempt<TOut>.unsuccessful(a.error)).assert_promotion<Attempts<T>>();
- }
-
- public new Attempts<TOut> select_many<TOut>(owned TransformDelegate<T, Enumerable<TOut>> transform) {
- return inner.select_many<Attempt<TOut>>(a => a.success ? transform(a.result).select<Attempt<TOut>>(i => new Attempt<TOut>.successful(i)) : Iterate.single<Attempt<TOut>>(new Attempt<TOut>.unsuccessful(a.error))).assert_promotion<Attempts<T>>();
- }
-
- public new Attempts<T> concat(Enumerable<Attempt<T>> other) {
- return inner.concat(other).assert_promotion<Attempts>();
- }
-
- public new Attempts<TOut> cast<TOut>() {
- return select<TOut>(i => (TOut)i);
- }
-
- public new Attempts<SelectionContext<T, TOut>> contextualised_select<TOut>(owned TransformDelegate<T, TOut> transform) {
- return select<SelectionContext<T, TOut>>((i) => new SelectionContext<T, TOut>(i, transform(i)));
- }
-
- public new TOut aggregate<TOut>(TOut initial, AggregateDelegate<TOut, T> aggregate_func) throws Error {
- var aggregate = initial;
- iterate(i => {
- aggregate = aggregate_func(aggregate, i);
- });
- return aggregate;
- }
-
- public new T max(TransformDelegate<T, int> int_delegate) throws Error{
- T item = null;
- var first = true;
- var value = 0;
- foreach (var a in this) {
- var i = a.unwrap();
- if(first) {
- first = false;
- item = i;
- value = int_delegate(i);
- continue;
- }
- var item_value = int_delegate(i);
- if(item_value > value) {
- value = item_value;
- item = i;
- }
- }
- return item;
- }
- public new T min(TransformDelegate<T, int> int_delegate) throws Error {
- T item = null;
- var first = true;
- var value = 0;
- foreach (var a in this) {
- var i = a.unwrap();
- if(first) {
- first = false;
- item = i;
- value = int_delegate(i);
- continue;
- }
- var item_value = int_delegate(i);
- if(item_value < value) {
- value = item_value;
- item = i;
- }
- }
- return item;
- }
-
- public new bool contains(T item) throws Error {
- return any(i => i == item);
- }
-
- public Type attempt_element_type {
- get {
- return typeof(T);
- }
- }
-
- public new Attempts<TOut> select_where<TOut>(owned FilterTransformDelegate<T, TOut> transform) {
- return inner.select_where<TOut>((a, out o) => !a.success || transform(a.result, out o)).assert_promotion<Attempts<TOut>>();
- }
- public new Attempts<TOut> attempt_select<TOut>(owned AttemptTransformDelegate<T, TOut> transform) {
- return inner.attempt_select<TOut>(a => a.success ? new Attempt<TOut>(() => transform(a.result)) : new Attempt<TOut>.unsuccessful(a.error));
- }
- public new Attempts<TOut> attempt_select_nested<TOut>(owned AttemptTransformDelegate<T, Enumerable<Attempt<TOut>>> transform) {
- return attempt_select<Enumerable<Attempt<TOut>>>((owned)transform)
- .as_enumerable()
- .select_many<Attempt<TOut>>(a => a.success ? a.result : Iterate.single<Attempt<TOut>>(new Attempt<TOut>.unsuccessful(a.error)))
- .assert_promotion<Attempts<TOut>>();
- }
- public new Attempts<TOut> attempt_map_with<TOut>(Mapper<TOut, T> mapper) {
- return attempt_select<TOut>(o => mapper.materialise(o));
- }
- private PredicateDelegate<T> resolve_nullable_predicate(PredicateDelegate<T>? predicate) {
- if(predicate == null) {
- return (p) => true;
- }
- return predicate;
- }
- }
- }
|