How can I apply a predicate to one array and use the result to obtain values from another in Kotlin










0















I have 2 arrays (they are actually 2 dimensional but I don't think that is important for the question)



val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')


I'm trying to implement the following method



fun filter(predicate: (T?) -> Boolean): Collection<Char> ... 


The arrays above are simplified as contents of arr1 in the real code is a nullable generic (T?) but I'm guessing we can carry on like this for the question.



So what I am trying to do is apply the predicate to the first array and get the values from the corresponding indices from the 2nd.



So lets say I try



val res = obj.filter it == 'b' 


I would want to get a collection with 'y' and 'u' in it.



I've been going around the houses on this so I think I've missed the proper way. My last attempt was along the lines of (used flatten as it's a 2 dimensional array)



val newList = arr1.flatten().mapIndexedidx, it -> predicate


I could then use this to get the values from arr2 (assuming they always flatten consistently ?)



My question I guess is either



a) how do I get a list of the indexes using mapIndexed with the predicate
or
b) what is the better way to do it (I'm assuming I've taken the wrong approach tbh)










share|improve this question

















  • 1





    Looks like an application for a Map i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.

    – leonardkraemer
    Nov 15 '18 at 12:18
















0















I have 2 arrays (they are actually 2 dimensional but I don't think that is important for the question)



val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')


I'm trying to implement the following method



fun filter(predicate: (T?) -> Boolean): Collection<Char> ... 


The arrays above are simplified as contents of arr1 in the real code is a nullable generic (T?) but I'm guessing we can carry on like this for the question.



So what I am trying to do is apply the predicate to the first array and get the values from the corresponding indices from the 2nd.



So lets say I try



val res = obj.filter it == 'b' 


I would want to get a collection with 'y' and 'u' in it.



I've been going around the houses on this so I think I've missed the proper way. My last attempt was along the lines of (used flatten as it's a 2 dimensional array)



val newList = arr1.flatten().mapIndexedidx, it -> predicate


I could then use this to get the values from arr2 (assuming they always flatten consistently ?)



My question I guess is either



a) how do I get a list of the indexes using mapIndexed with the predicate
or
b) what is the better way to do it (I'm assuming I've taken the wrong approach tbh)










share|improve this question

















  • 1





    Looks like an application for a Map i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.

    – leonardkraemer
    Nov 15 '18 at 12:18














0












0








0








I have 2 arrays (they are actually 2 dimensional but I don't think that is important for the question)



val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')


I'm trying to implement the following method



fun filter(predicate: (T?) -> Boolean): Collection<Char> ... 


The arrays above are simplified as contents of arr1 in the real code is a nullable generic (T?) but I'm guessing we can carry on like this for the question.



So what I am trying to do is apply the predicate to the first array and get the values from the corresponding indices from the 2nd.



So lets say I try



val res = obj.filter it == 'b' 


I would want to get a collection with 'y' and 'u' in it.



I've been going around the houses on this so I think I've missed the proper way. My last attempt was along the lines of (used flatten as it's a 2 dimensional array)



val newList = arr1.flatten().mapIndexedidx, it -> predicate


I could then use this to get the values from arr2 (assuming they always flatten consistently ?)



My question I guess is either



a) how do I get a list of the indexes using mapIndexed with the predicate
or
b) what is the better way to do it (I'm assuming I've taken the wrong approach tbh)










share|improve this question














I have 2 arrays (they are actually 2 dimensional but I don't think that is important for the question)



val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')


I'm trying to implement the following method



fun filter(predicate: (T?) -> Boolean): Collection<Char> ... 


The arrays above are simplified as contents of arr1 in the real code is a nullable generic (T?) but I'm guessing we can carry on like this for the question.



So what I am trying to do is apply the predicate to the first array and get the values from the corresponding indices from the 2nd.



So lets say I try



val res = obj.filter it == 'b' 


I would want to get a collection with 'y' and 'u' in it.



I've been going around the houses on this so I think I've missed the proper way. My last attempt was along the lines of (used flatten as it's a 2 dimensional array)



val newList = arr1.flatten().mapIndexedidx, it -> predicate


I could then use this to get the values from arr2 (assuming they always flatten consistently ?)



My question I guess is either



a) how do I get a list of the indexes using mapIndexed with the predicate
or
b) what is the better way to do it (I'm assuming I've taken the wrong approach tbh)







kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:13









gringogordogringogordo

52711132




52711132







  • 1





    Looks like an application for a Map i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.

    – leonardkraemer
    Nov 15 '18 at 12:18













  • 1





    Looks like an application for a Map i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.

    – leonardkraemer
    Nov 15 '18 at 12:18








1




1





Looks like an application for a Map i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.

– leonardkraemer
Nov 15 '18 at 12:18






Looks like an application for a Map i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.

– leonardkraemer
Nov 15 '18 at 12:18













2 Answers
2






active

oldest

votes


















4














val result = (arr1 zip arr2)
.filter (c1, _) -> predicate(c1)
.map (_, c2) -> c2





