locked
How to using Std:transform + Lambda function? RRS feed

  • Question


  • Hi, in Autolisp language I can using Mapcar + lambda function to work with multiple list or array,it's like that: 

    (mapcar '(lambda(a,b,c) ...) list1 list2 list3) return a custom list from combine 3 list 1,2,3

    Example:

    (mapcar '(lambda(a,b,c) (* a b c)) '(1 2 3 4) '(1 2 3 4) '(4 3 2 1)) return '(4 12 18 16)

    I am looking to use a similar function in c ++, i just thought of using transform because 

    I want to create a list of 2 Circle class elements or 3,4.. , made up from 3 arrays:

    array1 <Point3D:Centerpoint>,  array2 <vector3D: Oz>, array3 <float: radius>

    This is what I have:

    std::vector<PT3D> vcen { PT3D{1,1,1}, PT3D{4,4,4}}; //List CenterPoint3D <PT3D> to define Circles std::vector<VT3D> vOz{ normalZ , normalZ }; //List vector3D Oz (0,0,1) <VT3D> std::vector<float> vrad { 1 , 2 }; //List radius <Float> //now, creat Circles3D instance Class from 3 vector above then push to this vector vcir

    //it looks like: (mapcar '(lambda(a,b,c) Circle{a,b,c}) listPoint listOz listRadius)

    //This is what I got when translating it into C++ std::vector<CIR> vcir; //return list circle3D //Transform + Lambda -> creat instance Class circle then pushbach to vector vcir: std::transform(vcen.begin(), vcen.end(),

    vOz.begin(), vrad.begin(), [](PT3D p1, VT3D v, float r)->{vcir.push_back(new CIR{p1,v,r})});

    However this does not work, the errors I received:

    image1:----------------------------------------------

    image2:---------------------------------------------------------------------


    my question is:

    1. is my code feasible or not? If possible, how should I revise the code? because I want to create it quickly without using a for loop, can I do that? 

    2. Is this method complicated when working with multiple arrays?
    As you can see with mapcar + lambda I can add more lists later

    (mapcar '(lambda(a,b,c) ...) list1 list2 list3 list4 ...)

    3. Is there a more concise and better method? 

    I have read some of your comments but I still cannot do without using for-loop.

    Hope you guide me if the codes I use are not appropriate in this case.

    Thanks very much!!


    • Edited by Khanh01 Friday, January 17, 2020 10:15 AM
    Friday, January 17, 2020 1:31 AM

Answers

  • std::transform can apply an unary predicate to one sequence and write the result into another; or apply a binary predicate to two sequences, and write the result into a third. It doesn't know how to apply a predicate taking more than two parameters.

    Just write a normal loop.


    Igor Tandetnik

    • Marked as answer by Khanh01 Wednesday, January 22, 2020 8:01 AM
    Friday, January 17, 2020 5:10 AM

All replies

  • Hi,

    Thank you for posting here.

    Could you please tell me what is "PT3D","VT3D"? I'm appreciate that if you could provide us with (some of) your code to reproduce your issue, We will provide you with better help.

    For more details about transform and Lambda Expressions, I suggest you could refer to the links :
    https://docs.microsoft.com/en-us/cpp/standard-library/algorithm-functions?view=vs-2019#transform
    https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019

    >>How to using Std:transform + Lambda function?

    Here is a example,I suggest you could refer to: https://stackoverflow.com/questions/3885317/stdtransform-using-c0x-lambda-expression

    Best Regards,

    Jeanine Zhang

    Friday, January 17, 2020 5:07 AM
  • std::transform can apply an unary predicate to one sequence and write the result into another; or apply a binary predicate to two sequences, and write the result into a third. It doesn't know how to apply a predicate taking more than two parameters.

    Just write a normal loop.


    Igor Tandetnik

    • Marked as answer by Khanh01 Wednesday, January 22, 2020 8:01 AM
    Friday, January 17, 2020 5:10 AM
  • Check something like this too:

       float r = . . .;

       transform( vcen.cbegin( ), vcen.cend( ), vztor.cbegin( ), back_inserter(vcir),

          [r]( const PT3D& p1, const VT3D& v ) { return CIR(p1, v, r); } );

     


    • Edited by Viorel_MVP Friday, January 17, 2020 5:54 AM
    Friday, January 17, 2020 5:52 AM
  • Hi Viorel , I want each circle to have a different radius, I edited the post so you can visualize it better.
    <coccocgrammar></coccocgrammar>
    • Edited by Khanh01 Friday, January 17, 2020 10:24 AM
    Friday, January 17, 2020 10:06 AM
  • Hi Igor Tandetnik, thanks for the reply, it's just that I want to be able to somehow combine methods into one
    • Edited by Khanh01 Friday, January 17, 2020 10:23 AM
    Friday, January 17, 2020 10:07 AM
  • Hi Jeanine Zhang, thanks for the links, I'll read more about it.
    • Edited by Khanh01 Friday, January 17, 2020 10:23 AM
    Friday, January 17, 2020 10:14 AM