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 8762897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:38:52+00:00 2026-06-13T15:38:52+00:00

I want to sort and/or filter a sequence. Basically something like this: var result

  • 0

I want to sort and/or filter a sequence. Basically something like this:

var result = getReallyLongSeq() // returns Seq[SomeClass]

if (doFilter) {
  result = result.filter( ... )
}

if (doSort) {
  result = result.sortWith( ... )
}

Now, this is an obviously valid approach, but is there a more functional way of doing that?

  • 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-06-13T15:38:54+00:00Added an answer on June 13, 2026 at 3:38 pm

    Without library support, you can roll out your own Boolean Reader Monad.

    This is functional, pure and configurable.

    Boolean Reader

    case class BoolConf[A](run: Boolean => A) {
        def apply(b: Boolean) = run(b)
        def map[B](f: A => B): BoolConf[B] = BoolConf(b => f(run(b)))
        def flatMap[B](f: A => BoolConf[B]): BoolConf[B] = BoolConf(b => f(run(b))(b))
    }
    

    Here we made a wrapper for a Boolean => A that allows monadic composition, and it’s probably already implemented in some library, like scalaz.

    We’re only interested in the run method, for this case, but you can get interested in other opportunities.

    Configured filter & sort

    Then we wrap our filter and sort check with the Reader

    val mFilter: Seq[SomeClass] => BoolConf[Seq[SomeClass]] = seq => BoolConf(if(_) seq.filter(...) else seq)
    val mSort: Seq[SomeClass] => BoolConf[Seq[SomeClass]] = seq => BoolConf(if(_) seq.sortWith(...) else seq)
    

    Lifting

    Now, to compose these functions, since the output is no more a simple Seq, we need to lift one of them to work within a BoolConf

    def lift2Bool[A, B]: (A => B) => (BoolConf[A] => BoolConf[B]) =
        fun => cfg => BoolConf(bool => fun(cfg(bool)))
    

    Now we’re able to convert any function from A => B to a lifted function from BoolConf[A] => BoolConf[B]

    Composing

    Now we can compose functionally:

    val filterAndSort = lift2Bool(mSort) compose mFilter
    //or the equivalent
    val filterAndSort = mFilter andThen lift2Bool(mSort)
    //applies as in filterAndSort(<sequence>)(<do filter>)(<do sort>)
    

    There’s more

    We can also create a generic “builder” for our mFilter and mSort

    val configFilter[SomeClass]: (SomeClass => Boolean) => Seq[MyClass] => BoolConf[Seq[SomeClass]] = 
        filterer => seq => BoolConf(if(_) seq.filter(filterer))
    

    You can “sort” the sorting equivalent by yourself

    Thanks are due to Runar for the inspiration

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

Sidebar

Related Questions

In rails I could do something like this to make sure a user is
I have this SQL that works fine. Want the my filter to return the
I want filter and sort option in html table. I tried 'tablesorter.js', I can
I would like to fill a ComboBox but I want to sort data by
i am leveraging this project to use jqgrid to filter and sort collections .
I have the following query which I want to sort or filter using projected
I'm using CodeIgniter and I want to create some sort of filter to know
I have a query that returns a resultset. And I want to apply filter
I want to sort a mutable array by date. My array contains several dict
I want to sort the items that have been previously selected with checkboxes. If

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.