Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement UUIDGen capability trait. #1772

Merged
merged 4 commits into from
Jul 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions std/shared/src/main/scala/cats/effect/std/UUIDGen.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020-2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.std

import cats.Functor
import cats.effect.kernel.Sync
import cats.implicits._

import java.util.UUID

/**
* A purely functional UUID Generator
*/
trait UUIDGen[F[_]] {

/**
* Generates a UUID in a pseudorandom manner.
* @return randomly generated UUID
*/
def randomUUID: F[UUID]
}

object UUIDGen {
def apply[F[_]](implicit ev: UUIDGen[F]): UUIDGen[F] = ev

implicit def fromSync[F[_]: Sync]: UUIDGen[F] = new UUIDGen[F] {
override def randomUUID: F[UUID] = Sync[F].delay(UUID.randomUUID())
}
def randomUUID[F[_]: UUIDGen]: F[UUID] = UUIDGen[F].randomUUID
def randomString[F[_]: UUIDGen: Functor]: F[String] = randomUUID.map(_.toString)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add this to the companion?

  def randomString[F[_]: Functor: UUIDGen]: F[String] = randomUUID[F].map(_.toString)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds reasonable.