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 9253085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:07:32+00:00 2026-06-18T11:07:32+00:00

I have written a program or algorithm that parses command line arguments. First, one

  • 0

I have written a program or algorithm that parses command line arguments. First, one needs to specify valid arguments and their attributes (flag, separator and so on).

Now, I have a helper function that returns: Tuple<Tuple<Argument, String>, String>: the first element in the Tuple (which is a tuple too) contains a “key-value-pair”, null if no pair could be found. The second element contains a string that was identified as an option and separator was " " (meaning the value will be the next element in the command line)

The code for that function is as follows:

private Tuple<Tuple<Argument, String>, Argument> f(string e, List<Argument> valid) {
    var p = valid.Find( x => { return e.StartsWith( x.value ); } );
    if ( p == null )
        return new Tuple<Tuple<Argument, String>, Argument>( null, null );
    if ( p.flag )
        return new Tuple<Tuple<Argument, String>, Argument>( new Tuple<Argument, String>( p, "" ), null );
    if ( p.separator.Equals(" ") && p.value.Length == e.Length )
        return new Tuple<Tuple<Argument, String>, Argument>( null, p );
    var si = e.IndexOf(p.separator);
    if ( si != p.value.Length )
        return new Tuple<Tuple<Argument, String>, Argument>( null, null );
    return new Tuple<Tuple<Argument, String>, Argument>( new Tuple<Argument, String>( p, e.Substring( si + p.separator.Length ) ), null );
}

Is there any way to write this shorter? Do I have to explicitly put the Generics there?

Obviously, creating own classes for that would be a solution. But isn’t there another?

For reference, the same code in Scala looks like this:

val po = valid.find(p => e.startsWith(p.value))
po match {
    case Some(p) =>
        if ( p.flag )
            (Some((p, "")), None)
        else if ( p.separator.matches(" |\t") && p.value.length == e.length )
            (None, Some(p))
        else {
            val si = e.indexOf(p.separator)
            if ( si != p.value.length )
                (None, None)
            else
                (Some((p, e.substring(si + p.separator.length))), None)
        }
    case _ => (None, None)
}

Plus, is there a way to initiate Lists or Tuples in a compact way? As in List(1, 2, 3, 4) or (Key, Value) instead of Tuple<KeyType, ValueType>(Key, Value)

Regards,
Danyel.

  • 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-06-18T11:07:34+00:00Added an answer on June 18, 2026 at 11:07 am

    You can use typedefs to clean up the code. Technically, C# doesn’t have typedefs, but you can use usings to simulate them. Something like this:

    using MyTuple = Tuple<Tuple<Argument, string>, Argument>;
    using MyTuple2 = Tuple<Argument, string>;
    
    public class MyClass {
    
        private MyTuple f(string e, List<Argument> valid) {
            var p = valid.Find( x => { return e.StartsWith( x.value ); } );
            if ( p == null )
               return new MyTuple( null, null );
            if ( p.flag )
               return new MyTuple( new MyTuple2( p, "" ), null );
            if ( p.separator.Equals(" ") && p.value.Length == e.Length )
                return new MyTuple( null, p );
            var si = e.IndexOf(p.separator);
            if ( si != p.value.Length )
                return MyTuple( null, null );
            return new MyTuple( new MyTuple2( p, 
                e.Substring( si + p.separator.Length ) ), null );
        }
    }
    

    Regarding your question about initialization, I’m not sure i understand. Tuples already have a compact initialization method, using constructor arguments.

    Lists can use initializers, like this:

    var s = new List<string> { "one", "two", "three" };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written down the following program that uses the quicksort algorithm to sort
I have written a program that uses qhttp to get a webpage. This works
I have program written in C. It takes 2 arguments username/password and try to
I have written a java program that tests the speed of a couple of
I have a program that needs to retrieve some data about a set of
I have written a very basic encryption program, and whilst writing the decrypting algorithm
I have written a program that generates a few N x N matrices for
I have written php program and uploaded on server. I want run this program
I have written a program in VB6. When I compile it and send it
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>

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.