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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:57:04+00:00 2026-06-11T10:57:04+00:00

Given a function f: A => E \/ B , what is an idiomatic

  • 0

Given a function f: A => E \/ B, what is an idiomatic way to gather the B results for some list of As while logging the Es?

I came up with the following (partially while answering this SO question):

import scalaz._, Scalaz._
type Logger[+A] = Writer[List[String], A]

def logged[A, E, B, F[_]](f: A => E \/ B)(implicit FM: Monoid[F[E]], FP: Pointed[F]): (A => Writer[F[E], Option[B]]) = 
  (a: A) => f(a).fold(e => Writer(FP.point(e), None), b => Writer(FM.zero, Some(b)))

def listLogged[A, E, B](f: A => E \/ B) = logged[A, E, B, List](f)

type W[+A] = Writer[List[String], A]

def keepOdd(n: Int): String \/ Int = 
  if (n % 2 == 1) \/.right(n) else \/.left(n + " was even")

scala> val x = List(5, 6).traverse[W, Option[Int]](listLogged(keepOdd))
x: W[List[Option[Int]]] = scalaz.WriterTFunctions$$anon$26@503d0400

scala> x.run
res11: (List[String], List[Option[Int]]) = (List(6 was even),List(Some(5), None))

scala> val (logs, results) = x.map(_.flatten).run
logs: List[String] = List(6 was even)
results: List[Int] = List(5)

Is there a shorter / better / less restrictive / more general way?

  • 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-11T10:57:05+00:00Added an answer on June 11, 2026 at 10:57 am

    You can use putWith to write what to my eye is a more readable logged method:

    def logged[A, E, B, F[_]: PlusEmpty: Pointed](f: A => E \/ B) = (a: A) =>
      WriterT.putWith(f(a).point[Id])(_.swap.toOption.orEmpty[F]).map(_.toOption)
    

    I also think the PlusEmpty context bound on F looks a little cleaner than explicitly requiring F[E]] to be a monoid (it accomplishes the same thing, of course). It’s a shame that the .point[Id] bit is necessary—that there’s not a Writer.putWith—but beggars can’t be choosers, I guess.

    I’d also write keepOdd like this:

    def keepOdd(n: Int) = Either.cond(n % 2 == 1, n, n + " was even").disjunction
    

    Or at least use n.right instead of \/.right(n), but that’s just a matter of taste.

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

Sidebar

Related Questions

Consider the following wrapper function that retrys a given function some given number of
Given a JSON string like this: {Locations: {list: [ {description: some description, name: the
A way to draw the curve corresponding to a given function is this: fun1
Given this function: > function Get-ArrayOfArrays() { (1,2),(3,4) | % { $_ } }
I have the following code (generates a quadratic function given the a, b, and
Given the following Javascript, would it be better/more idiomatic to inject the MyService object
Reading this question got me thinking: For a given function f , how can
Given: function A(name) { this.name = name; } is: var a1 = new A(A1);
Given a Javascript DOM object, what is the most idiomatic way of calling a
Is there a way to find if given function reference has a closure and

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.