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

60%
+1 −0
Challenges Cumulative Counts

Scala 3, 50 44 bytes ? => ?.indices zip?map(?take _+1 count _.==) Try it in Scastie! This is an annoying, stupid approach. The more elegant one using inits was much longer (x=>x.inits.to...

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

Answer
#4: Post edited by user avatar user‭ · 2021-07-27T15:08:38Z (over 3 years ago)
  • # Scala 3, <s>50</s> 44 bytes
  • ```scala
  • ? => ?.indices zip?map(?take _+1 count _.==)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/ewPr1E4GRlKO7nkufl0qzA)
  • This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
  • ```scala
  • ? => //The input
  • ?.indices //The indices
  • zip ? //Zip them with the elements of the input
  • .map( //For every index i and the corresponding element x,
  • ? take _+1 //Take the first i+1 elements of the input
  • count //Count how many of them
  • _.==) //Are equal to the element x
  • ```
  • # Scala 3, <s>50</s> 44 bytes
  • ```scala
  • ? => ?.indices zip?map(?take _+1 count _.==)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/ewPr1E4GRlKO7nkufl0qzA)
  • This is an annoying, stupid approach. The more elegant one using `inits` was much longer (`x=>x.inits.toSeq.reverse.tail zip x map(_ count _.==)`).
  • ```scala
  • ? => //The input
  • ?.indices //The indices
  • zip ? //Zip them with the elements of the input
  • .map( //For every index i and the corresponding element x,
  • ? take _+1 //Take the first i+1 elements of the input
  • count //Count how many of them
  • _.==) //Are equal to the element x
  • ```
#3: Post edited by user avatar user‭ · 2021-07-25T15:20:23Z (over 3 years ago)
  • # Scala 3, 50 bytes
  • ```scala
  • x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/y4DHryrDRhGY3km2q9Kx6w)
  • This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
  • ```scala
  • x => //The input
  • x.zipWithIndex //Zip each element with its index
  • .map((?,i) => //For every element `?` and its index `i`,
  • x take i+1 //Take the first i+1 elements
  • count //Count how many of them
  • ?.==) //Are equal to `?` (the element)
  • ```
  • # Scala 3, <s>50</s> 44 bytes
  • ```scala
  • ? => ?.indices zip?map(?take _+1 count _.==)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/ewPr1E4GRlKO7nkufl0qzA)
  • This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
  • ```scala
  • ? => //The input
  • ?.indices //The indices
  • zip ? //Zip them with the elements of the input
  • .map( //For every index i and the corresponding element x,
  • ? take _+1 //Take the first i+1 elements of the input
  • count //Count how many of them
  • _.==) //Are equal to the element x
  • ```
#2: Post edited by user avatar user‭ · 2021-04-17T22:47:23Z (over 3 years ago)
  • # Scala 3, 50 bytes
  • ```
  • x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/y4DHryrDRhGY3km2q9Kx6w)
  • This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
  • # Scala 3, 50 bytes
  • ```scala
  • x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/y4DHryrDRhGY3km2q9Kx6w)
  • This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.
  • ```scala
  • x => //The input
  • x.zipWithIndex //Zip each element with its index
  • .map((?,i) => //For every element `?` and its index `i`,
  • x take i+1 //Take the first i+1 elements
  • count //Count how many of them
  • ?.==) //Are equal to `?` (the element)
  • ```
#1: Initial revision by user avatar user‭ · 2021-04-17T22:44:24Z (over 3 years ago)
# Scala 3, 50 bytes
```
x=>x.zipWithIndex.map((?,i)=>x take i+1 count?.==)
```
[Try it in Scastie!](https://scastie.scala-lang.org/y4DHryrDRhGY3km2q9Kx6w)

This is an annoying, stupid approach. The more elegant one using `inits` didn't work out.