share|improve this answer























  • That's fantastic thanks. I remember seeing the zip function a few days ago and thinking how useful it looked in the right circumstances. Obviously in one ear and out the other... Thanks.

    – gringogordo
    Nov 15 '18 at 13:46


















1














Zip the lists and use extension



fun <Any> List<Pair<Any,Any>>.myfilter(c: Any): List<Any>
val result: MutableList<Any> = mutableListOf()
for(item in this)
if(item.first == c)
result.add(item.second)

return result


fun main(args: Array<String>)
val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')

val zipped: List<Pair<Char,Char>> = arr1 zip arr2

print(zipped.myfilter('e'))






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319260%2fhow-can-i-apply-a-predicate-to-one-array-and-use-the-result-to-obtain-values-fro%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    val result = (arr1 zip arr2)
    .filter (c1, _) -> predicate(c1)
    .map (_, c2) -> c2





    share|improve this answer























    • That's fantastic thanks. I remember seeing the zip function a few days ago and thinking how useful it looked in the right circumstances. Obviously in one ear and out the other... Thanks.

      – gringogordo
      Nov 15 '18 at 13:46















    4














    val result = (arr1 zip arr2)
    .filter (c1, _) -> predicate(c1)
    .map (_, c2) -> c2





    share|improve this answer























    • That's fantastic thanks. I remember seeing the zip function a few days ago and thinking how useful it looked in the right circumstances. Obviously in one ear and out the other... Thanks.

      – gringogordo
      Nov 15 '18 at 13:46













    4












    4








    4







    val result = (arr1 zip arr2)
    .filter (c1, _) -> predicate(c1)
    .map (_, c2) -> c2





    share|improve this answer













    val result = (arr1 zip arr2)
    .filter (c1, _) -> predicate(c1)
    .map (_, c2) -> c2






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 12:36









    yoleyole

    60.9k11154145




    60.9k11154145












    • That's fantastic thanks. I remember seeing the zip function a few days ago and thinking how useful it looked in the right circumstances. Obviously in one ear and out the other... Thanks.

      – gringogordo
      Nov 15 '18 at 13:46

















    • That's fantastic thanks. I remember seeing the zip function a few days ago and thinking how useful it looked in the right circumstances. Obviously in one ear and out the other... Thanks.

      – gringogordo
      Nov 15 '18 at 13:46
















    That's fantastic thanks. I remember seeing the zip function a few days ago and thinking how useful it looked in the right circumstances. Obviously in one ear and out the other... Thanks.

    – gringogordo
    Nov 15 '18 at 13:46





    That's fantastic thanks. I remember seeing the zip function a few days ago and thinking how useful it looked in the right circumstances. Obviously in one ear and out the other... Thanks.

    – gringogordo
    Nov 15 '18 at 13:46













    1














    Zip the lists and use extension



    fun <Any> List<Pair<Any,Any>>.myfilter(c: Any): List<Any>
    val result: MutableList<Any> = mutableListOf()
    for(item in this)
    if(item.first == c)
    result.add(item.second)

    return result


    fun main(args: Array<String>)
    val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
    val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')

    val zipped: List<Pair<Char,Char>> = arr1 zip arr2

    print(zipped.myfilter('e'))






    share|improve this answer



























      1














      Zip the lists and use extension



      fun <Any> List<Pair<Any,Any>>.myfilter(c: Any): List<Any>
      val result: MutableList<Any> = mutableListOf()
      for(item in this)
      if(item.first == c)
      result.add(item.second)

      return result


      fun main(args: Array<String>)
      val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
      val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')

      val zipped: List<Pair<Char,Char>> = arr1 zip arr2

      print(zipped.myfilter('e'))






      share|improve this answer

























        1












        1








        1







        Zip the lists and use extension



        fun <Any> List<Pair<Any,Any>>.myfilter(c: Any): List<Any>
        val result: MutableList<Any> = mutableListOf()
        for(item in this)
        if(item.first == c)
        result.add(item.second)

        return result


        fun main(args: Array<String>)
        val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
        val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')

        val zipped: List<Pair<Char,Char>> = arr1 zip arr2

        print(zipped.myfilter('e'))






        share|improve this answer













        Zip the lists and use extension



        fun <Any> List<Pair<Any,Any>>.myfilter(c: Any): List<Any>
        val result: MutableList<Any> = mutableListOf()
        for(item in this)
        if(item.first == c)
        result.add(item.second)

        return result


        fun main(args: Array<String>)
        val arr1 = arrayOf<Char>('a', 'b', 'c', 'd', 'e', 'b')
        val arr2 = arrayOf<Char>('z', 'y', 'x', 'w', 'v', 'u')

        val zipped: List<Pair<Char,Char>> = arr1 zip arr2

        print(zipped.myfilter('e'))







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 13:03









        AbhiAbhi

        3539




        3539



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319260%2fhow-can-i-apply-a-predicate-to-one-array-and-use-the-result-to-obtain-values-fro%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto