shapeless

Map and reduce/fold over HList of scalaz.Validation

独自空忆成欢 提交于 2019-12-03 07:30:40
I started out with something like this: def nonEmpty[A] = (msg: String) => (a: Option[A]) => a.toSuccess(msg) val postal: Option[String] = request.param("postal") val country: Option[String] = request.param("country") val params = (postal |> nonEmpty[String]("no postal" )).toValidationNel |@| (country |> nonEmpty[String]("no country")).toValidationNel params { (postal, country) => ... } Now I thought it would be nice to reduce the boilerplate for better readability and for not having to explain to more junior team members what .toValidateNel and |@| mean. The first thought was List but then

Find type class instances for Shapeless HList

喜夏-厌秋 提交于 2019-12-03 07:03:30
Say that I have a trait Show[T] such as the one in Scalaz: https://github.com/scalaz/scalaz/blob/scalaz-seven/core/src/main/scala/scalaz/Show.scala#L9 I also have a Shapeless HList that may look like "1" :: 2 :: 3L :: HNil . Is there a way to find the Show instance for each element and apply shows such that I end up with "1" :: "2" :: "3L" :: HNil ? If any element were of a type that did not have an implicit Show instance in scope I would want a compile error. I think that if I build up an HList of the Show instances I should be able to use zipApply to get the HList I want, but I don't know if

How to define a function whose output type depends on the input type

不羁岁月 提交于 2019-12-03 05:51:21
问题 Given the following classes: case class AddRequest(x: Int, y: Int) case class AddResponse(sum: Int) case class ToUppercaseRequest(str: String) case class ToUppercaseResponse(upper: String) How do I define in a typesafe manner some function: def process(req: ???): ??? Such that the following should hold true: val r1: AddResponse = process(AddRequest(2, 3)) val r2: ToUppercaseResponse = process(ToUppercaseRequest("aaa")) Also, the following should not compile: val r3 = process("somestring") 回答1

How can I use Shapeless to create a function abstracting over arity

我的未来我决定 提交于 2019-12-03 03:22:19
Let's consider a specific example. I have lots of functions that take a variable number of arguments, and return a Seq[T] . Say: def nonNeg(start: Int, count: Int): Seq[Int] = Iterator.from(start).take(count).toSeq For each one of those function, I need to create a "Java version" of that function, returns a java.util.List[T] . I can create the "Java version" of the above function with: def javaNonNeg(start: Int, count: Int): java.util.List[Int] = nonNeg(start, count).asJava This is somewhat verbose, as the list of parameters is duplicated twice. Instead, I'd like to create a higher level

What is the purpose of the emptyCoproduct and coproduct methods of the TypeClass trait in Shapeless

折月煮酒 提交于 2019-12-03 01:15:40
It is not fully clear to me what is the purpose of the emptyCoProduct and coproduct methods of the TypeClass trait in Shapeless. When would one use the TypeClass trait instead of the ProductTypeClass ? What are some examples of ways those two methods would be implemented? Suppose I've got a simple type class: trait Weight[A] { def apply(a: A): Int } object Weight { def apply[A](f: A => Int) = new Weight[A] { def apply(a: A) = f(a) } } And some instances: implicit val stringWeight: Weight[String] = Weight(_.size) implicit def intWeight: Weight[Int] = Weight(identity) And a case class: case

Scala parameters pattern (Spray routing example)

给你一囗甜甜゛ 提交于 2019-12-03 00:29:23
Sorry about the vague title...wasn't sure how to characterize this. I've seen/used a certain code construction in Scala for some time but I don't know how it works. It looks like this (example from Spray routing): path( "foo" / Segment / Segment ) { (a,b) => { // <-- What's this style with a,b? ... }} In this example, the Segements in the path are bound to a and b respectively inside the associated block. I know how to use this pattern but how does it work? Why didn't it bind something to "foo"? I'm not so interested in how spray works for my purpose here, but what facility of Scala is this,

Materialize the Value for a Type that has One Inhabitant

前提是你 提交于 2019-12-03 00:09:50
Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence: sealed trait Digit case object Zero extends Digit case object One extends Digit sealed trait Dense { type N <: Dense } sealed trait DNil extends Dense { type N = DNil } case object DNil extends DNil final case class ::[+H <: Digit, +T <: Dense](digit: H, tail: T) extends Dense { type N = digit.type :: tail.N } /* The `A`th Fibonacci number is `B` */ trait Fib[A <: Dense, B <: Dense] object Fib { implicit val f0 = new Fib[_0, _0] {} implicit val f1 = new Fib[_1, _1] {} implicit def f2[A <: Dense, P <: Dense, P2 <: Dense

Scala Function.tupled and Function.untupled equivalent for variable arity, or, calling variable arity function with tuple

99封情书 提交于 2019-12-02 23:18:10
I was trying to do some stuff last night around accepting and calling a generic function (i.e. the type is known at the call site, but potentially varies across call sites, so the definition should be generic across arities). For example, suppose I have a function f: (A, B, C, ...) => Z . (There are actually many such f s, which I do not know in advance, and so I cannot fix the types nor count of A, B, C, ..., Z .) I'm trying to achieve the following. How do I call f generically with an instance of (A, B, C, ...) ? If the signature of f were known in advance, then I could do something

Folding a list of different types using Shapeless in Scala

柔情痞子 提交于 2019-12-02 21:12:44
As I known, shapeless provides the HList ( Heterogenous list) type which can include multiple types. Is it possible to fold HList ? for example, // ref - Composable application architecture with reasonably priced monad // code - https://github.com/stew/reasonably-priced/blob/master/src/main/scala/reasonable/App.scala import scalaz.{Coproduct, Free, Id, NaturalTransformation} def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): ({type cp[α] = Coproduct[F,G,α]})#cp ~> H = new NaturalTransformation[({type cp[α] = Coproduct[F,G,α]})#cp,H] { def apply[A](fa: Coproduct[F,G,A]): H[A] = fa.run match { case

Enforce Bounded Nat?

时光怂恿深爱的人放手 提交于 2019-12-02 20:06:31
问题 How can I enforce a Nat that's <= N ? Example: def lessThan5(x: NatLT5) = ??? where lessThan5(Nat(4)) would compile, but lessThan5(Nat(6)) would not. 回答1: You can use the type class LTEq (or LT if you want strictly less than). import shapeless.nat._ import shapeless.ops.nat._ def lessThan5[N <: Nat](n: N)(implicit ev: LTEq[N, _5]) = ??? lessThan5(_4) // compiles lessThan5(_5) // compiles lessThan5(_6) // doesn't compile because LTEq[_6, _5] cannot be found 来源: https://stackoverflow.com