Author Topic: .Net & Lambda...?  (Read 2117 times)

0 Members and 1 Guest are viewing this topic.

rugaroo

  • Bull Frog
  • Posts: 378
  • The Other CAD Guy
.Net & Lambda...?
« on: March 03, 2012, 12:04:15 AM »
Sorry if this has been covered, but I couldn't find anything in my searches, and was hoping someone could shed some light on something for me.

I have an old lisp I am trying to convert over to .Net and my experience with Lambda is killing me right now :(.

First off, I was trying to follow JTB World's little example (http://blog.jtbworld.com/2008/01/lambda-expressions-and-mapcar-with.html), but could not see the way to 'convert' it properly.

I did create the MapCar function as shown, but that is where I am now lost.

Code: [Select]
Function MapCar(ByVal f As Func(Of Object, Object), ByVal ParamArray x() As Object) As Array
            Dim res(UBound(x(0))) As Object
            For i As Integer = 0 To UBound(x(0))
                res(i) = f(x(0)(i))
            Next i
            Return res
        End Function

Now here is the code from my lisp in short...

Code: [Select]
(MAPCAR
    '(LAMBDA (OBJECTTYPE)
       (ENTS-SETVAR (STRCAT OBJECTTYPE".BLKNAME")
      )
       )
     )
    (STRINGTOLST
      (ENTS-GETVAR "ENTS.OBJECTTYPE")
      ","
      "\""
      T
    )
  )

Now would it be safe to say that to make things simple, I could just do something like:

Code: [Select]
For Each EntType In Application.GetSystemVariable(ENTS.OBJECTTYPE)
Application.SetSystemVariable(EntType)

Sorry if this is too rudimentary but I am half asleep and scratchin my noggin...
LDD06-09 | C3D 04-19 | Infraworks 360 | VS2012-VS2017

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: .Net & Lambda...?
« Reply #1 on: March 03, 2012, 12:22:44 AM »
Lambda is an anonymous function i.e. it doesn't have a name and isn't callable using a function name. This is why they are inline with other bits of code.

To write lambda functions, check out this:
http://msdn.microsoft.com/en-us/library/bb531253.aspx
« Last Edit: March 03, 2012, 12:26:59 AM by Keith™ »
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
« Last Edit: March 03, 2012, 03:26:06 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: .Net & Lambda...?
« Reply #3 on: March 03, 2012, 03:13:14 AM »
Hi,

You could define a generic Mapcar function equivalent this way:

Code - C#: [Select]
  1. public static IEnumerable<TResult> Map<TSource, TResult>(
  2.    this IEnumerable<TSource> source, Func<TSource, TResult> func)
  3. {
  4.    foreach (TSource item in source) yield return func(item);
  5. }
  6.  

Code - Visual Basic: [Select]
  1. <System.Runtime.CompilerServices.Extension> _
  2. Public Shared Function Map(Of TSource, TResult)(source As IEnumerable(Of TSource), _
  3. func As Func(Of TSource, TResult)) As IEnumerable(Of TResult)
  4.         For Each item As TSource In source
  5.                 yield Return func(item)
  6.         Next
  7. End Function

But the built-in Enumerable.Select<TSource, TResult> LINQ extension method works exactly the same.

Then you can pass to the Select (or Map) function a lambda expression as argument (as IEnumerable<T> uses lazy evaluation, you may have to force evaluation)

Code - C#: [Select]
  1. int[] ints = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  2. IEnumerable<int> squared = ints.Select(x => x * x);
  3.  
  4. List<string> lst = new List<string>(3) { "foo", "bar", "baz" };
  5. lst = lst.Select(s => s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower()).ToList();

Code - Visual Basic: [Select]
  1. Dim ints As Integer() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  2. Dim squared As IEnumerable(Of Integer) = ints.Select(Function(x) x * x)
  3.  
  4. Dim lst As New List(Of String)(3) From { "foo", "bar", "baz" }
  5. lst = lst.Select(Function(s) s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower()).ToList()
« Last Edit: March 03, 2012, 03:24:40 AM by gile »
Speaking English as a French Frog