How can I apply a predicate to one array and use the result to obtain values from another in Kotlin
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
add a comment |
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
1
Looks like an application for aMap
i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.
– leonardkraemer
Nov 15 '18 at 12:18
add a comment |
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
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
kotlin
asked Nov 15 '18 at 12:13
gringogordogringogordo
52711132
52711132
1
Looks like an application for aMap
i.e. store key-value pairs instead of handling array-indexes. Otherwise your approach looks viable.
– leonardkraemer
Nov 15 '18 at 12:18
add a comment |
1
Looks like an application for aMap
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
add a comment |
2 Answers
2
active
oldest
votes
val result = (arr1 zip arr2)
.filter (c1, _) -> predicate(c1)
.map (_, c2) -> c2
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
add a comment |
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'))
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
val result = (arr1 zip arr2)
.filter (c1, _) -> predicate(c1)
.map (_, c2) -> c2
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
add a comment |
val result = (arr1 zip arr2)
.filter (c1, _) -> predicate(c1)
.map (_, c2) -> c2
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
add a comment |
val result = (arr1 zip arr2)
.filter (c1, _) -> predicate(c1)
.map (_, c2) -> c2
val result = (arr1 zip arr2)
.filter (c1, _) -> predicate(c1)
.map (_, c2) -> c2
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
add a comment |
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
add a comment |
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'))
add a comment |
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'))
add a comment |
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'))
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'))
answered Nov 15 '18 at 13:03
AbhiAbhi
3539
3539
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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