Changeset 389

Show
Ignore:
Timestamp:
07/21/08 09:00:32 (4 months ago)
Author:
FeepingCreature
Message:
  • Small improvement to reverse
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tools/tools/base.d

    r385 r389  
    462462        LHS lhs; 
    463463        typeof(function() { LHS lhs; RHS rhs; "~CODE~" }()) "~OP~"(RHS)(RHS rhs) { "~CODE~" } 
    464         typeof(function() { LHS lhs; RHS rhs; "~CODE~" }()) opCall(RHS)(RHS rhs) { "~CODE~" } 
     464        alias "~OP~" opCall; 
    465465      }" 
    466466  )~" 
     
    469469    ?" 
    470470    typeof(function() { Unstatic!(LHS) lhs; "~CODE~" }()) "~OP~"_r(LHS)(LHS lhs) { "~CODE~" } 
    471     typeof(function() { Unstatic!(LHS) lhs; "~CODE~" }()) opCall  (LHS)(LHS lhs) { "~CODE~" } 
     471    alias "~OP~"_r opCall; 
    472472    " 
    473473    :" 
    474474    __"~NAME~"!(Unstatic!(LHS)) "~OP~"_r(LHS)(LHS lhs) { __"~NAME~"!(Unstatic!(LHS)) res=void; res.lhs = lhs; return res; } 
    475     __"~NAME~"!(Unstatic!(LHS)) opCall(LHS)(LHS lhs) { __"~NAME~"!(Unstatic!(LHS)) res=void; res.lhs = lhs; return res; } 
     475    alias "~OP~"_r opCall; 
    476476    " 
    477477  )~"} 
     
    588588template Length(T, U) { const int Length=CallConfig!(T, U).length; } 
    589589 
     590struct fix_late_holder(LHS, RHS) { 
     591  LHS lhs; RHS rhs; 
     592  typeof(lhs(rhs, TupleInit!(X))) opCall(X...)(X par) { 
     593    return lhs(rhs, par); 
     594  } 
     595} 
     596 
    590597mixin(Operator!("fix", " 
    591598  // pragma(msg, \"fix: \"~LHS.stringof~\" -> \"~RHS.stringof); 
    592599  struct holder { 
    593600    LHS fix_lhs; Unstatic!(RHS) fix_rhs; 
    594     static if(!is(Params!(LHS))) 
    595       static assert(false, \"fix: \"~LHS.stringof~\" is not callable!\"); 
    596     else mixin(\"Ret!(LHS) call(\"~refToParamList(\"Params!(LHS)[Length!(RHS, LHS)..$]\", isRef!(LHS)[Length!(RHS, LHS)..$])~\") { 
     601    mixin(\"Ret!(LHS) call(\"~refToParamList(\"Params!(LHS)[Length!(RHS, LHS)..$]\", isRef!(LHS)[Length!(RHS, LHS)..$])~\") { 
    597602      return mixin(\\\"fix_lhs(\\\"~Value!(RHS, Ret!(LHS) function(Params!(LHS)[0..Length!(RHS, LHS)]), \\\"fix_rhs\\\")~\\\" 
    598603        \"~((Length!(RHS, LHS) && (Params!(LHS).length-Length!(RHS, LHS)))?\", \":\"\")~refToValueList(isRef!(LHS)[Length!(RHS, LHS)..$])~\")\\\"); 
    599604    }\"); 
    600605  } 
    601   auto res=new holder; res.fix_lhs=lhs; res.fix_rhs=rhs; 
    602   return &res.call; 
     606  static if(!is(Params!(LHS))) { 
     607    // static assert(false, \"fix: \"~LHS.stringof~\" is not callable!\"); 
     608    fix_late_holder!(LHS, RHS) res = void; 
     609    res.lhs = lhs; res.rhs = rhs; return res; 
     610  } else { 
     611    auto res=new holder; res.fix_lhs=lhs; res.fix_rhs=rhs; 
     612    return &res.call; 
     613  } 
    603614")); 
    604615 
     
    626637_partial!(C) partial(C)(C callable) { _partial!(C) res; res.callable=callable; return res; } 
    627638 
    628 mixin(Operator!("chain", " 
    629   struct holder { 
    630     LHS _lhs; RHS _rhs; 
    631     Ret!(RHS) call(Params!(LHS) left, Params!(RHS)[Params!(LHS).length..$] right) { 
    632       return _rhs(_lhs(left), right); 
    633     } 
    634   } 
    635   auto res=new holder; res._lhs=lhs; res._rhs=rhs; 
    636   return &res.call; 
    637 ")); 
     639struct chain_holder(LHS, RHS) { 
     640  LHS _lhs; RHS _rhs; 
     641  Ret!(typeof(function(X foo) { return _rhs(_lhs(foo)); })) opCall(X...)(X par) { 
     642    return _rhs(_lhs(par)); 
     643  } 
     644
     645mixin(Operator!("chain", " chain_holder!(LHS, RHS) res = void; res._lhs = lhs; res._rhs = rhs; return res; ")); 
    638646 
    639647template Reverse(T...) { 
     
    646654  static if(LEN==1) const string ReverseIndices=BASE~"[0]"; else 
    647655  const string ReverseIndices=BASE~"["~(LEN-1).stringof~"], "~ReverseIndices!(BASE, LEN-1); 
     656} 
     657 
     658struct reverse_holder(LHS) { 
     659  LHS lhs; 
     660  typeof(lhs(TupleInit!(Reverse!(X)))) opCall(X...)(X par) { 
     661    return mixin("lhs("~ReverseIndices!("par", X.length)~")"); 
     662  } 
    648663} 
    649664 
     
    655670  } else { 
    656671    // reverse parameter order 
    657     struct reverted { 
    658       LHS _lhs; 
    659       Ret!(LHS) call(Reverse!(Params!(LHS)) rev) { 
    660         const string value=\"_lhs(\" ~ ReverseIndices!(\"rev\", Params!(LHS).length) ~ \") \"; 
    661         mixin(\"return \"~value~\";\"); 
     672    static if (is(Params!(LHS))) { 
     673      static typeof(mixin(\"lhs(\"~ReverseIndices!(\"TupleInit!(Reverse!(Params!(LHS)))\", Params!(LHS).length)~\")\")) 
     674        res_fn(typeof(lhs) lhs, Reverse!(Params!(LHS)) rev) { 
     675          return mixin(\"lhs(\"~ReverseIndices!(\"rev\", Params!(LHS).length)~\")\"); 
    662676      } 
    663     } 
    664     auto r=new reverted; r._lhs=lhs; 
    665     return &r.call; 
     677      return lhs /apply/ &res_fn; 
     678    } else { 
     679      reverse_holder!(LHS) res = void; 
     680      res.lhs = lhs; 
     681      return res; 
     682    } 
    666683  }", true 
    667684)); 
     
    945962T delegate(T) Loop(T)(int count, T delegate(T) dg) { 
    946963  assert(count>0); 
    947   if (count==1) return dg; else return Loop(count-1, dg) /chain/ dg; 
     964  if (count==1) return dg; else { 
     965    auto foo = Loop(count-1, dg) /chain/ dg; 
     966    return foo /apply/ (typeof(foo) foo, T t) { return foo(t); }; 
     967  } 
    948968} 
    949969 
  • trunk/tools/tools/functional.d

    r317 r389  
    100100 
    101101mixin(Operator!("sort", "auto res=lhs.dup; res.sort; return res;", true)); 
     102 
     103struct curry_holder(LHS, G...) { 
     104  LHS lhs; G g; 
     105  const string CODE = " 
     106    static if (is(typeof(lhs(g, par)))) return lhs(g, par); 
     107    else { 
     108      curry_holder!(LHS, G, X) res = void; 
     109      res.lhs = lhs; 
     110      foreach (id, entry; g) res.g[id] = entry; 
     111      foreach (id, entry; par) res.g[G.length + id] = entry; 
     112      return res; 
     113    }"; 
     114  Ret!(typeof(function { X par; mixin(CODE); })) opCall(X...)(X par) { 
     115    mixin(CODE); 
     116  } 
     117} 
     118 
    102119mixin(Operator!("curry", " 
    103   static if(Params!(LHS).length!>1) return lhs; 
    104   else return (LHS _lhs, Params!(LHS)[0] first) { return _lhs /fix/ first /curry; } /fix/ lhs; 
     120  static if (is(Params!(LHS))) { 
     121    static if (Params!(LHS).length!>1) return lhs; 
     122    else return (LHS _lhs, Params!(LHS)[0] first) { return _lhs /fix/ first /curry; } /fix/ lhs; 
     123  } else { 
     124    curry_holder!(LHS) res = void; 
     125    res.lhs = lhs; return res; 
     126  } 
    105127", true)); 
     128 
     129template CurryRes(S, T...) { 
     130  static if (T.length == 1) alias typeof(Init!(S) (Init!(T[0]))) CurryRes; 
     131  else alias typeof((Init!(S) (Init!(T[0])) /uncurry) (Init!(T[1 .. $]))) CurryRes; 
     132} 
     133 
    106134template CollectParameters(T) { 
    107135  static if (is(T==function) || is (T==delegate)) { 
     
    113141  } 
    114142} 
     143 
    115144template CurryCalls(int depth) { 
    116145  static if (!depth) const string CurryCalls=""; 
    117146  else const string CurryCalls=CurryCalls!(depth-1)~"(cp["~depth.stringof~"-1])"; 
    118147} 
     148  
    119149mixin(Operator!("uncurry", " 
    120150  alias CollectParameters!(LHS).tuple CPAR;