Skip to content

Instantly share code, notes, and snippets.

@acple acple/Memoize.cs
Last active Mar 18, 2019

Embed
What would you like to do?
メッメモリリークだあーーにげろーー
using System.Collections.Generic;
using ParsecSharp;
using ParsecSharp.Internal;
namespace ParsecSharp.Extensions
{
public static class Parser
{
public static Parser<TToken, T> Memoize<TToken, T>(Parser<TToken, T> parser)
{
var cache = new Dictionary<IParsecStateStream<TToken>, Result<TToken, T>>();
return Builder.GetState<TToken>()
.Bind(state => (cache.TryGetValue(state, out var result))
? Builder.Create<TToken, T>(_ => result)
: parser.ModifyResult(
(_, fail) => { cache[state] = fail; return fail; },
(_, success) => { cache[state] = success; return success; }));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.