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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:38:31+00:00 2026-06-11T00:38:31+00:00

The code below works but pattern matching on Option b doesn’t look elegant. Is

  • 0

The code below works but pattern matching on Option b doesn’t look elegant.
Is there a way to avoid it while keeping the same semantics?

object A {
  def apply(b: B): ValidationNEL[String, A] = ...
}

case class C(i: Int, a: Option[A])

object C {
  def apply(i: Int, b: Option[B]): ValidationNEL[String, C] = b match {
    case None => Success(C(i, None))
    case Some(sb) => A(sb).map(bb => C(i, Some(bb)))
  }     
}
  • 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-11T00:38:33+00:00Added an answer on June 11, 2026 at 12:38 am

    Using scalaz, you can fold over Option

    b fold ( sb => A(sb) map (bb => C(i, some(bb))), C(i, none).success)
    

    There’s probably a way of simplifying the sb => A(sb) map (bb => C(i, some(bb))) using pointfree style but this is often ugly in scala:

    Setup

    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    object A {
      def apply(b: B): ValidationNEL[String, A] = sys.error("")
    }
    case class A()
    case class B()
    case class C(i: Int, a: Option[A])
    
    // Exiting paste mode, now interpreting.
    
    defined module A
    defined class A
    defined class B
    defined class C
    

    First impl

    scala> def apply(i: Int, b: Option[B]): ValidationNEL[String, C] =
         | b fold ( sb => A(sb) map (bb => C(i, some(bb))), C(i, none).success)
    apply: (i: Int, b: Option[B])scalaz.Scalaz.ValidationNEL[String,C]
    

    Second Impl

    If you declare first-class functions, you stand a better chance of composition. For example:

    object A { val fromB: B => ValidationNEL[String, A] = _ => sys.error("") }
    object C { val fromA: Int => A => C = i => a => C(i, some(a)) }
    
    defined module A
    defined module C
    

    Then

    scala> def apply(i: Int, b: Option[B]): ValidationNEL[String, C] =
         | b fold (A.fromB andThen (_ map C.fromA(i)), C(i, none).success)
    apply: (i: Int, b: Option[B])scalaz.Scalaz.ValidationNEL[String,C]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below works well but grabs only the UIView which is VISIBLE on
the code below works fine but it takes an absolute age to run. How
Currently my code below works fine but it's a bit of overkill. In my
The code below works fine. But it seem clunky. How would you combine the
The code I have below works fine but I'm sure it could be more
The code below works in FF, Safari, Chrome. But IE is giving me issues.
The example code below works as as a server process. But when I add
I have to code below - works great in IE and Opera, but does
Please i need help, this code below works fine on my localhost, php5.3+ but
I have the following code below which works, but I want to insert values

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.