Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:18:15+00:00 2026-05-24T05:18:15+00:00

I’ve been implementing an enhanced Shunting-Yard algorithm for parsing an arithmetic expression. One aspect

  • 0

I’ve been implementing an enhanced Shunting-Yard algorithm for parsing an arithmetic expression. One aspect of the algorithm, is that it maintains a Queue, and a Stack.

In my implementation, the Queue contains Expressions and Operators.
The Stack contains Operators and Parenthesis.

Expressions, Parenthesis, and Operators have nothing in common that warrants any two of them having a shared interface.

Approaches:

  • My current implementation consists of Expression and Operator implementing a INotParanthesis. Operator and Paranthesis implement a INotExpression. I then declare Queue <INotParanthesis>, and Stack <INotExpression>.

    I don’t like this implementation – these interfaces seem very much a hack for the purpose of cleaner algorithm code. I also believe that interfaces should describe what an object is, as opposed to what it isn’t.

  • On the other hand, I also don’t want to use collections of <Object>, as it can be difficult to be certain of the correctness of such code.

  • The only one I’ve come up with, so far, is implementing my own NonParanthesisQueue and NonExpressionStack containers. This has the advantage of more consistent type checking on objects getting pulled out of those containers – and the disadvantage of a lot more code.

Are there any reasonable alternatives to my approaches?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:18:16+00:00Added an answer on May 24, 2026 at 5:18 am

    It sounds like what you really want is a sum type. Although C# does not have these built in, there’s a trick from functional programming that you can use called Church encoding to achieve this. It’s completely type safe with no casts involved, however it’s a bit weird to use in C# mostly due to the limitations of the type inference.

    The main trick is that instead of using properties and checks to retrieve one of the two alternatives, we have a higher order function Map that takes two functions as arguments and calls the appropriate one depending on which alternative was present. Here’s how you would use it:

    var stack = new Stack<IEither<Operator, Parenthesis>>();
    
    stack.Push(new Left<Operator, Parenthesis>(new Operator()));
    stack.Push(new Right<Operator, Parenthesis>(new Parenthesis()));
    
    while (stack.Count > 0)
    {
        stack.Pop().Map(op  => Console.WriteLine("Found an operator: " + op),
                        par => Console.WriteLine("Found a parenthesis: " + par));
    }
    

    Here’s the implementation of IEither, Left and Right. They are fully generic and could be used anywhere you want a sum type.

    public interface IEither<TLeft, TRight>
    {
        TResult Map<TResult>(Func<TLeft, TResult> onLeft, Func<TRight, TResult> onRight);
        void Map(Action<TLeft> onLeft, Action<TRight> onRight);
    }
    
    public sealed class Left<TLeft, TRight> : IEither<TLeft, TRight>
    {
        private readonly TLeft value;
    
        public Left(TLeft value)
        {
            this.value = value;
        }
    
        public TResult Map<TResult>(Func<TLeft, TResult> onLeft, Func<TRight, TResult> onRight)
        {
            return onLeft(value);
        }
    
        public void Map(Action<TLeft> onLeft, Action<TRight> onRight)
        {
            onLeft(value);
        }
    }
    
    public sealed class Right<TLeft, TRight> : IEither<TLeft, TRight>
    {
        private readonly TRight value;
    
        public Right(TRight value)
        {
            this.value = value;
        }
    
        public TResult Map<TResult>(Func<TLeft, TResult> onLeft, Func<TRight, TResult> onRight)
        {
            return onRight(value);
        }
    
        public void Map(Action<TLeft> onLeft, Action<TRight> onRight)
        {
            onRight(value);
        }
    }
    

    References:

    • Tagged union
    • Algebraic data type
    • Church encoding
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.