Mapping tuples in shapeless HList - scala

You do
import shapeless._ ; import poly._
object fun extends (List ~>> (List, Int)) {
override def apply[T](list: List[T]): (List, Int) = list -> list.size
}
println((List(1,2,3) :: List("a", "b", "c") :: HNil).map(fun))
to map every sublist into a couple. However, what if HList elements are more complex, like tutples, for instance? The natural attempt
object fun extends ((String -> List) ~>> (List, Int)) {
override def apply[T](list: (String -> List[T])): (List, Int) = list -> list.size
is rejected by the compiler. What do you do? Where can you learn that?

import shapeless._
import poly._
object fun2 extends Poly1 {
implicit def caseTuple[T] =
at[(String, List[T])](x => (x._2.tail, x._1.toInt))
}
Then:
println((("56", List(1,2,3)) :: ("78", List(4,2,3)) :: HNil).map(fun))
Or if you want to do it with original ~> it's yet still possible, but looks ugly to my taste:
object fun3 extends (({type L[T] = (String, List[T])})#L ~> ({type L[T] = (List[T], Int)})#L) {
override def apply[T](f: (String, List[T])): (List[T], Int) = f match {
case (str, list) => (list.tail, str.toInt)
}
}
println((("56", List(1,2,3)) :: ("78", List(4,2,3)) :: HNil).map(fun3))
P.S. Also please note that your code example does not compile. You miss [T] in the first part, you need to use ~> and String -> List is a wrong type in scala in the last part. List is a type constructor, not type and you can't use it this way

Related

Scala's type system and the input to FunctionN

I am wondering if there is a way of patching over an apparent inconsistency in Scala's handling of Function1 and Function2..N.
For a Function1, say Int => String, the parameter list (Int) is not identical to Int (even if the two are isomorphic), yet the compiler will infer the input as bare Int (see the code below).
For Function2..N, say
val f: (String, Int) => String = ???
the compiler will not infer any type for the input parameter list. In particular, there is no ParameterList[String, Int] even though it is isomorphic to tuples of (String, Int) and any other wrapper you fancy putting around Strings and Ints.
My first question is, is there is a reason it is like this (or is this something that exists on a list of scala todos)? i.e. why can Function1 be deconstructed into input and output type but not Function2 and does anybody want to fix that?
Are there any work arounds. Specifically, in the code below, is there a way to make invoke2 work?
package net.jtownson.swakka
import org.scalatest.FlatSpec
import org.scalatest.Matchers._
import shapeless.ops.function._
import shapeless.{HList, HNil, _}
class TypeclassOfFunctionTypeSpec extends FlatSpec {
// Here, we know the return type of F is constrained to be O
// (because that's how the shapeless FnToProduct typeclass works)
def invoke1[F, I <: HList, O](f: F, i: I)
(implicit ftp: FnToProduct.Aux[F, I => O]): O = ftp(f)(i)
// So let's try to express that by extracting the input type of F as FI
def invoke2[FI, I <: HList, O](f: FI => O, i: I)
(implicit ftp: FnToProduct.Aux[FI => O, I => O]): O = ftp(f)(i)
"Invoke" should "work for a Function1" in {
// Here's our function (Int) => String
val f: (Int) => String = (i) => s"I got $i"
val l = 1 :: HNil
// this works
val r1: String = invoke1(f, l)
// So does this. (With evidence that the compiler sees the function parameter list (Int) as just Int
val r2: String = invoke2[Int, Int::HNil, String](f, l)
r1 shouldBe "I got 1"
r2 shouldBe "I got 1"
}
"Invoke" should "work for a Function2" in {
// Here's our function (String, Int) => String
val f: (String, Int) => String = (s, i) => s"I got $s and $i"
val l = "s" :: 1 :: HNil
// this works
val r1: String = invoke1(f, l)
// But this does not compile. There is no expansion for the type of FI
// (String, Int) != the function Parameter list (String, Int)
val r2: String = invoke2(f, l)
/*
Error:(...) type mismatch;
found : (String, Int) => String
required: ? => String
val r1: String = invoke1(f, l)
*/
r1 shouldBe "I got s and 1"
r2 shouldBe "I got s and 1"
}
}
Int => String is syntax sugar for Function1[Int, String], (String, Int) => String is syntax sugar for Function2[String, Int, String], ((String, Int)) => String is syntax sugar for Function1[(String, Int), String] aka Function1[Tuple2[String, Int], String].
You can help Shapeless to resolve FnToProduct instances if you define implicit conversion
implicit def tupledFnToProduct[FI1, FI2, O, Out0](implicit
ftp: FnToProduct.Aux[Function2[FI1, FI2, O], Out0]
): FnToProduct.Aux[Function1[(FI1, FI2), O], Out0] =
new FnToProduct[Function1[(FI1, FI2), O]] {
override type Out = Out0
override def apply(f: Function1[(FI1, FI2), O]) = ftp((x, y) => f(x, y))
}
Then you can call invoke2 with .tupled
val f: (String, Int) => String = (s, i) => s"I got $s and $i"
val l = "s" :: 1 :: HNil
val r2: String = invoke2(f.tupled, l)
r2 == "I got s and 1" //true

Scala: reflection and case classes

The following code succeeds, but is there a better way of doing the same thing? Perhaps something specific to case classes? In the following code, for each field of type String in my simple case class, the code goes through my list of instances of that case class and finds the length of the longest string of that field.
case class CrmContractorRow(
id: Long,
bankCharges: String,
overTime: String,
name$id: Long,
mgmtFee: String,
contractDetails$id: Long,
email: String,
copyOfVisa: String)
object Go {
def main(args: Array[String]) {
val a = CrmContractorRow(1,"1","1",4444,"1",1,"1","1")
val b = CrmContractorRow(22,"22","22",22,"55555",22,"nine long","22")
val c = CrmContractorRow(333,"333","333",333,"333",333,"333","333")
val rows = List(a,b,c)
c.getClass.getDeclaredFields.filter(p => p.getType == classOf[String]).foreach{f =>
f.setAccessible(true)
println(f.getName + ": " + rows.map(row => f.get(row).asInstanceOf[String]).maxBy(_.length))
}
}
}
Result:
bankCharges: 3
overTime: 3
mgmtFee: 5
email: 9
copyOfVisa: 3
If you want to do this kind of thing with Shapeless, I'd strongly suggest defining a custom type class that handles the complicated part and allows you to keep that stuff separate from the rest of your logic.
In this case it sounds like the tricky part of what you're specifically trying to do is getting the mapping from field names to string lengths for all of the String members of a case class. Here's a type class that does that:
import shapeless._, shapeless.labelled.FieldType
trait StringFieldLengths[A] { def apply(a: A): Map[String, Int] }
object StringFieldLengths extends LowPriorityStringFieldLengths {
implicit val hnilInstance: StringFieldLengths[HNil] =
new StringFieldLengths[HNil] {
def apply(a: HNil): Map[String, Int] = Map.empty
}
implicit def caseClassInstance[A, R <: HList](implicit
gen: LabelledGeneric.Aux[A, R],
sfl: StringFieldLengths[R]
): StringFieldLengths[A] = new StringFieldLengths[A] {
def apply(a: A): Map[String, Int] = sfl(gen.to(a))
}
implicit def hconsStringInstance[K <: Symbol, T <: HList](implicit
sfl: StringFieldLengths[T],
key: Witness.Aux[K]
): StringFieldLengths[FieldType[K, String] :: T] =
new StringFieldLengths[FieldType[K, String] :: T] {
def apply(a: FieldType[K, String] :: T): Map[String, Int] =
sfl(a.tail).updated(key.value.name, a.head.length)
}
}
sealed class LowPriorityStringFieldLengths {
implicit def hconsInstance[K, V, T <: HList](implicit
sfl: StringFieldLengths[T]
): StringFieldLengths[FieldType[K, V] :: T] =
new StringFieldLengths[FieldType[K, V] :: T] {
def apply(a: FieldType[K, V] :: T): Map[String, Int] = sfl(a.tail)
}
}
This looks complex, but once you start working with Shapeless a bit you learn to write this kind of thing in your sleep.
Now you can write the logic of your operation in a relatively straightforward way:
def maxStringLengths[A: StringFieldLengths](as: List[A]): Map[String, Int] =
as.map(implicitly[StringFieldLengths[A]].apply).foldLeft(
Map.empty[String, Int]
) {
case (x, y) => x.foldLeft(y) {
case (acc, (k, v)) =>
acc.updated(k, acc.get(k).fold(v)(accV => math.max(accV, v)))
}
}
And then (given rows as defined in the question):
scala> maxStringLengths(rows).foreach(println)
(bankCharges,3)
(overTime,3)
(mgmtFee,5)
(email,9)
(copyOfVisa,3)
This will work for absolutely any case class.
If this is a one-off thing, you might as well use runtime reflection, or you could use the Poly1 approach in Giovanni Caporaletti's answer—it's less generic and it mixes up the different parts of the solution in a way I don't prefer, but it should work just fine. If this is something you're doing a lot of, though, I'd suggest the approach I've given here.
If you want to use shapeless to get the string fields of a case class and avoid reflection you can do something like this:
import shapeless._
import labelled._
trait lowerPriorityfilterStrings extends Poly2 {
implicit def default[A] = at[Vector[(String, String)], A] { case (acc, _) => acc }
}
object filterStrings extends lowerPriorityfilterStrings {
implicit def caseString[K <: Symbol](implicit w: Witness.Aux[K]) = at[Vector[(String, String)], FieldType[K, String]] {
case (acc, x) => acc :+ (w.value.name -> x)
}
}
val gen = LabelledGeneric[CrmContractorRow]
val a = CrmContractorRow(1,"1","1",4444,"1",1,"1","1")
val b = CrmContractorRow(22,"22","22",22,"55555",22,"nine long","22")
val c = CrmContractorRow(333,"333","333",333,"333",333,"333","333")
val rows = List(a,b,c)
val result = rows
// get for each element a Vector of (fieldName -> stringField) pairs for the string fields
.map(r => gen.to(r).foldLeft(Vector[(String, String)]())(filterStrings))
// get the maximum for each "column"
.reduceLeft((best, row) => best.zip(row).map {
case (kv1#(_, v1), (_, v2)) if v1.length > v2.length => kv1
case (_, kv2) => kv2
})
result foreach { case (k, v) => println(s"$k: $v") }
You probably want to use Scala reflection:
import scala.reflect.runtime.universe._
val rm = runtimeMirror(getClass.getClassLoader)
val instanceMirrors = rows map rm.reflect
typeOf[CrmContractorRow].members collect {
  case m: MethodSymbol if m.isCaseAccessor && m.returnType =:= typeOf[String] =>
    val maxValue = instanceMirrors map (_.reflectField(m).get.asInstanceOf[String]) maxBy (_.length)
    println(s"${m.name}: $maxValue")
}
So that you can avoid issues with cases like:
case class CrmContractorRow(id: Long, bankCharges: String, overTime: String, name$id: Long, mgmtFee: String, contractDetails$id: Long, email: String, copyOfVisa: String) {
val unwantedVal = "jdjd"
}
Cheers
I have refactored your code to something more reuseable:
import scala.reflect.ClassTag
case class CrmContractorRow(
id: Long,
bankCharges: String,
overTime: String,
name$id: Long,
mgmtFee: String,
contractDetails$id: Long,
email: String,
copyOfVisa: String)
object Go{
def main(args: Array[String]) {
val a = CrmContractorRow(1,"1","1",4444,"1",1,"1","1")
val b = CrmContractorRow(22,"22","22",22,"55555",22,"nine long","22")
val c = CrmContractorRow(333,"333","333",333,"333",333,"333","333")
val rows = List(a,b,c)
val initEmptyColumns = List.fill(a.productArity)(List())
def aggregateColumns[Tin:ClassTag,Tagg](rows: Iterable[Product], aggregate: Iterable[Tin] => Tagg) = {
val columnsWithMatchingType = (0 until rows.head.productArity).filter {
index => rows.head.productElement(index) match {case t: Tin => true; case _ => false}
}
def columnIterable(col: Int) = rows.map(_.productElement(col)).asInstanceOf[Iterable[Tin]]
columnsWithMatchingType.map(index => (index,aggregate(columnIterable(index))))
}
def extractCaseClassFieldNames[T: scala.reflect.ClassTag] = {
scala.reflect.classTag[T].runtimeClass.getDeclaredFields.filter(!_.isSynthetic).map(_.getName)
}
val agg = aggregateColumns[String,String] (rows,_.maxBy(_.length))
val fieldNames = extractCaseClassFieldNames[CrmContractorRow]
agg.map{case (index,value) => fieldNames(index) + ": "+ value}.foreach(println)
}
}
Using shapeless would get rid of the .asInstanceOf, but the essence would be the same. The main problem with the given code was that it was not re-usable since the aggregation logic was mixed with the reflection logic to get the field names.

Converting a List to a Case Class

As an exercise, I am trying to see if I can take a List[Any] and "cast" it into a case class using shapeless.
A very basic example of what I am trying to achieve:
case class Foo(i: Int, j: String)
val foo: Option[Foo] = fromListToCaseClass[Foo]( List(1:Any, "hi":Any) )
Here is how I am shaping my solution (this can be quite off):
def fromListToCaseClass[CC <: Product](a: List[Any]): Option[CC] = a.toHList[???].map( x => Generic[CC].from(x) )
Here is my reasoning:
I know that you can go from a case class to an HList[T] (CC -> HList[T]); where T is the type of the HList. I also know that you can create an HList from a list (list -> Option[HList]) as long as you know the type of the HList. Finally I know that you can go from an HList to a case class (HList -> CC).
CC -> HList[T]
list -> Option[HList[T]] -> Option[CC]
I am wondering if this makes sense or if I am way off here. Can we make this work? Any other suggestions? Thanks!
This can be done very straightforwardly using shapeless's Generic and FromTraversable type classes,
import scala.collection.GenTraversable
import shapeless._, ops.traversable.FromTraversable
class FromListToCaseClass[T] {
def apply[R <: HList](l: GenTraversable[_])
(implicit gen: Generic.Aux[T, R], tl: FromTraversable[R]): Option[T] =
tl(l).map(gen.from)
}
def fromListToCaseClass[T] = new FromListToCaseClass[T]
(There's some accidental complexity here due to Scala's awkwardness when it comes to mixing explicit and inferred type parameters: we want to specify T explicitly, but have R inferred for us).
Sample REPL session ...
scala> case class Foo(i: Int, j: String)
defined class Foo
scala> fromListToCaseClass[Foo](List(23, "foo"))
res0: Option[Foo] = Some(Foo(23,foo))
scala> fromListToCaseClass[Foo](List(23, false))
res1: Option[Foo] = None
You can do it with shapeless the following way:
import shapeless._
trait Creator[A] { def apply(list:List[Any]): Option[A] }
object Creator {
def as[A](list: List[Any])(implicit c: Creator[A]): Option[A] = c(list)
def instance[A](parse: List[Any] => Option[A]): Creator[A] = new Creator[A] {
def apply(list:List[Any]): Option[A] = parse(list)
}
def arbitraryCreate[A] = instance(list => list.headOption.map(_.asInstanceOf[A]))
implicit val stringCreate = arbitraryCreate[String]
implicit val intCreate = arbitraryCreate[Int]
implicit val hnilCreate = instance(s => if (s.isEmpty) Some(HNil) else None)
implicit def hconsCreate[H: Creator, T <: HList: Creator]: Creator[H :: T] =
instance {
case Nil => None
case list => for {
h <- as[H](list)
t <- as[T](list.tail)
} yield h :: t
}
implicit def caseClassCreate[C, R <: HList](
implicit gen: Generic.Aux[C, R],
rc: Creator[R]): Creator[C] =
instance(s => rc(s).map(gen.from))
}
And
val foo:Option[Foo] = Creator.as[Foo](List(1, "hi"))

Map `State` via `Lens`

Is there some function with signature like
lensMapState[S, T, A](lens : Lens[S, T]): State[T, A] => State[S, A]
With semantics run modification of chosen part and get result
One implementation could be
def lensMapState[S, T, A](lens: Lens[S, T]): State[T, A] => State[S, A] =
stateT => State { s =>
val (result, x) = stateT.run(lens.get(s))
(lens.set(result)(s), x)
}
but if there more straightforward way using monocle or scalaz.Lens ?
I think what you are looking for is something like this:
import scalaz._
import Scalaz._
case class Person(name: String, age: Int)
case object Person {
val _age = Lens.lensu[Person, Int]((p, a) => p.copy(age = a), _.age
}
val state = for {
a <- Person._age %= { _ + 1 }
} yield a
state.run(Person("Holmes", 42))
which results in
res0: scalaz.Id.Id[(Person, Int)] = (Person(Holmes,43),43)
There are many lens/state related functions defined in https://github.com/scalaz/scalaz/blob/series/7.1.x/core/src/main/scala/scalaz/Lens.scala
Monocle follows a similiar principle. The related functions are defined in monocle.state as far as I know.

Shapeless HList polymorphic map with an argument

Given an HList of Label[A](String) I want to map it into an HList of LabelWithValue[A](Label[A], A), where the actual values come from a Map[String, Any]. In the example below I just defined the map of values in the method, just imagine the values come from a database.
The below works, but it is very veeery hacky because it uses a global var. Instead I'd like to pass the Map[String, Any] into GetLabelWithValue. I didn't find a way though, because the caller of getValues implicitly creates a Mapper, and at that point the map of values doesn't exist yet. I tried to create a Mapper myself, but my type level programming skills aren't yet good enough.
import shapeless._
import shapeless.poly._
import shapeless.ops.hlist._
object Main extends App {
case class Label[A](name: String)
case class LabelWithValue[A](label: Label[A], value: A)
// TODO: avoid the horrible global state - pass in the Map as a parameter
var horribleGlobalState: Map[String, Any] = _
object GetLabelWithValue extends (Label ~> LabelWithValue) {
def apply[A](label: Label[A]) =
LabelWithValue(label, horribleGlobalState.get(label.name).asInstanceOf[A])
}
val label1 = Label[Int]("a")
val label2 = Label[String]("b")
val labels = label1 :: label2 :: HNil
val labelsWithValues: LabelWithValue[Int] :: LabelWithValue[String] :: HNil = getValues(labels)
println(labelsWithValues)
def getValues[L <: HList, M <: HList](labels: L)(
implicit mapper: Mapper.Aux[GetLabelWithValue.type, L, M]) = {
horribleGlobalState = Map("a" -> 5, "b" -> "five")
labels map GetLabelWithValue
}
}
Here is an alternative implementation of GetLabelWithValue, which behaves the same way:
object GetLabelWithValue extends Poly1 {
implicit def caseLabel[A] = at[Label[A]] { label ⇒
LabelWithValue(label, horribleGlobalState.get(label.name).asInstanceOf[A])
}
}
I am by no means shapeless guru but here's first thing that comes to my mind:
object Main extends App {
case class Label[A](name: String)
case class LabelWithValue[A](label: Label[A], value: A)
object combine extends Poly2 {
implicit def workS[A <: HList, B] = at[Label[B], (Map[String, Any], A)] {
case (i, (map, res)) ⇒
(map, LabelWithValue(i, map.get(i.name).asInstanceOf[B]) :: res)
}
}
var state: Map[String, Any] = Map("a" -> 5, "b" -> "five")
val label1 = Label[Int]("a")
val label2 = Label[String]("b")
val labels = label1 :: label2 :: HNil
val mapped = labels.foldRight((state, HNil))(combine)._2
println(mapped)
}
I'm not saying there's not better way, but this seems pretty reasonable - instead of global state you capture it using fold and decide based on it. Probably gives you a bit more power than you need though (as you could mutate the map inbetween folds, but...)
Here's the full solution (based on KadekM's solution) when you want to use it in a method. The hard bit was to extract the type out of the tuple (which is the result of the fold).
import shapeless._
import shapeless.ops.hlist._
import shapeless.ops.tuple.IsComposite
object Main extends App {
case class Label[A](name: String)
case class LabelWithValue[A](label: Label[A], value: A)
object combineLabelWithValue extends Poly2 {
implicit def atLabel[A, B <: HList] = at[Label[A], (B, Map[String, Any])] {
case (label, (acc, values)) ⇒
(LabelWithValue(label, values.get(label.name).asInstanceOf[A]) :: acc, values)
}
}
val label1 = Label[Int]("a")
val label2 = Label[String]("b")
val labels = label1 :: label2 :: HNil
val labelsWithValues: LabelWithValue[Int] :: LabelWithValue[String] :: HNil = getValues(labels)
println(labelsWithValues)
def getValues[L <: HList, Out, P](labels: L)(
implicit folder: RightFolder.Aux[L, (HNil.type, Map[String, Any]), combineLabelWithValue.type, P],
ic: IsComposite.Aux[P, Out, _]
): Out = {
val state = Map("a" -> 5, "b" -> "five")
val resultTuple = labels.foldRight((HNil, state))(combineLabelWithValue)
ic.head(resultTuple)
}
}