Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges Truthify an array

Scala 3, 68 bytes _.zipWithIndex.flatMap(_.zipWithIndex.filter(_._1)map(_._2)map _.->) Try it in Scastie! Takes a List[List[Boolean]], returns a List[(Int, Int)]. This abuses underscores a ...

posted 3y ago by user‭  ·  edited 3y ago by user‭

Answer
#2: Post edited by user avatar user‭ · 2021-04-19T18:42:47Z (about 3 years ago)
  • # Scala 3, 68 bytes
  • ```scala
  • _.zipWithIndex.flatMap(_.zipWithIndex.filter(_._1)map(_._2)map _.->)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/RDjAuL1cTmqhAmtlwpIJRQ)
  • Takes a `List[List[Boolean]]`, returns a `List[(Int, Int)]`.
  • Explanation:
  • ```scala
  • _.zipWithIndex //Zip each row with its index
  • .flatMap( //Map every (row, index) tuple to a list of truthy indices,
  • //then flatten
  • _.zipWithIndex //Zip each element with its column
  • .filter(_._1) //Keep the tuples where the element (_1) is true
  • map(_._2) //Keep the second part of the tuple (the column)
  • map _.->) //Make a 2-tuple with the row's index
  • ```
  • # Scala 3, 68 bytes
  • ```scala
  • _.zipWithIndex.flatMap(_.zipWithIndex.filter(_._1)map(_._2)map _.->)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/RDjAuL1cTmqhAmtlwpIJRQ)
  • Takes a `List[List[Boolean]]`, returns a `List[(Int, Int)]`. This abuses underscores a bit (which is why Scala 3 is needed), but I'm proud of it.
  • Explanation:
  • ```scala
  • _.zipWithIndex //Zip each row with its index
  • .flatMap( //Map every (row, index) tuple to a list of truthy indices,
  • //then flatten
  • _.zipWithIndex //Zip each element with its column
  • .filter(_._1) //Keep the tuples where the element (_1) is true
  • map(_._2) //Keep the second part of the tuple (the column)
  • map _.->) //Make a 2-tuple with the row's index
  • ```
#1: Initial revision by user avatar user‭ · 2021-04-19T18:42:16Z (about 3 years ago)
# Scala 3, 68 bytes
```scala
_.zipWithIndex.flatMap(_.zipWithIndex.filter(_._1)map(_._2)map _.->)
```
[Try it in Scastie!](https://scastie.scala-lang.org/RDjAuL1cTmqhAmtlwpIJRQ)

Takes a `List[List[Boolean]]`, returns a `List[(Int, Int)]`.

Explanation:
```scala
_.zipWithIndex //Zip each row with its index
  .flatMap(    //Map every (row, index) tuple to a list of truthy indices,
               //then flatten
  _.zipWithIndex //Zip each element with its column
    .filter(_._1) //Keep the tuples where the element (_1) is true
    map(_._2)     //Keep the second part of the tuple (the column)
    map _.->)     //Make a 2-tuple with the row's index
```