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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:21:53+00:00 2026-05-14T04:21:53+00:00

This question isn’t meant as flame-bait! As it might be apparent, I’ve been looking

  • 0

This question isn’t meant as flame-bait! As it might be apparent, I’ve been looking at Scalaz recently. I’m trying to understand why I need some of the functionality that the library provides. Here’s something:

import scalaz._
import Scalaz._
type NEL[A] = NonEmptyList[A]
val NEL = NonEmptyList

I put some println statements in my functions to see what was going on (aside: what would I have done if I was trying to avoid side effects like that?). My functions are:

val f: NEL[Int] => String    = (l: NEL[Int]) => {println("f: " + l); l.toString |+| "X" }
val g: NEL[String] => BigInt = (l: NEL[String]) => {println("g: " + l);  BigInt(l.map(_.length).sum) }

Then I combine them via a cokleisli and pass in a NEL[Int]

val k = cokleisli(f) =>= cokleisli(g)
println("RES: "  + k( NEL(1, 2, 3) ))

What does this print?

f: NonEmptyList(1, 2, 3)
f: NonEmptyList(2, 3)
f: NonEmptyList(3)
g: NonEmptyList(NonEmptyList(1, 2, 3)X, NonEmptyList(2, 3)X, NonEmptyList(3)X)
RES: 57

The RES value is the character count of the (String) elements in the final NEL. Two things occur to me:

  1. How could I have known that my NEL was going to be reduced in this manner from the method signatures involved? (I wasn’t expecting the result at all)
  2. What is the point of this? Can a reasonably simple and easy-to-follow use case be distilled for me?

This question is a thinly-veiled plea for some lovely person like retronym to explain how this powerful library actually works.

  • 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-14T04:21:53+00:00Added an answer on May 14, 2026 at 4:21 am

    To understand the result, you need to understand the Comonad[NonEmptyList] instance. Comonad[W] essentially provides three functions (the actual interface in Scalaz is a little different, but this helps with explanation):

    map:    (A => B) => W[A] => W[B]
    copure: W[A] => A
    cojoin: W[A] => W[W[A]]
    

    So, Comonad provides an interface for some container W that has a distinguished “head” element (copure) and a way of exposing the inner structure of the container so that we get one container per element (cojoin), each with a given element at the head.

    The way that this is implemented for NonEmptyList is that copure returns the head of the list, and cojoin returns a list of lists, with this list at the head and all tails of this list at the tail.

    Example (I’m shortening NonEmptyList to Nel):

    Nel(1,2,3).copure = 1
    Nel(1,2,3).cojoin = Nel(Nel(1,2,3),Nel(2,3),Nel(3))
    

    The =>= function is coKleisli composition. How would you compose two functions f: W[A] => B and g: W[B] => C, knowing nothing about them other than that W is a Comonad? The input type of f and the output type of g aren’t compatible. However, you can map(f) to get W[W[A]] => W[B] and then compose that with g. Now, given a W[A], you can cojoin it to get the W[W[A]] to feed into that function. So, the only reasonable composition is a function k that does the following:

    k(x) = g(x.cojoin.map(f))
    

    So for your nonempty list:

    g(Nel(1,2,3).cojoin.map(f))
    = g(Nel(Nel(1,2,3),Nel(2,3),Nel(3)).map(f))
    = g(Nel("Nel(1,2,3)X","Nel(2,3)X","Nel(3)X"))
    = BigInt(Nel("Nel(1,2,3)X","Nel(2,3)X","Nel(3)X").map(_.length).sum)
    = BigInt(Nel(11,9,7).sum)
    = 27
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question isn't about unit-testing. And it is for a desktop product. This is
Important: This question isn't actually really an ASP.NET question. Anyone who knows anything about
I know this question isn't directly programming related, but since I want to be
I apologise in advance if this question isn't very specific. Would it be possible
I'm relatively new to web application programming so I hope this question isn't too
This question is related to the question posted here: Why isn't my custom WCF
I haven't found an answer to this particular question; perhaps there isn't one. But
This isn't a question but I thought I'd start a thread where links to
I know this isn't strictly a programming question but y'all must have experienced this.
This isn't a holy war, this isn't a question of which is better. What

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.