SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using ParsecSharp;
  3. using ParsecSharp.Internal;
  4.  
  5. namespace ParsecSharp.Extensions
  6. {
  7.     public static class Parser
  8.     {
  9.         public static Parser<TToken, T> Memoize<TToken, T>(Parser<TToken, T> parser)
  10.         {
  11.             var cache = new Dictionary<IParsecStateStream<TToken>, Result<TToken, T>>();
  12.             return Builder.GetState<TToken>()
  13.                 .Bind(state => (cache.TryGetValue(state, out var result))
  14.                     ? Builder.Create<TToken, T>(_ => result)
  15.                     : parser.ModifyResult(
  16.                         (_, fail) => { cache[state] = fail; return fail; },
  17.                         (_, success) => { cache[state] = success; return success; }));
  18.         }
  19.     }
  20. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top