Porting to NETMF

I have this neat little quick-function in .Net 4.5 program that I would like to re-use…but I dont think its possible…


  Func<string, string> lookupFunc =
                option => args.Where(s => s.StartsWith(option)).Select(s => s.Substring(option.Length)).FirstOrDefault();

Any ideas how to make this short and precise in NETMF…?

Unlikely, because the version of C# on netmf doen’t support this. There would be no way to make it simple and elegant like that.

1 Like

ReSharper would convert the linq to classic Code in a key press.
but lets see:


delegate string LoopFunc(string);

string LoopFuncProc(string option)
{
  foreach(var s in args)
  {
    if(s.StartsWith(option))
    {
      return s.SubString(Option.Length);
    }
  }
  return null;
}

...
LoopFunc loopFunc = LoopFuncProc;

should work I guess. (not tested)

1 Like

Except that NETMF string type doesn’t support StartsWith(). But, that’s easy enough to work around.

1 Like

Thanks everybody, interesting to see how fancy new frameworks are destroying the ability to think. I ended up with this peice of code:


string lookupFunc(string option, string[] args) 
{
    foreach (var s in args) 
        if (s.IndexOf(option) == 0) return s.Substring(option.Length);
    return null;
}

Essentially, its easier to read, and works like the other one. Shame on me. ::slight_smile:

Thanks again!

1 Like

@ njbuch - Personally, I find your function easier to read than the Linq solution. However, a solution based on char arrays would be even better for microcontrollers since it would avoid GC. I don’t have time at the moment to create one, though. I’m sure someone else already has. Also, I haven’t used the NETMF RegEx class much but I’d be curious to see how GC friendly a regex based solution to this problem would be.

@ ianlee74 - Avoiding string manipulation isn’t just a good idea on NETMF…

When you see your “% time in GC” performance counter go north of 80%, you know someone’s been messing around with strings again…

Indeed. But, it sure is a lot more noticeable when the GC kicks in :slight_smile:

@ njbuch - Actually your function always returns the same string as option or null.
So an even faster solution would be:

 args) 
{
    foreach (var s in args) 
        if (s.IndexOf(option) == 0) return option;
    return null;
}

or even

 args) 
{
    foreach (var s in args) 
        if (s.IndexOf(option) == 0) return true;
    return false;
}

Edit:
about readability of linq.
I think an actual programmer (even more if old School) will understand plain C# Code more easy.
But I think that linq Statements (no matter if with Extension methods or from, select, … Keywords) is more easy to understand for others, since it’s actually quite Close to a normal sentence.
If you get used to read it like this, t hen it’s quite easy.
What gets complicated is if the Statement gets too Long, since you have no results in the middle you can jump back as an reminder.

@ Reinhard Ostermeier - You need to check the semantics of Substring, the function returns the remainder of the arg string after the option.

Passing “time=12:00” returns “12:00”. I used this for analyzing command strings coming from a server, where there can be random sequence of options.

@ njbuch - Ah, you are right, I got that mixed up.

I’ve said it before. If this trend keeps up, we’ll all be programming with a Kinect and interpretive dance.

(My favorite tool, Resharper, keeps suggesting code mutations that won’t compile under the netmf-compatible compiler.)

Yes that’s true, but I don’t blame this fully to ReSharper. Of course it could look which compiler is used and do not offer the new language features. But by this it also keeps my mind sharpened at what ReSharper actually does to my code.
I mean ReSharper is good, really good. but I would never trust it 100% for a commercial project.
I do not even trust myself a 100% for that (ReSHarper found already a lot of Bugs in my Code that only would have shown up in some Special cases), so I think me and ReSharper are a good team, looking at what each other is doing
8)

2 Likes

Maybe it’s pedantic, but what we’re discussing here isn’t LINQ at all. LINQ is a functional-ish DSL in C# that the compiler translates into calls to extension methods. What we’re talking about here is a few calls to those extension methods using the lambda syntax. It isn’t LINQ at all, it’s just plain old C# code. Out in the wild, actual LINQ code is vanishingly rare, in my experience.

Now, if I were to give my opinion on what’s more readable, it would depend on the situation, but unless things get pretty hairy, I think that the lambda syntax is actually very readable. It’s often possible to express an algorithm very elegantly and succinctly, which helps to get the idea across without burying it in a bunch of syntax.

The least readable code is anything done in the “fluent” style. Whoever thought that up has a serious brain problem, and ought to get that checked out.

@ godefroi - You are right, that the original Code in the 1st post isn’t actually linq, but this is what linq is translated to.
Some call it linq with lamdas and Extension methods, that’s why I call this linq too.
If this is actually wrong, I still don’t care :smiley: