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

  • Home
  • SEARCH
  • 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 806343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:13:43+00:00 2026-05-15T00:13:43+00:00

In language like python and ruby to ask the language what index-related methods its

  • 0

In language like python and ruby to ask the language what index-related methods its string class supports (which methods’ names contain the word “index”) you can do

“”.methods.sort.grep /index/i

And in java

List results = new ArrayList();  
Method[] methods = String.class.getMethods();  
for (int i = 0; i < methods.length; i++) {  
    Method m = methods[i];  
    if (m.getName().toLowerCase().indexOf(“index”) != -1) {  
        results.add(m.getName());  
    }  
}  
String[] names = (String[]) results.toArray();  
Arrays.sort(names);  
return names;  

How would you do the same thing in Scala?

  • 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-15T00:13:44+00:00Added an answer on May 15, 2026 at 12:13 am

    Curious that no one tried a more direct translation:

    ""
    .getClass.getMethods.map(_.getName) // methods
    .sorted                             // sort
    .filter(_ matches "(?i).*index.*")  // grep /index/i
    

    So, some random thoughts.

    • The difference between “methods” and the hoops above is striking, but no one ever said reflection was Java’s strength.

    • I’m hiding something about sorted above: it actually takes an implicit parameter of type Ordering. If I wanted to sort the methods themselves instead of their names, I’d have to provide it.

    • A grep is actually a combination of filter and matches. It’s made a bit more complex because of Java’s decision to match whole strings even when ^ and $ are not specified. I think it would some sense to have a grep method on Regex, which took Traversable as parameters, but…

    So, here’s what we could do about it:

    implicit def toMethods(obj: AnyRef) = new { 
      def methods = obj.getClass.getMethods.map(_.getName)
    }
    
    implicit def toGrep[T <% Traversable[String]](coll: T) = new {
      def grep(pattern: String) = coll filter (pattern.r.findFirstIn(_) != None)
      def grep(pattern: String, flags: String) = {
        val regex = ("(?"+flags+")"+pattern).r
        coll filter (regex.findFirstIn(_) != None)
      }
    }
    

    And now this is possible:

    "".methods.sorted grep ("index", "i")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems like every web framework has its own pet template language. Ruby has
I am experimenting with several languages (Python, Ruby...), and I would like to know
I'm learning Python because I think is an awesome and powerful language like C++,
One of the nice features of languages like Python, Ruby or LISP is the
So I'm aware of the big ammount of general-purpose scripting languages like Ruby, Python,
I'd like to build a multiplayer web game application in which it supports chat.
I have few experience with languages like Python, Perl and Ruby, but I have
When I speak of scripting languages I am speaking of languages like Python, Perl,
in languages like PHP or Python there are convenient functions to turn an input
Basically, I want to do something like this (in Python, or similar imperative languages):

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.