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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:34:09+00:00 2026-05-26T20:34:09+00:00

I have a List[Option[Int]] and want to sum over it using applicative functors. From

  • 0

I have a List[Option[Int]] and want to sum over it using applicative functors.
From [1] I understand that it should be something like the following

import scalaz._
import Scalaz._

List(1,2,3).map(some(_)).foldLeft(some(0))({
    case (acc,value) => (acc <|*|> value){_+_}
})

however I am simply not able to figure out the correct way to write this.
I would be glad if somebody could help me with this.

Thank you very much

[1] How to combine Option values in Scala?

Edit

Thanks for all the great answers.

If there is any None in the list, I want it to return None.
I am trying to replace Null/Exception with Option/Either and see if I can produce some usable code.

Some function will fill my list and I want to process it further as easy as possible without checking if one of the elements was None. It should work similar as Exceptions, where I don’t have to check for it in my function, but let the caller take care of it.

  • 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-26T20:34:09+00:00Added an answer on May 26, 2026 at 8:34 pm

    If you have Option[T] and if there’s a Monoid for T, then there’s a Monoid[Option[T]]:

    implicit def optionTIsMonoid[T : Monoid]: Monoid[Option[T]] = new Monoid[Option[T]] {
      val monoid = implicitly[Monoid[T]]
      val zero = None
      def append(o1: Option[T], o2: =>Option[T]) = (o1, o2) match {
        case (Some(a), Some(b)) => Some(monoid.append(a, b))
        case (Some(a), _)       => o1
        case (_, Some(b))       => o2
        case _                  => zero
      }
    }
    

    Once you are equipped with this, you can just use sum (better than foldMap(identity), as suggested by @missingfaktor):

     List(Some(1), None, Some(2), Some(3), None).asMA.sum === Some(6)
    

    UPDATE

    We can actually use applicatives to simplify the code above:

    implicit def optionTIsMonoid[T : Monoid]: Monoid[Option[T]] = new Monoid[Option[T]] {
       val monoid = implicitly[Monoid[T]]
       val zero = None
       def append(o1: Option[T], o2: =>Option[T]) = (o1 |@| o2)(monoid.append(_, _))
    }
    

    which makes me think that we can maybe even generalize further to:

    implicit def applicativeOfMonoidIsMonoid[F[_] : Applicative, T : Monoid]: Monoid[F[T]] = 
      new Monoid[F[T]] {
        val applic = implicitly[Applicative[F]]
        val monoid = implicitly[Monoid[T]]
    
        val zero = applic.point(monoid.zero)
        def append(o1: F[T], o2: =>F[T]) = (o1 |@| o2)(monoid.append(_, _))
      }
    

    Like that you would even be able to sum Lists of Lists, Lists of Trees,…

    UPDATE2

    The question clarification makes me realize that the UPDATE above is incorrect!

    First of all optionTIsMonoid, as refactored, is not equivalent to the first definition, since the first definition will skip None values while the second one will return None as soon as there’s a None in the input list. But in that case, this is not a Monoid! Indeed, a Monoid[T] must respect the Monoid laws, and zero must be an identity element.

    We should have:

    zero    |+| Some(a) = Some(a)
    Some(a) |+| zero    = Some(a)
    

    But when I proposed the definition for the Monoid[Option[T]] using the Applicative for Option, this was not the case:

    None    |+| Some(a) = None
    None    |+| None    = None
    => zero |+| a      != a
    
    Some(a) |+| None    = zero
    None    |+| None    = zero
    => a    |+| zero   != a
    

    The fix is not hard, we need to change the definition of zero:

    // the definition is renamed for clarity
    implicit def optionTIsFailFastMonoid[T : Monoid]: Monoid[Option[T]] = 
      new Monoid[Option[T]] {
        monoid = implicitly[Monoid[T]]
        val zero = Some(monoid.zero)
        append(o1: Option[T], o2: =>Option[T]) = (o1 |@| o2)(monoid.append(_, _))
      }
    

    In this case we will have (with T as Int):

    Some(0) |+| Some(i) = Some(i)
    Some(0) |+| None    = None
    => zero |+| a       = a
    
    Some(i) |+| Some(0) = Some(i)
    None    |+| Some(0) = None
    => a    |+| zero    = zero
    

    Which proves that the identity law is verified (we should also verify that the associative law is respected,…).

    Now we have 2 Monoid[Option[T]] which we can use at will, depending on the behavior we want when summing the list: skipping Nones or “failing fast”.

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

Sidebar

Related Questions

i have list of images and on mouse over there is option box shows
I have a situation where the second select list option is generated from the
I have a routine that dynamically changes a select list's selected option when the
I have a program that takes a list of names from a file and
I have a class that I fill from the database: public class Option<T> {
I have string on the format $0Option one$1$Option two$2$Option three (etc) that I want
I have a list in a select. After selecting an option i do a
I have a list like this: <select name=select_list_name id=list_id> <option value=>Select Option</option> <option value=value1>Option
So I have a dropdown list <select id=theSelectId> <option value=volvo>Volvo</option> <option value=saab>Saab</option> <option value=mercedes>Mercedes</option>
Suppose I have a drop-down list like: <select id='list'> <option value='1'>Option A</option> <option value='2'>Option

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.