Author Topic: MAPCAR for .net collections  (Read 3899 times)

0 Members and 1 Guest are viewing this topic.

Peter Jamtgaard

  • Guest
MAPCAR for .net collections
« on: May 28, 2011, 11:57:53 AM »
Do any of you have examples of a function something
similar to mapcar (in LISP) for use with .net collections?

kaefer

  • Guest
Re: MAPCAR for .net collections
« Reply #1 on: May 28, 2011, 01:11:48 PM »
Do any of you have examples of a function something
similar to mapcar (in LISP) for use with .net collections?

Try LINQ, for projections either the Select method or the select keyword.

Simple C# example for AutoCAD...

Code: [Select]
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Runtime;
    using System.Linq;
    using System.Collections.Generic;

    public class Class1
    {
        [CommandMethod("Foo")]
        public void Foo(){
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction()){
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                IEnumerable<ObjectId> btseq = bt.Cast<ObjectId>();
                IEnumerable<BlockTableRecord> btrs1 =
                    btseq.Select(oid => (BlockTableRecord)tr.GetObject(oid, OpenMode.ForRead));
                IEnumerable<BlockTableRecord> btrs2 =
                    from oid in btseq select (BlockTableRecord)tr.GetObject(oid, OpenMode.ForRead);
                tr.Commit();
            }
        }
    }

Peter Jamtgaard

  • Guest
Re: MAPCAR for .net collections
« Reply #2 on: May 31, 2011, 08:52:32 AM »
OK how about a mapcar like expression or function that works with arrays?

Like I want to convert an array of fullnames to an array of filenames for a list box control.


kaefer

  • Guest
Re: MAPCAR for .net collections
« Reply #3 on: May 31, 2011, 09:31:31 AM »
OK how about a mapcar like expression or function that works with arrays?

Like I want to convert an array of fullnames to an array of filenames for a list box control.

Why shouldn't that work, too? Like this:
Code: [Select]
   using System.IO;
    ...
    string[] files = Directory.GetFiles("c:/temp").Select(fqn => Path.GetFileName(fqn)).ToArray();

You wouldn't want to go down the road of declaring your "mapcar" in terms of a function that takes a System.Func<string,string> and a string array and returns a string array. That's cumbersome and not very generic.

Cheers
« Last Edit: May 31, 2011, 11:49:15 AM by kaefer »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: MAPCAR for .net collections
« Reply #4 on: May 31, 2011, 09:59:33 AM »
Hi,

If you want to use .NET with a more 'LISP like style', why not using F#:

Code: [Select]
let file = Directory.GetFiles("C:\\Temp") |> Array.map(fun f -> Path.GetFileName(f))
Speaking English as a French Frog

kaefer

  • Guest
Re: MAPCAR for .net collections
« Reply #5 on: May 31, 2011, 12:06:12 PM »
If you want to use .NET with a more 'LISP like style', why not using F#:

Code: [Select]
let file = Directory.GetFiles("C:\\Temp") |> Array.map(fun f -> Path.GetFileName(f))

Thanks for bringing that up. You could write this even in a compositional way:
Code: [Select]
open System.IO
let getFileNamesOnly = Directory.GetFiles >> Array.map Path.GetFileName
getFileNamesOnly @"C:\Temp"

That is, combine Directory.GetFiles, signature (string->string[]), with a projection (string[]->string[]), so that the resulting function getFileNamesOnly has the signature (string->string[]): it takes a string and returns a string array.

All the best
« Last Edit: June 01, 2011, 08:51:35 AM by kaefer »