|
@@ -4,8 +4,8 @@ namespace Invercargill {
|
|
|
|
|
|
public Enumerable<Attempt<T>> wrap (Enumerable<Attempt<T>> enumerable) {
|
|
public Enumerable<Attempt<T>> wrap (Enumerable<Attempt<T>> enumerable) {
|
|
inner = enumerable;
|
|
inner = enumerable;
|
|
- results = where(a => a.success).select<T>(a => a.result);
|
|
|
|
- errors = where(a => !a.success).select<Error>(a => a.error);
|
|
|
|
|
|
+ results = inner.where(a => a.success).select<T>(a => a.result);
|
|
|
|
+ errors = inner.where(a => !a.success).select<Error>(a => a.error);
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
public bool can_wrap (GLib.Type element_type) {
|
|
public bool can_wrap (GLib.Type element_type) {
|
|
@@ -16,7 +16,7 @@ namespace Invercargill {
|
|
public Enumerable<Error> errors { get; private set; }
|
|
public Enumerable<Error> errors { get; private set; }
|
|
|
|
|
|
public bool fully_successful() {
|
|
public bool fully_successful() {
|
|
- return all(a => a.success);
|
|
|
|
|
|
+ return inner.all(a => a.success);
|
|
}
|
|
}
|
|
|
|
|
|
public bool unwrap_iterate_if(PredicateDelegate<T> handler) throws Error {
|
|
public bool unwrap_iterate_if(PredicateDelegate<T> handler) throws Error {
|
|
@@ -83,7 +83,7 @@ namespace Invercargill {
|
|
return dict;
|
|
return dict;
|
|
}
|
|
}
|
|
|
|
|
|
- public virtual Dictionary<TKey, TValue> unwrap_select_to_dictionary<TKey, TValue>(TransformDelegate<T, TKey> key_selecter, TransformDelegate<T, TValue> value_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
|
|
|
|
|
|
+ public Dictionary<TKey, TValue> unwrap_select_to_dictionary<TKey, TValue>(TransformDelegate<T, TKey> key_selecter, TransformDelegate<T, TValue> value_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
|
|
var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);
|
|
var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);
|
|
unwrap_iterate(i => dict.set(key_selecter(i), value_selecter(i)));
|
|
unwrap_iterate(i => dict.set(key_selecter(i), value_selecter(i)));
|
|
return dict;
|
|
return dict;
|
|
@@ -95,7 +95,7 @@ namespace Invercargill {
|
|
return @set;
|
|
return @set;
|
|
}
|
|
}
|
|
|
|
|
|
- public virtual Elements unwrap_to_elements() throws Error {
|
|
|
|
|
|
+ public Elements unwrap_to_elements() throws Error {
|
|
var series = new ElementSeries();
|
|
var series = new ElementSeries();
|
|
if(typeof(T).is_a(typeof(Element))) {
|
|
if(typeof(T).is_a(typeof(Element))) {
|
|
unwrap_iterate(i => series.add((Element)i));
|
|
unwrap_iterate(i => series.add((Element)i));
|
|
@@ -106,10 +106,144 @@ namespace Invercargill {
|
|
return series;
|
|
return series;
|
|
}
|
|
}
|
|
|
|
|
|
- public virtual Object[] unwrap_to_object_array() throws Error {
|
|
|
|
|
|
+ public Object[] unwrap_to_object_array() throws Error {
|
|
return unwrap_to_vector().to_object_array();
|
|
return unwrap_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);
|
|
|
|
+ unwrap_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)).promote_to<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)).promote_to<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)) : Invercargill.single<Attempt<Tout>>(new Attempt<Tout>.unsuccessful(a.error))).promote_to<Attempts<T>>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public new Attempts<T> concat(Enumerable<Attempt<T>> other) {
|
|
|
|
+ return inner.concat(other).promote_to<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>() {
|
|
|
|
+ origin = i,
|
|
|
|
+ result = transform(i)
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public new Tout aggrigate<Tout>(Tout initial, AggrigateDelegate<Tout, T> aggrigate_func) throws Error {
|
|
|
|
+ var aggrigate = initial;
|
|
|
|
+ unwrap_iterate(i => {
|
|
|
|
+ aggrigate = aggrigate_func(aggrigate, i);
|
|
|
|
+ });
|
|
|
|
+ return aggrigate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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, ref o) => !a.success || transform(a.result, ref o)).promote_to<Attempts<Tout>>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public new Attempts<Tout> try_select<Tout>(owned AttemptTransformDelegate<T, Tout> transform) {
|
|
|
|
+ return inner.try_select<Tout>(a => a.success ? new Attempt<Tout>(() => transform(a.result)) : new Attempt<Tout>.unsuccessful(a.error));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public new Attempts<Tout> try_map_with<Tout>(Mapper<Tout, T> mapper) {
|
|
|
|
+ return try_select<Tout>(o => mapper.materialise(o));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private PredicateDelegate<T> resolve_nullable_predicate(PredicateDelegate<T>? predicate) {
|
|
|
|
+ if(predicate == null) {
|
|
|
|
+ return (p) => true;
|
|
|
|
+ }
|
|
|
|
+ return predicate;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|