Code Monkey home page Code Monkey logo

Comments (5)

armanbilge avatar armanbilge commented on August 18, 2024 1

We have prependK and appendK on Alternative with those signatures. Sergey has optimized their implementations for standard collections in #4052 and #4055. Does that address your usecase? Thanks!

/**
* Lift `a` into `F[_]` and prepend it to `fa`.
*
* Example:
* {{{
* scala> NonEmptyAlternative[List].prependK(1, List(2, 3, 4))
* res0: List[Int] = List(1, 2, 3, 4)
* }}}
*/
def prependK[A](a: A, fa: F[A]): F[A] = combineK(pure(a), fa)
/**
* Lift `a` into `F[_]` and append it to `fa`.
*
* Example:
* {{{
* scala> NonEmptyAlternative[List].appendK(List(1, 2, 3), 4)
* res0: List[Int] = List(1, 2, 3, 4)
* }}}
*/
def appendK[A](fa: F[A], a: A): F[A] = combineK(fa, pure(a))

from cats.

benhutchison avatar benhutchison commented on August 18, 2024

Oh that's nice, didn't notice, thanks to @satorg 🙏

That seems to address my wish list above, and the intent of #1360

While on this topic, what about the co-operations to pull items out of containers, perhaps called "uncons" and "unsnoc", F[A] => (F[A], A)?

I see we have an implementation for Chain, but no abstraction for it afaict.

from cats.

armanbilge avatar armanbilge commented on August 18, 2024

While on this topic, what about the co-operations to pull items out of containers

You can use Comonad#extract to e.g. get the head of a NonEmptyList. But off the top of my head I'm not aware of any operations for getting the tail, or the (init, last).

from cats.

satorg avatar satorg commented on August 18, 2024

Also, perhaps as a work-around, there's Foldable#toIterable that can provide access to head and tail, but the latter will be just Iterable of course, not F[_] anymore.

from cats.

benhutchison avatar benhutchison commented on August 18, 2024
  • When I tried rewriting code written in terms of List to M: Alternative, I got stuck where the lists (used as stacks) get destructured to remove the head. Naturally Foldable#toIterable doesn't express the efficient destructure of List/Chain -like containers.

  • I do think the complimentary co- operations Uncons and Unsnoc are valuable in some contexts, but should I create a separate issue as its drifted from the title?

  • What would be the correct type-class for destructuring duals to prependK & appendK?

    Comonad seems too specific. Would a CoSemigroupK define some splitK dual to combineK? And CoNonEmptyAlternative define a unprependK and unappendK (perhaps with aliases uncons and unsnoc)?

from cats.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.