What the Functor? | Functors in Scala | Rock the JVM

  Рет қаралды 11,568

Rock the JVM

Rock the JVM

Күн бұрын

Written version: blog.rockthejvm.com/what-the-f...
Scala & Cats course: rockthejvm.com/p/cats
Links from the video:
Type classes: • Why are Type Classes u...
Scala 3 Given/Using: • Given and Using Clause...
Monads intro: • A Monads Approach for ...
This is a video primarily for Scala folks, but it transfers to other languages e.g. Java, TypeScript, C++ or other typed languages.
In this video we'll explore functors, a critical piece of abstract functional programming. Rather than explaining the theory behind it, we go through a concrete use-case of generalizing an API that would otherwise be terribly bloated and repeated. We show how functors work, how they represent the transferable concept of a "mappable" structure and how we can put them in practice and attach a functor to data structures which are mappable, but don't have a standard map method.
Follow Rock the JVM on:
LinkedIn: / rockthejvm
Twitter: / rockthejvm
Blog: rockthejvm.com/blog
-------------------------------------------------------------------------
Home: rockthejvm.com
-------------------------------------------------------------------------

Пікірлер: 33
@jimaaman
@jimaaman 3 жыл бұрын
As always, a high quality tutorial and explanation 👍 particularly excited to see more examples of Scala 3 in action.
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad you liked it!
@MrOliwander
@MrOliwander 3 жыл бұрын
I have grasped it finally. Thank you, Daniel! I can't wait to watch another similar one for monads.
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad it helped!
@mahesh_kndpl
@mahesh_kndpl 7 ай бұрын
Top notch quality. Loved it.
@Dr_Dude
@Dr_Dude 3 жыл бұрын
This is one of the best videos i watched on this channel, this is a must for all those wanting to do proper functional programming, and of course the rest of the category theory programming imo (monad, monoid, type classes -- supported in scala 3 now without too much typing-- and many others)
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad you liked it!
@alanlewis1625
@alanlewis1625 3 жыл бұрын
Hi Daniel Thanks!! Yet another awesome video :-) . Great content, and perfect length.
@rockthejvm
@rockthejvm 3 жыл бұрын
Glad you liked it!
@massimodaros391
@massimodaros391 9 ай бұрын
Wow Daniel !! This video is very inspiring Scala rocks !!
@ZoYaBoBo
@ZoYaBoBo 2 жыл бұрын
You are awesome, still looking your videos one by one
@MrCesarification
@MrCesarification Жыл бұрын
beautiful
@MartinZachrison
@MartinZachrison 3 жыл бұрын
Very good! Takes the ghost out of the Functor
@rockthejvm
@rockthejvm 3 жыл бұрын
That's the goal
@TJ-hs1qm
@TJ-hs1qm 5 ай бұрын
11:37 line 32 could you further abstract over the concrete type C[Int]... similar to C[?] perhaps
@Nebulorum
@Nebulorum 3 жыл бұрын
Really like the video. After a mid video epiphany I would love to see a series of this small type classes building something larger for free. E.g. Functor + Applicative + Monoid of Tree allows me to do X for free, for some X that is complex.
@rockthejvm
@rockthejvm 3 жыл бұрын
Nice nuanced wish. Will think of something
@eugenehlushin1746
@eugenehlushin1746 Жыл бұрын
great
@amol_
@amol_ 9 ай бұрын
Great Explanation, now please consider to make video on Applicative.
@rockthejvm
@rockthejvm 9 ай бұрын
The Cats course has all of them in great detail!
@pr0master
@pr0master 3 жыл бұрын
Hi, question: I still do not understand the exact applicability of an Functor, or better say, in combination with a typeclass. I do understand the concept of "mapping" and reusing this construct to multiple datatypes, but, you said in the video that you reduced this API to a single method with the given functor implementation. How I see it, is, what you actually did is moving the implementation to multiple typeclasses (list, option, leaf etc.). You need the implementation of the map function for different types. Why using all these abstract and complicated language constructs to reduce the method/api to a single method? So, why not just define a simple trait / interface? No typeclasses, no extention functions, no implicit stuff (dependency), no misdirection etc.
@rockthejvm
@rockthejvm 3 жыл бұрын
Legit question. The idea is we're moving code so we can keep a "stable" API and then "plug" in different implementations as we see fit. The moving of code to reduce duplication happens at all levels. Think of the standard OO polymorphism: it serves a similar purpose, whereby you rely on an instance of a general type (e.g. Animal) in a stable API but then at runtime you can use Cats, Dogs, Crocodiles in your actual logic, instead of explicitly handling Cats, Dogs, Crocodiles directly in your public API.
@Nebulorum
@Nebulorum 3 жыл бұрын
Quick comment this works with Scala 3.0.0M3 but there have been some syntax changes. Fundamentally the as token was dropped.
@rockthejvm
@rockthejvm 3 жыл бұрын
Thanks for the tip - take the ideas and stick the new syntax.
@Jankoekepannekoek
@Jankoekepannekoek 3 жыл бұрын
At 4:23 you mention that java.util.Optional has the same semantics as scala.Option, but that is not true; Java's Optional can't contain null, but Some(null) is perfectly allowed in Scala. While probably no programmer on earth would explicitly instantiate an Option with null on its initial creation, this situation can still occur when trying to map a function over an option. If the function used as the argument for map returns null, then Java's Optional#map will return the empty Optional, but Scala's Option#map will return Some(null).
@rockthejvm
@rockthejvm 3 жыл бұрын
Partially true. Some(null) is allowed in Scala but it's an anti-pattern, much like Optional.of(null). The map method of optionals will return None if the resulting optional is empty. Even so, the validity of the statement does not change: the semantics are the same, as both Scala Option and Java Optional were built for the same purpose.
@user-tm8ff8xf4w
@user-tm8ff8xf4w 3 жыл бұрын
quite the beginner here so sorry if its a dumb question -how does a functor differ from a generic ?
@user-tm8ff8xf4w
@user-tm8ff8xf4w 3 жыл бұрын
btw thank you for all your vids, they are invaluable !
@luismiguelmejiasuarez2020
@luismiguelmejiasuarez2020 3 жыл бұрын
What is a generic for you? And why you think a Functor is something similar? - BTW, I would encourage you to ask this and future questions in the gitter channel or in the Scala sub-reddit since you would get more audience :)
@rockthejvm
@rockthejvm 3 жыл бұрын
A "generic" is the language feature of using the same code for more than one type, e.g. the List logic is the same for Ints, Strings, etc. A Functor is a particular kind of concept (a "mappable" data structure) that we attach to a particular set of types (in this case, List, Option and Tree).
@user-tm8ff8xf4w
@user-tm8ff8xf4w 3 жыл бұрын
@@rockthejvm thank you for this just seen it !!my understanding is slowly but surely improving, thanks to you ! appreciate it
@serragnoli
@serragnoli 3 жыл бұрын
Is there a button to like the video twice?
@rockthejvm
@rockthejvm 3 жыл бұрын
:D
Objects and Companions in Scala | Rock the JVM
14:01
Rock the JVM
Рет қаралды 8 М.
Algebraic Data Types (ADT) in Scala | Rock the JVM
14:19
Rock the JVM
Рет қаралды 10 М.
Кәріс өшін алды...| Synyptas 3 | 10 серия
24:51
kak budto
Рет қаралды 1,3 МЛН
Super gymnastics 😍🫣
00:15
Lexa_Merin
Рет қаралды 96 МЛН
Idiomatic Error Handling in Scala | Rock the JVM
26:45
Rock the JVM
Рет қаралды 9 М.
Cats with Scala: Essential Type Class Hierarchy, Explained
35:00
Rock the JVM
Рет қаралды 17 М.
Scala: Beyond the basics with Christopher Batey
48:05
Devoxx
Рет қаралды 48 М.
Self-Types in Scala - the Why and the How | Rock the JVM
14:57
Rock the JVM
Рет қаралды 7 М.
Given and Using Clauses in Scala 3 | Rock the JVM
20:24
Rock the JVM
Рет қаралды 6 М.
Functional Programming - 22: Functor
19:21
Web Village Voyage
Рет қаралды 4,7 М.
A Load Balancer in Scala with Cats Effect and Http4s
37:33
Rock the JVM
Рет қаралды 3,8 М.
This Doesn't Make Sense - Scala Loops
13:20
Rock the JVM
Рет қаралды 7 М.
WWDC 2024 - June 10 | Apple
1:43:37
Apple
Рет қаралды 10 МЛН
ВЫ ЧЕ СДЕЛАЛИ С iOS 18?
22:40
Overtake lab
Рет қаралды 120 М.
Apple watch hidden camera
0:34
_vector_
Рет қаралды 62 МЛН
Main filter..
0:15
CikoYt
Рет қаралды 5 МЛН