public object reduce(IFn f, object start) { object ret = f.invoke(start, _array[_off]); for (int x = _off + 1; x < _end; x++) ret = f.invoke(ret, _array[x]); return ret; }
public object reduce(IFn f) { Object init; if (_cnt > 0) { init = ArrayFor(0)[0]; } else { return(f.invoke()); } int step = 0; for (int i = 0; i < _cnt; i += step) { Object[] array = ArrayFor(i); for (int j = (i == 0) ? 1 : 0; j < array.Length; ++j) { init = f.invoke(init, array[j]); if (RT.isReduced(init)) { return(((IDeref)init).deref()); } } step = array.Length; } return(init); }
public object reduce(IFn f, object start) { Object ret = start; if (_count == INFINITE) { while (true) { ret = f.invoke(ret, _val); if (RT.isReduced(ret)) { return(((IDeref)ret).deref()); } } } else { for (long i = 0; i < _count; i++) { ret = f.invoke(ret, _val); if (RT.isReduced(ret)) { return(((IDeref)ret).deref()); } } return(ret); } }
private static void require(string s) { if (requireFn == null) { requireFn = RT.var("clojure.core", "require"); } requireFn.invoke(Symbol.intern(s)); }
public override object first() { if (_seed == UNREALIZED_SEED) { _seed = _f.invoke(_prevSeed); } return(_seed); }
public object reduce(IFn f, object start) { object ret = f.invoke(start, _array[_off]); for (int x = _off + 1; x < _end; x++) { ret = f.invoke(ret, _array[x]); } return(ret); }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <param name="start">An initial value to get started.</param> /// <returns>The reduced value</returns> public object reduce(IFn f, object start) { object ret = f.invoke(start, _v.nth(_i)); for (int x = _i + 1; x < _v.count(); x++) { ret = f.invoke(ret, _v.nth(x)); } return(ret); }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <param name="start">An initial value to get started.</param> /// <returns>The reduced value</returns> public object reduce(IFn f, object start) { object ret = f.invoke(start, first()); for (ISeq s = next(); s != null; s = s.next()) { ret = f.invoke(ret, s.first()); } return(ret); }
public object reduce(IFn f, object start) { object ret = f.invoke(start, Reflector.prepRet(_ct, _array[_i])); for (int x = _i + 1; x < _array.Length; x++) { ret = f.invoke(ret, Reflector.prepRet(_ct, _array[x])); } return(ret); }
public object reduce(IFn f, object start) { object ret = f.invoke(start, Reflector.prepRet(_ct, _a.GetValue(_i))); for (int x = _i + 1; x < _a.Length; x++) { ret = f.invoke(ret, Reflector.prepRet(_ct, _a.GetValue(x))); } return(ret); }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <param name="start">An initial value to get started.</param> /// <returns>The reduced value</returns> /// <remarks>Computes f(...f(f(f(start,i0),i1),i2),...).</remarks> public object reduce(IFn f, object start) { object ret = f.invoke(start, _n); for (int x = _n + 1; x < _end; x++) { ret = f.invoke(ret, x); } return(ret); }
public void InvokeOn1ArgDoesValAt1() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IFn f = (IFn)PersistentArrayMap.create(d); Expect(f.invoke(1), EqualTo("a")); Expect(f.invoke(7), Null); }
public void InvokeOn2ArgsDoesValAt2() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IFn f = (IFn)PersistentArrayMap.create(d); Expect(f.invoke(1, 99), EqualTo("a")); Expect(f.invoke(7, 99), EqualTo(99)); }
public void ReduceWithNoStartIterates() { MockRepository mocks = new MockRepository(); IFn fn = mocks.StrictMock <IFn>(); RMExpect.Call(fn.invoke(2, 3)).Return(5); RMExpect.Call(fn.invoke(5, 4)).Return(7); mocks.ReplayAll(); Range r = new Range(2, 5); object ret = r.reduce(fn); Expect(ret, EqualTo(7)); mocks.VerifyAll(); }
// ========================================================== // duplication public JumpMap CopyTo(JumpMap jm2, IFn defaultConversion, ILookup conversions, object sourceObject, object targetObject) { // TODO: clean this up when we get generic persistent lookup if (conversions != null) { foreach (var e in dict) { object key = e.Key; IFn conversion = defaultConversion; object tempConversion = conversions.valAt(key); if (tempConversion != null) { conversion = Arcadia.Util.AsIFn((IFn)tempConversion); } jm2.Add(key, conversion.invoke(key, e.Value.val, sourceObject, targetObject)); } } else { foreach (var e in dict) { jm2.Add(e.Key, defaultConversion.invoke(e.Key, e.Value.val, sourceObject, targetObject)); } } return(jm2); }
public void ReduceWithNoStartIterates() { MockRepository mocks = new MockRepository(); IFn fn = mocks.StrictMock <IFn>(); RMExpect.Call(fn.invoke(1, 2)).Return(5); RMExpect.Call(fn.invoke(5, 3)).Return(7); mocks.ReplayAll(); PersistentList p = (PersistentList)PersistentList.create(new object[] { 1, 2, 3 }); object ret = p.reduce(fn); Expect(ret, EqualTo(7)); mocks.VerifyAll(); }
void Run(string[] args) { Log($"starting..."); Log($" CLOJURE_LOAD_PATH={Environment.GetEnvironmentVariable("CLOJURE_LOAD_PATH")}"); IFn load = Clojure.var("clojure.core", "load-file"); Log($" clojure.core/load-file found."); IFn printEx = Clojure.var("clojure.stacktrace", "print-cause-trace"); Log($" clojure.stacktrace/print-stack-trace found."); var script = args[0]; Log($" (load-file '{script}') starting..."); try { load.invoke(script); Log($" (load-file '{script}') done."); } catch (Exception ex) { Console.WriteLine(ex.Message); printEx.invoke(ex); } Log("done."); }
void EnsureGeneratedFn() { if (generatedFn == null) { generatedFn = (IFn)fn.invoke(); } }
private static object ReadTagged(PushbackTextReader r, Symbol tag, IPersistentMap opts) { object o = ReadAux(r, opts); ILookup readers = (ILookup)RT.get(opts, READERS); IFn dataReader = (IFn)RT.get(readers, tag); if (dataReader == null) { dataReader = (IFn)RT.get(RT.DefaultDataReadersVar.deref(), tag); } if (dataReader == null) { IFn defaultReader = (IFn)RT.get(opts, DEFAULT); if (defaultReader != null) { return(defaultReader.invoke(tag, o)); } else { throw new InvalidOperationException("No reader function for tag " + tag.ToString()); } } else { return(dataReader.invoke(o)); } }
public void sceneLoadedHook(GameObject go, IFn fn) { // TODO: Add some event listener clear for this SceneManager.sceneLoaded += delegate(Scene arg0, LoadSceneMode arg1) { fn.invoke(go, arg0.buildIndex); }; }
static ArcadiaBehaviourEditor() { requireFn = RT.var("clojure.core", "require"); requireFn.invoke(Symbol.intern("arcadia.internal.editor-interop")); intoArrayFn = RT.var("clojure.core", "into-array"); allUserFns = RT.var("arcadia.internal.editor-interop", "all-user-fns"); }
public void OnBeforeSerialize() { if (prStr == null) { prStr = (IFn)RT.var("clojure.core", "pr-str"); } if (requireFn == null) { requireFn = (IFn)RT.var("clojure.core", "require"); } requireFn.invoke(Symbol.intern("arcadia.literals")); Namespace ArcadiaLiteralsNamespace = Namespace.findOrCreate(Symbol.intern("arcadia.literals")); Var ObjectDbVar = Var.intern(ArcadiaLiteralsNamespace, Symbol.intern("*object-db*")).setDynamic(); initializeVars(); WipeDatabase(); Var.pushThreadBindings(RT.map(ObjectDbVar, objectDatabase)); try { edn = (string)prStr.invoke(state.deref()); // side effects, updating objectDatabase var map = (PersistentHashMap)objectDatabase.deref(); objectDatabaseIds = (int[])RT.seqToTypedArray(typeof(int), RT.keys(map)); objectDatabaseObjects = (Object[])RT.seqToTypedArray(typeof(Object), RT.vals(map)); } finally { Var.popThreadBindings(); } }
public void RSeqReduceWithNoStartIterates() { MockRepository mocks = new MockRepository(); IFn fn = mocks.StrictMock <IFn>(); RMExpect.Call(fn.invoke(3, 2)).Return(5); RMExpect.Call(fn.invoke(5, 1)).Return(7); mocks.ReplayAll(); CPV v = new CPV(new object[] { 1, 2, 3 }); IReduce r = (IReduce)v.rseq(); object ret = r.reduce(fn); Expect(ret, EqualTo(7)); mocks.VerifyAll(); }
public static object runInTransaction(IFn fn) { // TODO: This can be called on something more general than an IFn. // We can could define a delegate for this, probably use ThreadStartDelegate. // Should still have a version that takes IFn. LockingTransaction t = _transaction; Object ret; if (t == null) { _transaction = t = new LockingTransaction(); try { ret = t.Run(fn); } finally { _transaction = null; } } else { if (t._info != null) { ret = fn.invoke(); } else { ret = t.Run(fn); } } return(ret); }
/// <summary> /// Get the value. /// </summary> /// <returns>The value</returns> /// <remarks>Forces the computation if it has not happened yet.</remarks> public object deref() { if (_fn != null) { lock (this) { // double check if (_fn != null) { try { _val = _fn.invoke(); } catch (Exception e) { _exception = e; } _fn = null; } } } if (_exception != null) { throw _exception; } return(_val); }
void EnterCommand() { Log(TerminalLogType.Input, "{0}", command_text); // Shell.RunCommand(command_text); map = (IPersistentMap)repl_rfn.invoke(map.valAt(env), command_text); string txt = (string)map.valAt(result); txt = txt.TrimEnd(); log.invoke(txt); if (command_text != last_command_text) { History.Push(command_text); } else { History.Next(); } last_command_text = command_text; if (IssuedError) { Log(TerminalLogType.Error, "Error: {0}", Shell.IssuedErrorMessage); } command_text = ""; scroll_position.y = int.MaxValue; }
public void AddFunction(IFn f, object key) { if (!fullyInitialized) { Init(); } addFnFn.invoke(this, key, f); }
public void ReduceWithNoStartIterates() { MockRepository mocks = new MockRepository(); IFn fn = mocks.StrictMock <IFn>(); RMExpect.Call(fn.invoke(2, 3)).Return(5); RMExpect.Call(fn.invoke(5, 4)).Return(7); mocks.ReplayAll(); object[] array = new object[] { 2, 3, 4 }; ArraySeq a = ArraySeq.create(array); object ret = a.reduce(fn); Expect(ret, EqualTo(7)); mocks.VerifyAll(); }
public static void REPL2(Stream input, Stream output, Stream err, IExecutor ex) { var codeReader = new LineNumberingTextReader(new StreamReader(new PushbackInputStream(input))); var outputWriter = TextWriter.Synchronized(new StreamWriter(output)); var errorWriter = TextWriter.Synchronized(new StreamWriter(err)); repl.invoke(codeReader, outputWriter, errorWriter, ex); }
public void RemoveFunction(object key) { if (!fullyInitialized) { Init(); } removeFnFn.invoke(this, key); }
public void RemoveAllFunctions() { if (!fullyInitialized) { Init(); } removeAllFnsFn.invoke(this); }
public object reduce(IFn f, object start) { object ret = f.invoke(start, _array[_off]); if (RT.isReduced(ret)) return ret; for (int x = _off + 1; x < _end; x++) { ret = f.invoke(ret, _array[x]); if (RT.isReduced(ret)) return ((IDeref)ret).deref(); } return ret; }
public object swap(IFn f) { for (; ; ) { object v = deref(); object newv = f.invoke(v); Validate(newv); if (_state.CompareAndSet(v, newv)) { NotifyWatches(v,newv); return newv; } } }
static object FoldTasks(List<Func<object>> tasks, IFn combinef, IFn fjtask, IFn fjfork, IFn fjjoin) { if (tasks.Count == 0) return combinef.invoke(); if (tasks.Count == 1 ) return tasks[0].Invoke(); int half = tasks.Count / 2; List<Func<object>> t1 = tasks.GetRange(0, half); List<Func<object>> t2 = tasks.GetRange(half, tasks.Count - half); object forked = fjfork.invoke(fjtask.invoke(new Func<object>(() => { return FoldTasks(t2, combinef, fjtask, fjfork, fjjoin); }))); return combinef.invoke(FoldTasks(t1, combinef, fjtask, fjfork, fjjoin), fjjoin.invoke(forked)); }
public object fold(long n, IFn combinef, IFn reducef, IFn fjinvoke, IFn fjtask, IFn fjfork, IFn fjjoin) { // JVM: we are ignoring n for now Func<object> top = new Func<object>(() => { object ret = combinef.invoke(); if (_root != null) ret = combinef.invoke(ret, _root.Fold(combinef, reducef, fjtask, fjfork, fjjoin)); return _hasNull ? combinef.invoke(ret, reducef.invoke(combinef.invoke(), null, _nullValue)) : ret; }); return fjinvoke.invoke(top); }
/// <summary> /// Start a transaction and invoke a function. /// </summary> /// <param name="fn">The fucntion to invoke.</param> /// <returns>The value computed by the function.</returns> object run(IFn fn) { bool done = false; object ret = null; List<Ref> locked = new List<Ref>(); for (int i = 0; !done && i < RETRY_LIMIT; i++) { try { getReadPoint(); if (i == 0) { _startPoint = _readPoint; _startTime = DateTime.Now.Ticks; } _info = new Info(RUNNING, _startPoint); ret = fn.invoke(); // make sure no one has killed us before this point, // and can't from now on if (_info.Status.compareAndSet(RUNNING, COMMITTING)) { foreach (KeyValuePair<Ref, List<CFn>> pair in _commutes) { Ref r = pair.Key; r.EnterWriteLock(); locked.Add(r); Info refinfo = r.TInfo; if (refinfo != null && refinfo != _info && refinfo.IsRunning) { if (!barge(refinfo)) { throw _retryex; } } object val = r.TryGetVal(); if (!_sets.Contains(r)) _vals[r] = val; foreach (CFn f in pair.Value) _vals[r] = f.Fn.applyTo(RT.cons(_vals[r], f.Args)); } foreach (Ref r in _sets) { if (!_commutes.ContainsKey(r)) { r.EnterWriteLock(); locked.Add(r); } } // validate and enqueue notifications foreach (KeyValuePair<Ref, object> pair in _vals) { Ref r = pair.Key; r.Validate(pair.Value); r.notifyWatches(); } // at this point, all values calced, all refs to be written locked // no more client code to be called int msecs = System.Environment.TickCount; long commitPoint = getCommitpoint(); foreach (KeyValuePair<Ref, object> pair in _vals) { Ref r = pair.Key; object val = pair.Value; r.SetValue(val, commitPoint, msecs); } done = true; _info.Status.set(COMMITTED); } } catch (RetryEx) { // eat this so we retry rather than fall out } catch (Exception ex) { if (ContainsNestedRetryEx(ex)) { // Wrapped exception, eat it. } else { throw ex; } } finally { for (int k = locked.Count - 1; k >= 0; --k) { locked[k].ExitWriteLock(); } locked.Clear(); stop(done ? COMMITTED : RETRY); if (done) // re-dispatch out of transaction { foreach (Agent.Action action in _actions) { Agent.DispatchAction(action); } } _actions.Clear(); } } if (!done) throw new Exception("Transaction failed after reaching retry limit"); return ret; }
/// <summary> /// Invoke a function in a transaction /// </summary> /// <param name="fn">The function to invoke.</param> /// <returns>The value computed by the function.</returns> public static object runInTransaction(IFn fn) { LockingTransaction t = _transaction; if (t == null) _transaction = t = new LockingTransaction(); if (t._info != null) return fn.invoke(); return t.run(fn); }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <returns>The reduced value</returns> /// <remarks>Computes f(...f(f(f(i0,i1),i2),i3),...).</remarks> public object reduce(IFn f) { object ret = _n; for (int x = _n + 1; x < _end; x++) ret = f.invoke(ret, x); return ret; }
public void Setup() { _mocks = new MockRepository(); _fn = _mocks.StrictMock<IFn>(); RMExpect.Call(_fn.invoke()).Return(10); RMExpect.Call(_fn.invoke(null)).Return(new Cons(20,null)); _lc = new LazyCons(_fn); _values = new object[] { 10, 20 }; _mocks.ReplayAll(); }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <param name="start">An initial value to get started.</param> /// <returns>The reduced value</returns> public object reduce(IFn f, object start) { object ret = f.invoke(start, first()); for (ISeq s = next(); s != null; s = s.next()) ret = f.invoke(ret, s.first()); return ret; }
public object kvreduce(IFn f, object init) { int step = 0; for (int i = 0; i < _cnt; i += step) { object[] array = ArrayFor(i); for (int j = 0; j < array.Length; j++) { init = f.invoke(init, j + i, array[j]); if (RT.isReduced(init)) return ((IDeref)init).deref(); } step = array.Length; } return init; }
public object reduce(IFn f) { ISeq s = Current(); Object ret = s.first(); while (true) { s = s.next(); if (s == null) s = _all; ret = f.invoke(ret, s.first()); if (RT.isReduced(ret)) return ((IDeref)ret).deref(); } }
/// <summary> /// Start a transaction and invoke a function. /// </summary> /// <param name="fn">The function to invoke.</param> /// <returns>The value computed by the function.</returns> object Run(IFn fn) { // TODO: Define an overload called on ThreadStartDelegate or something equivalent. bool done = false; object ret = null; List<Ref> locked = new List<Ref>(); List<Notify> notify = new List<Notify>(); for (int i = 0; !done && i < RetryLimit; i++) { try { GetReadPoint(); if (i == 0) { _startPoint = _readPoint; _startTime = Environment.TickCount; } _info = new Info(RUNNING, _startPoint); ret = fn.invoke(); // make sure no one has killed us before this point, // and can't from now on if (_info.Status.compareAndSet(RUNNING, COMMITTING)) { foreach (KeyValuePair<Ref, List<CFn>> pair in _commutes) { Ref r = pair.Key; if (_sets.Contains(r)) continue; bool wasEnsured = _ensures.Contains(r); // can't upgrade read lock, so release ReleaseIfEnsured(r); TryWriteLock(r); locked.Add(r); if (wasEnsured && r.CurrentValPoint() > _readPoint ) throw _retryex; Info refinfo = r.TInfo; if ( refinfo != null && refinfo != _info && refinfo.IsRunning) { if (!Barge(refinfo)) { throw _retryex; } } object val = r.TryGetVal(); _vals[r] = val; foreach (CFn f in pair.Value) _vals[r] = f.Fn.applyTo(RT.cons(_vals[r], f.Args)); } foreach (Ref r in _sets) { TryWriteLock(r); locked.Add(r); } // validate and enqueue notifications foreach (KeyValuePair<Ref, object> pair in _vals) { Ref r = pair.Key; r.Validate(pair.Value); } // at this point, all values calced, all refs to be written locked // no more client code to be called int msecs = System.Environment.TickCount; long commitPoint = GetCommitPoint(); foreach (KeyValuePair<Ref, object> pair in _vals) { Ref r = pair.Key; object oldval = r.TryGetVal(); object newval = pair.Value; r.SetValue(newval, commitPoint, msecs); if (r.getWatches().count() > 0) notify.Add(new Notify(r, oldval, newval)); } done = true; _info.Status.set(COMMITTED); } } catch (RetryEx) { // eat this so we retry rather than fall out } catch (Exception ex) { if (ContainsNestedRetryEx(ex)) { // Wrapped exception, eat it. } else { throw; } } finally { for (int k = locked.Count - 1; k >= 0; --k) { locked[k].ExitWriteLock(); } locked.Clear(); foreach (Ref r in _ensures) r.ExitReadLock(); _ensures.Clear(); Stop(done ? COMMITTED : RETRY); try { if (done) // re-dispatch out of transaction { foreach (Notify n in notify) { n._ref.NotifyWatches(n._oldval, n._newval); } foreach (Agent.Action action in _actions) { Agent.DispatchAction(action); } } } finally { notify.Clear(); _actions.Clear(); } } } if (!done) throw new InvalidOperationException("Transaction failed after reaching retry limit"); return ret; }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <param name="start">An initial value to get started.</param> /// <returns>The reduced value</returns> public object reduce(IFn f, object start) { object ret = start; for (int x = _i; x >= 0; x--) ret = f.invoke(ret, _v.nth(x)); return ret; }
public object reduce(IFn f) { Object init; if (_cnt > 0) init = ArrayFor(0)[0]; else return f.invoke(); int step = 0; for (int i = 0; i < _cnt; i += step) { Object[] array = ArrayFor(i); for (int j = (i == 0) ? 1 : 0; j < array.Length; ++j) { init = f.invoke(init, array[j]); if (RT.isReduced(init)) return ((IDeref)init).deref(); } step = array.Length; } return init; }
void CommuteRoot(IFn fn) { object newRoot = fn.invoke(_root); Validate(getValidator(), newRoot); object oldRoot = _root; _root = newRoot; ++_rev; NotifyWatches(oldRoot, newRoot); }
public object Fold(IFn combinef, IFn reducef, IFn fjtask, IFn fjfork, IFn fjjoin) { return NodeSeq.KvReduce(_array, reducef, combinef.invoke()); }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <param name="start">An initial value to get started.</param> /// <returns>The reduced value</returns> /// <remarks>Computes f(...f(f(f(start,i0),i1),i2),...).</remarks> public object reduce(IFn f, object start) { object ret = f.invoke(start, _n); for (int x = _n + 1; x < _end; x++) ret = f.invoke(ret, x); return ret; }
// Not in Java original /// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <returns>The reduced value</returns> public object reduce(IFn f) { object ret = _v.nth(_i); for (int x = _i-1; x >= 0; x--) ret = f.invoke(ret, _v.nth(x)); return ret; }
public object kvreduce(IFn f, object init) { init = _hasNull ? f.invoke(init,null,_nullValue) : init; if (RT.isReduced(init)) return ((IDeref)init).deref(); if (_root != null) return _root.KVReduce(f, init); return init; }
public object reduce(IFn f) { if (_oa != null) { object ret = _oa[_i]; for (int x = _i + 1; x < _oa.Length; x++) ret = f.invoke(ret, _oa[x]); return ret; } object ret1 = _ilist[_i]; // JAVA 1112 wraps in RT.prepRet for (int x = _i + 1; x < _ilist.Count; x++) ret1 = f.invoke(ret1, _ilist[x]); // JAVA 1112 wraps in RT.prepRet return ret1; }
/// <summary> /// Compute and set a new value. Spin loop for coordination. /// </summary> /// <param name="f">The function to apply to current state and additional arguments.</param> /// <param name="arg1">First additional argument.</param> /// <param name="arg2">Second additional argument.</param> /// <returns>The new value.</returns> /// <remarks>Lowercase name for core.clj compatability.</remarks> public object swap(IFn f, Object arg1, Object arg2) { for (; ; ) { object v = deref(); object newv = f.invoke(v, arg1, arg2); Validate(newv); if (_state.CompareAndSet(v, newv)) { if (v != newv) notifyWatches(); return newv; } } }
public object reduce(IFn f, object start) { int step = 0; for (int i = 0; i < _cnt; i += step) { Object[] array = ArrayFor(i); for (int j = 0; j < array.Length; ++j) { start = f.invoke(start, array[j]); if (RT.isReduced(start)) return ((IDeref)start).deref(); } step = array.Length; } return start; }
public object kvreduce(IFn f, object init) { for (int i = 0; i < _array.Length; i += 2) { init = f.invoke(init, _array[i], _array[i + 1]); if (RT.isReduced(init)) return ((IDeref)init).deref(); } return init; }
/// <summary> /// Invoke an <see cref="IFn">IFn</see> on a value to validate it. /// </summary> /// <param name="vf">The <see cref="IFn">IFn</see> to invoke.</param> /// <param name="val">The value to validate.</param> /// <remarks>Uneventful return marks a successful validation. /// To indicate a failed validation, the validation function should return <value>false</value> or throw an exception. /// <para>This appears in multiple places. Should find it a common home?</para></remarks> protected internal static void Validate(IFn vf, object val) { if (vf == null) return; bool ret = false; try { ret = RT.booleanCast(vf.invoke(val)); } catch (Exception e) { throw new InvalidOperationException("Invalid reference state", e); } if ( ! ret ) throw new InvalidOperationException("Invalid reference state"); }
public object reduce(IFn rf) { Object ff = first(); Object ret = ff; Object v = _f.invoke(ff); while (true) { ret = rf.invoke(ret, v); if (RT.isReduced(ret)) return ((IDeref)ret).deref(); v = _f.invoke(v); } }
public static object ApplyToHelper(IFn fn, ISeq argList) { switch (RT.BoundedLength(argList, 20)) { case 0: argList = null; return fn.invoke(); case 1: return fn.invoke(Util.Ret1(argList.first(),argList=null)); case 2: return fn.invoke(argList.first() , Util.Ret1((argList = argList.next()).first(),argList = null) ); case 3: return fn.invoke(argList.first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 4: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 5: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 6: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 7: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 8: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 9: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 10: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 11: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 12: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 13: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 14: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 15: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 16: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 17: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 18: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 19: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); case 20: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , Util.Ret1((argList = argList.next()).first(), argList = null) ); default: return fn.invoke(argList.first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , (argList = argList.next()).first() , RT.SeqToArray<object>(Util.Ret1(argList.next(),argList=null))); } }
public static object runInTransaction(IFn fn) { // TODO: This can be called on something more general than an IFn. // We can could define a delegate for this, probably use ThreadStartDelegate. // Should still have a version that takes IFn. LockingTransaction t = _transaction; if (t == null) _transaction = t = new LockingTransaction(); if (t._info != null) return fn.invoke(); return t.Run(fn); }
public static object KvReduce(object[] array, IFn f, object init) { for (int i = 0; i < array.Length; i += 2) { if (array[i] != null) init = f.invoke(init, array[i], array[i + 1]); else { INode node = (INode)array[i + 1]; if (node != null) init = node.KVReduce(f, init); } if (RT.isReduced(init)) return ((IDeref)init).deref(); } return init; }
/// <summary> /// Reduce the collection using a function. /// </summary> /// <param name="f">The function to apply.</param> /// <param name="start">An initial value to get started.</param> /// <returns>The reduced value</returns> public object reduce(IFn f, object start) { object ret = f.invoke(start, _v.nth(_i)); for (int x = _i + 1; x < _v.count(); x++) ret = f.invoke(ret, _v.nth(x)); return ret; }