Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4111080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:01:12+00:00 2026-05-20T22:01:12+00:00

I have an Iterable[String] representing the lines in a file and I’d like to

  • 0

I have an Iterable[String] representing the lines in a file and I’d like to find the first line in that sequence that matches a regular expression and return a numerical value extracted by the regex. The file is big enough that it wouldn’t make sense to load the whole thing into memory and then call toString() or something, so I’ll need to go through it a line at a time.

Here’s what I have (it works):

val RateRegex : Regex = ".....".r

def getRate(source : Source) : Option[Double] = {
  import java.lang.Double._

  for(line <- source.getLines() ) {
    line match {
      case RateRegex(rawRate) => return Some(parseDouble(rawRate))
      case None => ()
    }
  }

  return None
}

This seems ugly to me. It feels very imperative and case None => () might as well be replaced with a comment that says “you’re doing it wrong.”

I think I want something like def findFirstWhereNonNone(p : Function[A,Option[B]]) => Option[B] where the collection’s elements are of type A.

Are there built-in methods that would let me do this in a more functional way? Should I just write that method?

P.S. While I’m at it, is there an alternative to using java.lang.Double.parseDouble? Scala’s Double class doesn’t expose it.

P.P.S I’ve seen a lot of posts on SO suggesting that the Source API shouldn’t be used in production, but they’re all from 2008 and 2009. Is that still the case? If so, what should I use for IO?

Update

I now have:

import util.matching.Regex.Groups

for{line <- source.getLines()
    Groups(rawRate) <- RateRegex.findFirstMatchIn(line)} {
  return Some(parseDouble(rawRate))
}

return None

which feels a lot better to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:01:13+00:00Added an answer on May 20, 2026 at 10:01 pm

    EDIT: This third alternative ia quite neat:

    source
    .getLines()
    .collectFirst{ case RateRegex(x) => x.toDouble}
    

    Not sure if it’s more functional, but you can use the behaviour of foreach/for-comprehensions on Options

    def getRate(source : Source) : Option[Double] = {
    
         for {line    <- source.getLines() 
              rawRate <- RateRegex.findFirstIn(line)}
           return  Some(rawRate toDouble)
    
      return None
    }
    

    This works too (quite similar to EasyAngel’s answer):

    source
    .getLines()
    .map{RateRegex.findFirstMatchIn(_)}
    .filter{_.isDefined}
    .map{_.get.group(0).toDouble}
    .head
    .toList
    .headOption
    

    The last three are a little ugly. The take(1) is to ensure we only evaluate up to the first match. The toList is to force the evaluation, and the headOption to extract the first value as Some() or None if there is none. Is there a more idiomatic way of doing this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So lets say I have an incredibly nested iterable of lists/dictionaries. I would like
Assuming I have final Iterable<String> unsorted = asList(FOO, BAR, PREFA, ZOO, PREFZ, PREFOO); What
I have instantiated an object like this: GraphMatrixDirected<String, Integer> g g is passed to
If I have a Matcher[A] how do create a Matcher[Iterable[A]] that is satisfied only
I would like to have a c.g.c.c.Multimap that is sorted based on keys only.
I have an iterable of entries on which I would like to gather some
I have a sequence of key-value pairs (String, Int), and I want to group
I have the following pattern matching case in a scala function: def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int]
Suppose I have a function which can either take an iterable/iterator or a non-iterable
I have this reduce function: protected void reduce(Text key, Iterable<SortedMapWritable> values, Context context) throws

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.