TheSwamp

Code Red => .NET => Topic started by: rugaroo on March 03, 2012, 12:04:15 AM

Title: .Net & Lambda...?
Post by: rugaroo 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 (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...
Title: Re: .Net & Lambda...?
Post by: Keith™ 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
Title: Re: .Net & Lambda...?
Post by: Kerry on March 03, 2012, 02:44:16 AM
:)

or here
http://msdn.microsoft.com/en-us/library/bb397687(v=vs.110).aspx

and from the cookbook ..
http://msdn.microsoft.com/en-us/library/orm-9780596516109-03-09.aspx
Title: Re: .Net & Lambda...?
Post by: gile 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()