## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
Fabio Labella
@SystemFw
basically when you nest functions, things switch from positive to negative to positive to negative (and so does their variance)
in this case: flatMap takes f that returns Sum
georgreen
@georgreen
@SystemFw, thank you.
Fabio Labella
@SystemFw

f: B => Sum[A, C] and construct it from trait Function1[-I, +O]{ def apply(i: I): O} it seems B is the contravariant argument while Sum[A, C] is the covariant term in this function

Right, but now if you write flatMap you f is in turn in negative position, so all the variances switch

anyway, I would fully agree it's very confusing
this is tl;dr
Positive position: the type variable is the result/output/range/codomain of the function
Negative position: the type variable is the argument/input/domain of the function
When a type variable appears in positive position, the data type is covariant with that variable.
When the variable appears in negative position, the data type is contravariant with that variable.
so if you apply these rules and try to work the type of flatMap out, it should all fit
in f, Sum is in positive position (return type)
flatMap is a function that takes f, so all of f is in negative position, which makes B have positive position and Sum have negative position
which makes it contravariant
the least crap intuition is multiplying numbers by 1 and -1, they keep switching. Roughly speaking, functions of functions is the multiplication
@msali123
Hi
I'm new here. Is there any way to host a project built with scala. On github pages or etc?
nano
@mr-nano
Hey folks. Glad to find this channel. Wish for everyone's safety in this crazy virus time!
Noel Welsh
@noelwelsh
:+1:
@eusojk
Hi all,
I just started my Scala/FP learning journey with Creative Scala. Very fantastic and engaging so far :raised_hands: !
For some reason, the images (figures) on the website don't render for me. I've tried multiple browsers with no luck. Has anyone experienced this by any chance? Thanks in advance!
@arosien
@eusojk no, you're right, the images for the book on creativescala.com aren't loading. i'll let @noelwelsh know! thanks
@eusojk
Thanks @arosien, appreciate it!
Noel Welsh
@noelwelsh
Thanks for letting me know. I don't know why the images aren't working (nothing has changed in the build system) but the system we use to build the books is well past its use by date.
Paul Carey
@paulcarey
hi, what does the [_] before the List in the following refer to?
List(1).withFilter(_ > 0)
val res63: scala.collection.WithFilter[Int,[_]List[_]] = scala.collection.IterableOps\$WithFilter@cf76c0b
@arosien
@paulcarey there are 2 type parameters to WithFilter, and the second one is List[_], where [_] is syntax for "the type parameter is missing". List is a type constructor with one type parameter, and if you supply the type parameter, like Int, you get back a type List[Int].
note this syntax only applies to type parameters. if you have a value parameter def something(list: List[_]), then the [_] is a wildcard, meaning you can pass a list value whose elements are any type.
([_] there means "i don't care what the type is, so i'm going to forget its name", just like _ in places like pattern matching means "i don't care what this is, i'm not even going to name it")
Alan Lewis
@london-coder
Hi All, just joined, so I hope I might get a bit of help, and not offend any members if my enquiry is too simple. I've looked at @davegurnell validation framework. A nice library, that I'm sure is useful in a number of scenarios. However, I have a much simpler need to validate values in a single case class. Is there a 'lighter weight' capability that will be suitable for a smaller requirement?
@arosien
@london-coder you could just use "plain" functions to validate, perhaps in the case class or companion object. the functions could return Either or perhaps cats.Validated
Alan Lewis
@london-coder
Hi @arosien thanks for the quick reply. I'll give that a try. But, I was hoping I could use the validation (of 2 case class field values, and for any duplicate entries) in a for comprehension. Yes, I realise that means the mechanism will require map and flatMap methods.... Therein lies my difficulty. I'm not confident in implementing those methods for non-collection types
@arosien
ah i see. and the flatMap doesn't exist on Validation, and will "fail-fast", when you probably want to collect all the errors

if you use cats you can follow the examples at https://typelevel.org/cats/typeclasses/parallel.html#parallel

basically you write your validation functions to produce Either. but you "glue together" those functions to act using Validated, which then gets converted back to Either at the end

that is, you can't use a for-comprehension if you want to collect all the errors. you need something like mapN from cats. it's like a mode: you switch from fail-fast (flatMap via Either) to collect-errors (mapN via Validated), and back.
@arosien
let me know if any of that makes sense :)
Paul Carey
@paulcarey
@arosien thanks, but doesn't your answer refer to the second [_] in [_]List[_]? I'm wondering about the first one.
@arosien
ah. i think i mentally didn't see the first [_], which is some sort of toString artifact. it's not legal scala syntax.
it should have printed val res63: scala.collection.WithFilter[Int, List] = ... to be correct
(i think)
Paul Carey
@paulcarey
ok, that's what I would have expected. I was a bit puzzled by the preceding [_].
@arosien
yeah, i can guess that there's some anonymous parameter that it is choosing to also anonymize, which makes it extra confusing.
REPL output should be valid REPL input!
Paul Carey
@paulcarey
thanks
@arosien
np
Alan Lewis
@london-coder
@arosien Appreciate the help, it did make sense, thank you. If you have a bit of time, could you possibly elaborate slightly on what you meant by "glue together"? I don't want you to think I'm asking for a complete solution, just a thumb nail sketch? Or, will having a look at the cats example be enough?
@arosien , I've had a look at the examples in the link you included. I see what you meant now... many thanks.
@arosien
no problem.
"glue together" = more than 1 Either => 1 Either. a for-comprehension does this, but mapN also does it.
Paul Carey
@paulcarey
actually, I see the same syntax - [A]Foo[Int, String, A] - on this post about partial unification, so I guess it does have a specific meaning that I'm not familiar with
@arosien
what REPL is that? from sbt?
i think it is a "type lambda": we need a name for a unsupplied type parameter, so you first name it ([A]), then refer to it later in the type expression