Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jul 19 20:37
    @SethTisue banned @tonyobanon
  • Jan 02 23:58
    @SethTisue banned @fakubishes:nerdsin.space
  • Dec 15 2021 05:01
    som-snytt commented #12516
  • Dec 15 2021 04:38
    SethTisue edited #1312
  • Dec 15 2021 04:38
    SethTisue opened #2273
  • Dec 15 2021 04:31
    jackkoenig opened #12516
  • Dec 15 2021 04:29
    SethTisue edited #1312
  • Dec 15 2021 04:28
    SethTisue edited #1312
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    scala-jenkins milestoned #9831
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    SethTisue opened #9831
  • Dec 15 2021 03:35
    som-snytt commented #11339
  • Dec 15 2021 03:27
    som-snytt labeled #12494
  • Dec 15 2021 03:07
    SethTisue edited #1312
  • Dec 15 2021 03:07
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 02:58
    SethTisue edited #1312
Fabio Labella
@SystemFw
@dsebban_twitter well, I personally have a fairly precise path for explaining the mindset behind those abstractions, although that path is tuned for completeness of understanding, not necessarily ramp up time, so it's not for everyone
Fabio Labella
@SystemFw

but basically it starts from thinking about what types are for (and not what they are against, to quote Connor McBride), to the fact that they are given meaning and semantics not by their underlying representation, but by the operations you define on them (let's say just functions for now).


The second step would be defining algebras as specifications of part of the behaviour of a data type. This works really well with typeclasses, which give you an has a, rather than the is a relationship you typically get from OO style interfaces. The result of this thought process is that given a datatype, part of what it can do is specified by operations that are unique to that type, and part by algebras (i.e. Monoid, Functor, and so on). This kind of reasoning can be explained with simple types and lower kinded typeclasses only.


Then, you move on to explaining how types of higher kind can be used not just for containers, but for computations as well: List and Option are interesting because they can be viewed both ways, but there are somethings that really only make sense as computations (like State or IO).


At this point you can actually explain F-A-M as algebras that specify part of the behaviour of higher kinded types:

  • Functor lifts functions of one argument into F
  • Applicative lifts functions of n arguments into F
  • Monad gives you context sensitivity: the ability to change the structure of a computation based on the result of a previous one

The final bit is learning how to operate on types based on their algebras and operations only, while being agnostic to their representation (e.g avoiding pattern matching on Option): this is propedeutic to learning about types which either have an opaque representation (IO) or a very complex one (fs2.Stream). It also means that you can tackle a new library which exposes different types, and basically know most of what you need to do to use it once you know which algebras it forms (e.g. doobie ConnectionIO).


I guess the main problem with this approach is that it requires some upfront motivation, and ideally a mentor to give you clear explanations and "unstuck" you along the way

@dsebban_twitter

Daniel Sebban
@dsebban_twitter
I think completeness of understanding is important, shallow understanding for these concepts will limit you and eventually make you give up on FP. I appreciate the answers, piecing together data types -> hkt ->computation -> algebras -> monad is indeed the clearest explanation I have seen so far
Fabio Labella
@SystemFw
yeah, imho is the best way of explaining things but it doesn't fare very well in "what's the point of monads" pub conversations (or reddit flamewars) :P
note that the fact that algebras only describe part of the behaviour of a data type really is crucial: if you understand that, you realise that "how do I extract a value out of a monad" is a question that literally makes no sense on multiple levels : the monad is the algebra, not the type, and extraction appears nowhere in the definition of monad, although it might be part of the behaviour of a given datatype which also happens to form a monad (like State), but makes no sense for others (like IO)
Seth Tisue
@SethTisue
@marcinsokrates_twitter if nobody answers here about Vector branching factor, you might try the scala/contributors room, and/or https://contributors.scala-lang.org
Josh
@joshlemer
I did recently ask about making Vectors of size < 32 use arrays of exactly that size rather than of size 32, @Ichoran responded something to the effect of that allocating the extra bytes are very likely cheaper than the additional logic of handling different array sizes
Also I was specifically thinking that in the case of varargs like in Vector("a","b","c","d"), since in 2.13 these will be passed as an ArraySeq[String], we could just share the underlying array with the arrayseq like...
object Vector {
  def apply[A](elems: A*): Vector[A] = 
    if (elems.length <= 32 && elems.unsafeArray.isInstanceOf[Array[AnyRef]]) { 
      new Vector(0, elems.unsafeArray, 0) 
    } else { ... } 
}
Marcin Sokrates
@marcinsokrates_twitter
@joshlemer I thought about making the two most leafward levels 4 or 8-wide
Need to try it at home
That's neat
Daniel Sebban
@dsebban_twitter
Thanks @SystemFw and @tpolecat for the great explanation on how to explain what a monad is :)
moritz bust
@busti
Is there any good library for doing some very basic vector and matrix mathmatics for graphics in 2d and 3d space?
I would use spire, but I feel like vectors have quite a lot of overhead in it and it does not support matrices.
Josh
@joshlemer
@Busti I haven't used it but there is http://www.scalanlp.org/
moritz bust
@busti
@joshlemer But that feels like it really is more about manipulating large matrices using the GPU. I really just need a 4x4 matrix to work in scala-js
Josh
@joshlemer
Sorry I don't know what would work in scala.js
Peter Aaser
@PeterAaser
Sure wish I knew of gitter when I was trying to figure out monads...
moritz bust
@busti
no worries. It might be smarter to ask the scala-js channel about this.
Peter Aaser
@PeterAaser
By the way, does anyone here use recursion schemes? I found them quite interesting, but I don't really know what sort of problems they are well suited for
Rob Norris
@tpolecat
I use them in a lot of things that never make it into production :-)
Peter Aaser
@PeterAaser
haha
Yeah I don't think it's something I need, but I thought they were cool
And I remember last time I looked at them they made no sense at all to me, and when I looked at them this time it was obvious
pretty great feeling
Rob Norris
@tpolecat
Cool!
I think a lot of the time you can specialize to Free or Cofree so you see those pretty often.
Peter Aaser
@PeterAaser
Yeah, self deprecation aside I do feel I'm getting the hang of programming ;)
Rob Norris
@tpolecat
Cool, what's that like?
Peter Aaser
@PeterAaser
Feels like I can finally make software that is actually good
Rob Norris
@tpolecat
:-)
Peter Aaser
@PeterAaser
I mean I've always claimed I'd never ever cross a bridge that was made by a programmer
and I think that it shouldnt have to be like that
and I feel like I'm getting closer to being able to make software that doesn't fall apart when you look at it wrong :D
Rob Norris
@tpolecat
Anyway I think compilers are probably the canonical use case for recursion schemes because you typically have a bunch of orthogonal annotations and a bunch of passes that you's like to fuse together, so it kind of matches up.
Peter Aaser
@PeterAaser
Yeah, the quasar stuff I saw was really cool
Do you use it in doobie?
Rob Norris
@tpolecat
I have this grand idea for a talk that goes from text file to classfile in one grand hylomorphism but I havent gotten around to figuring out how to do it ;-)
Peter Aaser
@PeterAaser
In what sense, how to make it understandable or how to implement it?
Rob Norris
@tpolecat
Doobie is all built with Free so depending on how much you want to squint it's doing recursion schemy stuff.
Well, both. And how to fit it into a 45-minute talk.
Peter Aaser
@PeterAaser
Just talk really really fast
moritz bust
@busti
hylomorphism sounds really scary
Rob Norris
@tpolecat
Yeah the terminology is really intimidating.
I did a talk at Scala World a few years ago that introduces this stuff pretty gently, if you're interested.
moritz bust
@busti
It is one of those terms where you do not know what to expect in a programming context and then you google it and find out that it is used in philosophy instead.
Rob Norris
@tpolecat
Not even generalized recursion schemes, but enough to get you to understand fixpoint types, which was kind the big mental barrier when I was learning this stuff.
Peter Aaser
@PeterAaser
Yeah, fixpoint types was the difficult part for me too
Rob Norris
@tpolecat
I think the hard part is understanding Fix and ana and cata and then it's just variations of the same ideas.
Peter Aaser
@PeterAaser
it's the sort of thing I like to believe I could explain very easily in an interactive conversation, but not in text or a talk
Fix that is