 Baiklah, terima kasih kerana datang, terima kasih untuk Spencer dan Oldham. Ini pekerjaan yang sangat bagus. Jadi nama saya Raymond. Saya telah menjadi pemeriksaan software untuk masa yang lama. Jadi, hari ini adalah untuk berkongsi dengan anda tentang perkara ini yang dipanggil Katz. Saya tidak akan membuat joket kru, tetapi itu pasti tidak musikal. Katz adalah projek yang ada di sebuah pemeriksaan, pemeriksaan yang dipanggil Type-Level. Saya tidak mempunyai Type-Level atau sebuah pemeriksaan, tetapi hanya kawan-kawan yang mempunyai dari masa ke masa, minta tukar pertanyaan dan belajar daripada orang lain. Jadi, boleh saya mempunyai sebuah pemeriksaan? Berapa banyak anda sebenarnya dengar tentang Katz atau Scala-Z? Oh, wow! Benar-benar! Saya gembira. Bagus. Jadi, hari ini saya hanya akan berkongsi dengan anda apa yang saya lakukan di sebuah pemeriksaan tentang menggunakan Katz dari sebuah pemeriksaan dari sebuah hari-hari. Okey, tentang saya, saya menulis dua pemeriksaan. Satu pemeriksaan adalah Aka, saya menulis dengan Thomas Logney. Jadi, kami berdua untuk menghubungkan masa keperluan pada pemeriksaan Aka, sehingga lebih awal apabila kami lebih mudah. Jadi, yang lain adalah Pemeriksaan GPU. Saya mempunyai sebuah pemeriksaan tentang membuat Pemeriksaan GPU pada tahun 2013. Jadi, seseorang menarik saya untuk membuat pemeriksaan, jadi saya melakukannya. Jadi, anda mahu belajar bagaimana untuk menggunakan CEL untuk menggunakan pemeriksaan GPU dan membuat pemeriksaan untuk membuat pemeriksaan. Saya mempunyai 60 algeradam di dalam sana, yang mempunyai algeradam stokastik. Okey, mari kita jumpa dengan cepat. Okey, Katz. Sebelum anda belajar Katz, ada banyak nama akar yang perlu digunakan. Mereka panggil fungtas, aplikatif, monads, monoids, semi-groves, validasi. Ada banyak perkara yang perlu digunakan. Namun, saya boleh beritahu anda perjalanan. Selepas anda memahami apa yang mereka tentang, sebenarnya anda boleh mengubah cara anda menggunakan aplikasi. Tapi, untuk saya, saya adalah pemeriksaan. Pertama, apabila saya melihat algeradam, saya rasa saya sesuai dengan keberanakanan, perkara apa yang benar-benar? Apa yang benar-benar adalah aplikatif yang paling tidak berlalu? Kenapa orang berkata aplikatif yang haram? Apa perniagaannya? Saya tidak menganggap tentang perniagaan ini. Saya bukan seorang seorang seorang seorang seorang seorang... Saya bukan seorang seorang seorang seorang seorang seorang referasinya. Tapi saya suka membuat hidup saya lebih mudah. Saya sudah 30an, mereka di sana 8 alalat, dan saya mahu pergi rumah awal daripada mungkin tidak sepanjang dan jadi efisien, itu saja. Pertama, apabila anda mempunyai pengalaman fungsi, penyelidikan, penyelidikan seperti Kat atau Scalazi, perkara pertama yang penting yang anda perlukan untuk menggantikan diri anda adalah bahawa mereka benar-benar membangun, bahawa orang yang benar-benar senyum mempercayai sebuah pattern yang terkenal, mempercayai dan memberikan nama. Sesetengah orang tak suka nama. Sesetengah orang suka. Sesetengah orang tak ada pilihan tapi mempercayai. Jadi perkara penting yang penting di sini adalah secara separasi keadaan. Jadi apabila anda menulis kota, selalu ada tiga perkara yang penting. Anda mempunyai pilihan pertama yang tidak ada apa-apa lagi daripada menginitialisasi. Kemudian anda mempercayai data dari sebuah kota atau sebuah MQ3 atau sebuah kota. Anda mempercayai keadaan ini dan kemudian anda mempercayai nama. Selepas keadaan ini berakhir, keadaan ini berakhir, anda perlu mempercayai apabila anda mempercayai atau mempercayai sesuatu yang lain. Jika anda melihat bagaimana anda mempercayai keadaan ini, selalu ada tiga pilihan yang penting. Jadi... Jadi... Ya. Okey. Dalam keadaan, SKALASI Haskell. Mereka mengambil konsep klas type. Jadi anda perlu faham apa klas type. Adakah semua orang tahu klas type? Anda tahu? Ya. Okey. Mereka mempercayai dari Haskell. Mereka mempercayai bahawa mereka ingin mempercayai java yang berada di java. Anda mempercayai keadaan ini. Tapi kemudian anda mempercayai keadaan ini selalu berlainan dalam keadaan lain. Dari contoh java, anda mempercayai keadaan yang sama. Kemudian, dalam beberapa keadaan yang lain di SKALA, anda perlu berkata, apa yang sebenarnya sama untuk keadaan saya dan sebagainya. Jadi Haskell orang mempercayai bahawa kita patut mempunyai keadaan yang unik atau apa yang berkata-kata. Jadi itu berkata-kata kepada idea klas type. Jadi anda mempunyai klas type yang lain seperti monoi, seperti semi-groups, seperti fungtus, surplikatif. Itu adalah keadaan yang dipercayai untuk cara anda ingin mempunyai keadaan anda dalam keadaan. Jadi, mengahwini keadaan ini, daripada idea klas type. Ya, orang-orang berkata, Ya, saya akan berkata, SKALA adalah keadaan yang besar dan ada lebih daripada satu cara untuk menerima sesuatu. Ia menjadi penyakit sesungguh-sungguh. Pada awal, pada tahun 2008, orang akan beritahu anda, Oh, anda perlu menggunakan pakaian pakaian. Kemudian, apabila anda meletakkan pakaian keadaan, pada tahun 2009, seseorang berkata, itu adalah idea yang teruk. Kita harus berhenti menggunakan pakaian. Jadi sekarang, tahun 2017, beberapa orang masih sedia. Mereka berkata, okey, pakaian pakaian itu bagus. Ia berkata pada pakaian. Saya tidak melihat kenapa ia tidak boleh berkata-kata di mana-mana lain. Saya personalnya tidak suka pakaian pakaian kerana terlalu banyak magik untuk saya. Jadi saya suka pakaian saya untuk menjadi eksplosif. Sebab itu, saya menyebabkan penyakit saya. Kerana, jika anda telah melihat Mao Xiebin mempunyai program ini, saya akan menyebabkan penyakit yang tidak teruk. Saya rasa dia mengambil segala-galanya keadaan. Dalam pakaian yang dibina, pakaian pakaian yang berkata-kata dengan pakaian yang berkata-kata di skala untuk membuat sebuah ekspresi untuk menggabungkan dan menggabungkan idea seperti, seperti biasa di Algebra. Di SM. Maaf, saya mendapatkan sedikit terlalu lama. Okey. Jadi, sebenarnya, kita perlu tahu tentang kelas kaya di skala atau bahan-kaya. Saya harus berhenti mengatakan skala. Mereka mempunyai idea-idea keadaan. Kemudian, keadaan ini mempunyai konsep Haskell. Seperti keadaan, keadaan identiti, keadaan komposisyen, etc. Jadi apabila anda mempunyai kota-kata, jika anda mempunyai kota-kata jika anda mempunyai kota-kata, anda perlu pastikan kota-kata anda perlu mempunyai kota-kata yang digunakan dan memastikan kota-kata kata-kata yang digunakan. Jika tidak, saya ingat ada sebuah kota-kata yang baru-baru disebabkan flat-map atau slope. Daniel Speewab sebenarnya pergi online dan beritahu anda, pastikan semua kota-kata anda mempunyai kota-kata sebelum anda mempunyai kota-kata. Jadi, itu yang saya katakan. Ya. Baiklah. Jadi, mari kita bergerak. Semua orang berminat dengan kota-kata yang digunakan. Sebenarnya? Baiklah. Jadi, dalam kota-kata ini, ada idea untuk menghubungi perjalanan. Perjalanan adalah kebebasan. Itu seperti, orang yang menghubungi, ia seperti java interface. Jadi, ada idea untuk mempunyai kota-kata yang digunakan. Jadi, anda perlu faham. Jadi, kita akan menghubungi. Jadi, kita akan mempunyai beberapa perjalanan. Kita akan menghubungi kota-kata yang basic. Saya akan menunjukkan kota-kata yang saya pernah lakukan. Kota-kata orang saya di sini sebenarnya pernah melihat kota-kata yang saya pernah lakukan. Ya. Jadi, tapi pertama, kita perlu mengambil sesuatu sorta terminologi. Jadi, mari kita lakukan perkara ini. Jadi, semai grup. Sebenarnya, semai grup dan monoi mempunyai kota-kata yang digunakan. Mereka menggabungi sesuatu. Bagaimana saya tahu bagaimana menggabungi? Di kota-kata, di kota-kata atau di kota-kata, seperti ini, bagaimana anda ingin menganggap cara anda menggabungi dua perkara bersama dengan kota-kata A. Mereka menggabungi semai grup. Jadi, perbezaan antara semai grup dan monoi adalah perkara ini disebabkan kota-kata yang digunakan. Sekarang, perkara ini sangat menggabungi apabila, bagaimanapun, anda mempunyai dua kelas agrogat. Anda perlu mengagrogat. Mereka mempunyai banyak kelas yang sama. Tetapi, anda tidak mahu. Mereka tidak mempunyai kelas yang sebenarnya menggunakan monoi. Jadi, sebenarnya, ada kelas yang digunakan di sini. Setiap kelas ini sudah digunakan dalam kelas kata-kata sendiri dan kelasnya diberikan kepada anda. Jadi, perbezaan antara semai grup yang bermakna, bagaimana saya mempunyai satu plus satu sebagai perbezaan antara semai grup? Tetapi, anda akan melihat satu, satu kelas yang berbentuk. Sebenarnya, ada kelas yang berbentuk. Ini adalah kelas yang berbentuk. Jadi, dalam kelas yang berbentuk, dalam monoi yang anda akan melihat di kelas yang berbentuk, sebenarnya, ada kelas yang berbentuk. Yang berkata, sebuah metode antara untuk monoi, ini sebenarnya berbentuk. Jadi, jika anda memahami kelas itu, anda memperkenalkan kelas yang lebih generalisasi, ia menerima anda membuat kombinasi dari dua perkara. Saya akan menunjukkan kelas yang berbentuk. Ini adalah monoi yang berbentuk. Tidak ada perbezaan. Dan sebenarnya, ada perbezaan yang berbentuk sebuah metode antara. Sebenarnya, ia berkata, beritahu saya, apa ini elemen inisial untuk kelas A. Jika ia adalah kelas yang berbentuk, ia selalu berbentuk. Jika ia adalah kelas yang berbentuk, ia akan menjadi kelas yang berbentuk. Atau benar, setelah bagaimana mereka berbentuk. Sebelum itu, ia okey? Okey. Jangan risau untuk berhenti saya. Jadi, ini adalah contoh bagaimana monoi dapat dipindahkan. Ini adalah perbezaan yang berbentuk. Apabila anda bermain dengan kelas, anda akan mempunyai ini. Di Scalasi, anda selalu Scalasi.underscore dan kelas Scalasi.underscore. Sekarang, ada beberapa perkara anda ingin mengerti di sini. Sekarang, bagaimana perkara ini berbentuk adalah bahawa ia sebenarnya menghormati kelas yang berbentuk. Kelas yang berbentuk yang mempunyai monoi untuk kelas ini. Pertanyaan ini. Pertanyaan ini. Dan kemudian, ia tahu bagaimana menggambarkan kelas. Jadi, apabila anda mempunyai monoi untuk kelas, bukan kelas mt, ia adalah kelas mt. Sebab itu, ia adalah sebab itu. Jadi, ini adalah sebuah syntax. Sekarang, apabila anda menulis kelas monoi, anda boleh buat sesuatu seperti itu. Ia sedikit menarik sebab, tergantung pada syntax Sekarang, perkara yang dipercayai dan mempunyai kelas, perkara yang dipercayai dan tidak mempunyai kelas. Jadi, saya tahu, sekurang-kurangnya, salah satu atau dua kreator yang mempunyai kelas yang tak berbentuk, mungkin semasa semasa, cuba mempunyai kelas yang sebenarnya mempunyai kelas, dan cuba mempunyai membuat kelas, membuat kelas untuknya. Tak ada apa-apa yang terkenal di sini. Di sini, jika anda membuat kelas yang cukup, anda tahu bahawa ada cara ini mencari sesuatu di dalam kelas. Sebenarnya, ia adalah kelas yang tepat. Dan kemudian, ia mengambil kelas. Sebab, ia melihat cara ini, mengambil kelas untuk monoi dan mempercayai kelas ini. Jadi, itu adalah cara yang boleh dibuat. Jadi, kelas untuk monoi dan semi-group adalah untuk mengambil kelas. Berhati-hati. Tak ada kelas yang diambil. Ia boleh mengambil kelas yang lain. Jadi, jika anda membuat kelas, monoi yang anda mahu mengambil kelas, anda perlu pastikan ia boleh mengambil kelas yang lain. Maksudnya... Maaf? Ia tidak mengambil kelas. Ya, anda betul. Kamu lihat? Saya bukan seorang teori. Tetapi dalam kelas, saya memahami apabila saya bermain, anda perlu berhati-hati tentang kelas ini. Apabila anda mengambil kelas, mempercayai kelas monoi anda sendiri. Ada beberapa kelas yang diambil. Maaf? Maaf. Maaf datang ke sini. Ia sepatutnya sepatutnya. Ya, ia sepatutnya sepatutnya. Ia sepatutnya. Okey. Jadi, apa ini? Jadi, sebuah projek yang saya bekerja, saya cuba beritahu, beritahu anda idea apa yang kita buat. Jadi, saya bekerja dalam perniagaan perniagaan. Jadi, basically there is a data stream of data of different types that is coming in. But they all have a have a similar behaviour. They all carrying values, carrying something, carrying something. So, when you hit the end point after we do a passing, we need to collect them in that sense. So, these two data types are very similar in the behaviours in the data that they carry. So, our job is actually to do passing. So, this is a very, it's like a pseudo code. Basically, there is this method, right? That collects the data types from stream itself, represented here by called data type A, data type B. Then there is an accumulator. Now, over here, basically what we have done is actually in the validated data, we basically do some validation and because it's actually doing, it will return basically a left or a right. In terms of Scala syntax, it's actually the either.left or the either. Right. So, if there's an error, this is what we do. If it's not an error, this is what we do. So, a lot of the code that we write are always in this kind of fashion, in a sense. So, what we did is that, because we know that there are common paths inside the data types that we created this idea of a monoid so that we can copy the errors in and then we can sort of put it into the accumulator. So, the accumulator carries both the values and the errors at the same time. So, this is what we basically do, at least in this current employment setting, is by creating, by leveraging, creating a monoid for this data type and then initialising it and capturing errors or values. That's what we do. Any questions? Or is it like, hmm, what the hell is this? So, okay. Now, in validated data, it's actually an either type. So, it either returns a left or a right. In CADs, you can actually invoke the foal. The foal basically looks at the the return value of valid data and then if it sees that it's actually an error, I will apply this function over here. Now, this is a anonymous function. If I see that it's actually, ooh, good. The data has been validated and has been passed. This is what I literally do. Okay, this is not the actual code because I have to skip out some parts of it so that, you know, it's not meant to be added in public, but that's a general idea. Okay. So far, any questions about Monoids? So, it's about combining things. Combining. Okay. So, the next thing is actually a functor. Eh, sorry. Ah. Okay. Okay. So, for Monoids, basically there are actually two ways you can do this. Let's say if you're using a rappel, if you're using it, if you invoke the colon and then implicit, it will lift up and show you what are all the implicit for the Monoids that is being showed on the rappel itself. The second way is actually to read the source code for the cats itself. The instances, there aren't too many of them. There aren't too many of them. But the defaults are always being implemented. The usual primitives, the strings, bullions, for list, sequences, etc. Collections, the collection stuff. So, yeah. Oh, okay. Okay, so the next thing is functor. So, so what's a functor? So have you heard of functors? So functors is nothing more than something that can be mapped over. So, but I'm actually trivializing this holding. What it actually does is that what we're trying to say is that there's a functor that takes in a type parameter that has a hole inside. This thing is called a hole. So, actually you come across them all the time. They are lists, right? A list of ints, a sequence of ints, a vector of ints, an option of something. They are all, they conform to this type signature because there's a hole inside. That means I know the outside which is an optional list but I don't really know what you're carrying inside that value. Now, what this signature says, if you give me this thing of type A and you tell me, you give me a function that transform the values inside here from A to B, I can give you F of B. So, it means if I have a list of integers, you give me a function that transforms integers to strings. I can actually do it for you. It's called functor. So, in CADs itself, you can see that that's what they always say, it's something that can be mapped over. So, if you use options, futures, you must have come across this. It's just that maybe you don't know the terminology. But that's what it is. So, I should sort of explain why you need to know the terminology. It's because if you go on to Gitter, everyone has used Gitter. If you go to the type level site and you start asking them, they will ask you, oh, can you show me your buy functor code? Are you talking about this applicative? You'll be like, what? So, I mean, what I'm trying to say is that in order to communicate with them, there should be some terminology that you can sort of communicate. Because these guys are like type people. They're like type people. I have to like show them code and I was like, oh, you mean it's a, I was like, oh, that's what you call it. Yeah. Then by the time you'll be like 1am, I'll be like so sleepy. Yeah. So, no, the first thing is, once you understand this concept, you actually know that they're actually very common structures. They're very common. Actually, you've been using them when you're starting using Scala. They are already there. It's just that I don't know how they, I mean Scala was developed much earlier than Katz. So, they don't have the idea of generalizing it into a functor. So, they call it a map. You will see it in your Scala documentation on your browser. They're actually they're very commonly used in this thing called Recursion Schemes. I learned about this this year when I was with Heody at Scala Matsuri and Powerwell, this sports guy, Powerwell, sorry, if I cannot pronounce his name correctly, he gave an excellent talk about Recursion Schemes. So, they actually they're used really frequently. Yeah. Okay, let's have a look at an example. Okay, Funtas. Whoa, what is this thing? Again, it's the usual import and what does this thing do? Now, this is an idea where it's actually lifting a function to be applied to a list. Now, when you create something like that, you actually have reusable code. When you create this thing, it won't work for any list of integers because you already gave it. You already told it what it needs to do. You basically lifted this function and say that whenever you see a list of integers and I want to apply this function itself, this is the function you need to apply for me. So, when you sort of go through and what happens is that when you apply it to a list of one, it actually will give you a list of two. So, the Funtas are actually very common structures. You actually do them all the time. It's just that you don't know they were there in the first place and how you can actually create reusable code. So, what you notice once you learn about how to lift functions and then so that they live inside a context, this is what they call a context, you can actually create a lot of reusable code that people can go share because you already done it for them. Okay. So far, how is it? It's like complete blur. I don't really care. I just came for pizza or something. Okay. All right. We're coming to the next part at the top. It's called monas. Okay. Martin Odeski said it very nicely in this year's Scala Days. It's vocal sequencing computations. Sequencing. When I first started out with cats and ScalaZ, when I look at this thing called a mona, I was like, what? You see a lot of blog posts talking about how you can do this, can do that. It's like, okay. So, what's the whole point? I didn't quite get it back then. So, later on the funny thing is how I learned about this is actually I went to study Haskell. Then I realized, oh, so this is what you meant. Yeah. So, I'm not asking you to study Haskell but I find that it's very beneficial if you study Haskell so that you can go read somebody else's code. So it's meant for sequencing computations. So what actually does it mean? Monas is nothing more than flat maps. You're just flat mapping flat mapping something. So this you must have seen, you must have done this, right? Have you done this before? You have some list, you flat map and you say, oh, I have two lists. I want to create tuples of this list. I go through them. I iterate, iterate and I create a tuple. I always do this. Now, the nice thing about flat maps is actually it actually helps you to short circuit your computation because this created if either this or this is empty. So it's a nice way to say something about your computation. I mean, of course in Java you can say the same thing if this if this is empty and this is empty don't do something, don't do something but it's kind of ugly. So it's one way you can do it. So this is the point I was trying to iterate. Monas is good for short circuiting of your computations. Okay. So monas is about sequencing. So just remember that is about sequencing. Taking something at the top and then saying, oh, I have a value and if there's something at the bottom or pass it to the guy on the bottom and the bottom recursively computes oh, I have something and pass it along. I keep passing it along. This is a quick summary of how many kinds of monas are being supported in cats. I actually left out one. It's called WST which stands for reader, writer, state and eval at the same time. They inherit this thing from the Haskell side. So you will see a lot of people talking about a writer monad, a reader monad, a state monad and an eval monad. So this one I don't really use it because I replace it with implementations like monics if you want to because they have actually a much more mature implementation in a sense much more featureful. So we can go through what this actually means one by one. So that's a writer. Writer really is about carrying some information along with your computation. Readers I can tell you you've encountered readers all your life. It's just that you don't know that they're called readers. Later I'll show you. Readers compose operations that depend on some input. The state monad basically allows you to say I have a state at this part of the computation. Once this thing succeeds I want to pass this state along to the next guy to compute based on what I've done in state step number one and that's how it's being propagated. Some people will tell you once you start using this thing they say please avoid using the writer monad because it is not safe. I think there's some merit to that argument. I'll show you sort of an example why it in some use cases is good some it's not so good. But let's familiarize ourselves what we can do with the monad. Again let's have a look. What the hell is this thing? It's nothing more than two graded signs and followed by an equal operator. This is what they call the bind operator. It's really a flat map. Now how did they call this? They didn't invent it. It was invented by Haskell. So now if you look at the whole chronology of events the guys who created Scarla Z were Haskell guys the guys who created cats took a lot of ideas inspired by the ideas from Scarla Z. So a lot of terminology sort of was being passed out it's like a state monad. So what does it say? So this syntax actually follows Haskell if you've done some Haskell you'll know that on the left hand side it's the object or the containers and there's a function and returns me a container. The containers here for this to compile and run this must match this because it's meant to be a monadic computation and the types needs to be needs to be the same. Now this is just a very simplistic view of this whole thing because you actually can chain them together. You can chain a lot of these kind of bind operators together as long as you return me a list. So this is what it basically means I lifted this off from the monad trade in the cats so you will notice that by itself it doesn't actually work. So what it basically means is that on the left hand side when I see a container imagine this is a list of integers you tell me a function I know that oh you want me to convert elements of A into a list of B for example in this case I can tell you how to do it. You will see a lot of these type signatures littered all over cats because these guys are so expert at lifting and abstracting computation you probably will take a while to figure out how these things work but on the right hand side of the equal really it's a flat map this is the same as a flat map so the next time when you go talking with the gurus on the type level gitter channel you'll be like oh you're talking about the fish oh okay you're expert look like expert okay the next thing lifting all the type classes in cats oscarzi on Haskell whatever it is have the ability to lift functions for the outer containers you can actually define something like this you can say this is a function I want to be applied whenever you see a list or something so when you do this what happens is that again when you say monad of list the implicit is being invoked and you say oh okay and you will look at this expression and know that oh it's an int that returns an int I have the current implicit I'll let you pass and then you can actually pass in the list of integers and you return it to 34 in my own experience I find a type classes really helps me to define a lot of reusable code because most of these things it's you know it's it's like what I said at the beginning of the talk it's always a three phases you take something out from storage you do something with it crunch it in memory and then you decide whether to chuck it or do something else it's a function from int to int it shouldn't be a function from mentalistic okay this is the handy syntax they have given you yep so by the way because this one it's actually doing this thing called flat mapping so if you look at the code right actually yeah I know it sounds a bit funny from what you saw in the previous slide oh we have an expert on implicit here actually he's sitting right at the back there's actually a lot of implicit in ball so you will find that when you're programming cats at least if you're using Scala 2.11.8 you need to import Miles Sabin's compiler fix otherwise some of the types don't actually match it's actually a deficiency and the Scala compiler as of the version 2.11.8 but when I try using 2.11.11 it seems to be okay yep so I mean the rest of the world is moving towards 2.12 I assume so should be fine yeah oh good question yeah I I think it's a ah yeah oh so what you can do actually I'll be making this slide available on the meet up side or wherever he wants it to be you can go I'll post some actually there are some links behind where you can learn more about cats per se from this guy this clever guy called Mr Eugene Yocota I don't know whether you know him have you heard of him who was that yeah he's an SPD guy sorry sorry did you have a question okay ah okay so the next thing about this type process is the idea of lifting any ordinary value into a monad now so far if you look at the previous presentations the previous slides it's all about okay you give me something and I'll try to apply it to a list of something but what if I have this thing let's say an integer over there but I want to be part of a monad computation so what should I do now all the type processing cats actually have a similar behavior to Haskell it allows you to lift that means lift a plain value this actually again it's it's from Haskell a poor name in my opinion but it basically says oh I have a number 4 I want to lift it into become a monad so that it can participate in a monad computation you'll find this very common when you're creating your own code like you have like a stupid example but let's say it's a user class a user class it's a plain case class most of the time it's modeled as a case class and you want it to participate in some sort of computation you have to lift it in if you write cats in the flat map style you actually need to do this kind of stuff so that you can lift it in to participate in the computation we'll go through what I mean shortly okay so this is an example of monadic flows now when they say a monad and sort of flow the context they basically mean it can actually flat map so what happens so what is this that first I just gave it a name because I'm not really good with names this is a reader monad a reader monad that always takes in one argument of some type and returns a monad so that's what a reader is now what I'm saying here is that what I'm saying here let me go through this so I want to take an int and I give you this is a repeat repeat basically says that if I see a true give me a list of a list of acts if I see it is if it is true also give me the list of acts as well this is what it means okay so this these two are the same thing I he wasn't my intention to confuse you but they are the same thing now what happens is that when you put it into a monadic computation what it actually does is that when you invoke first and give it a four you will give it a two list but because it's a sorry a list of two values and because it's a flat map it has to go through each element of the list and that's the same thing again you will always see four elements because flat map is always all of edgeware so this is a slightly more complicated example of using monads a monad that is that is called reader monad and you can actually compose them okay so next thing is called writer monad writers are meant to carry not only the information about the computation but sometimes how you compute it okay this is an example I lifted off from Eugene's website it's called herding cats this page I actually can understand some of the other pages I'm so sorry some of them really way beyond me but this page actually was quite illuminating in the sense that what he did was this he wanted to create a computation this is the end part of the computation he wanted to say whenever I have a number I just want to log the number itself writer allows you to do this so how does he do it writer always carry some information and then the value of what is being computed it is a very simple example here but at least what we've done back in what we're doing here is this is everybody familiar with the idea of a correlation ID a correlation ID in the HGP world is an ID to uniquely identify a particular request normally it's embedded into a HGP header and use widely in web context now this is the thing when you're writing code in Scala you'll notice especially when you write code in Aka the logs are always interleaved they're always interleaved I can never understand whether this transaction was coming from that guy or not even if I had the correlation ID I spew it onto the logs I'm still confused so what we basically do for the writers is that we create an idea of a writer monad and we pass this guy around in each step of the computation and we use the writer to accumulate the logs so in the end we don't really need the correlation ID we just spew and voila the transaction is perfectly captured and when it spews out because writes is basically atomic so when you collect them together in the same object and you spew them onto Aka log they will always appear as something like that now why did we do a very stupid thing like this is really because we actually have an elastic search the ELK stack that is reading off the data from these logs and then pushing them off to somewhere so they need to be in this sort of format and then spew in exactly the same order that it was being computed so writer monad can help you to do this now that's really a side story so let's come back to this story now in the log number what he actually did was to illustrate the very idea that the monads can actually be composed so this is the usual your favourite form expression basically it does what it does is actually log number 3, log number 5 and then when you run this is the syntax I don't know why they want to do this you can choose not to do the run you can just invoke run is being called implicitly and you can see that the log is being spewed out so it respects the order of the computation because the order is actually implied here so when you're writing complicated monads writer monads it will ask you to give it a monoid oh, a good catch good catch it is a trick they did to make sure you're paying attention so when you do this what happens the tree gets placed over here the value is always extracted onto the left hand side and the log stays inside this context it doesn't reveal itself you can force it to reveal itself if you want to so this is an example the nice thing I like about writers is that I can actually reset it so by resetting it I basically clear out the logs you'll see this is still the same object mount with log, mount with log so it's kind of nice for certain computations or you can do a swap I don't really use this but it seems to be I'm pretty sure that it's useful for some people's use cases but at least I don't use it the nice thing is that that I like is that you can actually extract the value it's always the right hand side of the tuple here right hand side over here and that's the written that's what they call it the written, you can see it so it's very good like for example later you'll see why this is important when I sort of introduce F monads because I can actually have the same computation that carries the log and I can choose whether to run it with logging or run it without logging just by switching it that's all so we'll come to that later so far is it okay? is it like oh shit I need to get out now okay okay so reader monads you've been seeing this all your life okay reader is it's very simple so let's use this example now normally you have in this example I'm giving you is that there's a conflict class just imagine there's a conflict class you always do the usual thing get a setting get the value etc but I'm always if you look at your own quote you'll be like oh this conflict class for certain people have to behave in this way and that way readers gives you a way to abstract it out in a sense that okay give me a name a meaningful name that's recognized in your application I know that I'm going to get a conflict and you just want the setting I'm going to give it to you this is called a reader monad you've been using this all your life but you know there's another way to write this now the nice thing about reader monads is that it can be composed you notice I'm favoring one style the four comprehension one major reason is because I find that it's very readable when I read it like this because I know that it's always oh okay if I find this and I find this I'll get this so it's always very real from a top to down kind of approach you are free to choose any other style you want but you know okay so far it's okay state monad this seems like a paradox am I trying to trick you or something allows us to pass state information around in a computation a state monad passes state information in a computation let's look at an example of a state monad this is an application I built recently it follows the style that you've written before let's focus on this this is a state monad now a state monad basically says give me a state you do something with it it's a function that takes a state you do something with it return me the new state plus some value that's what the state monad is now in this case what I'm choosing to do is that I just want to do two very simple things I have a config I have a config class which is the same which actually is the type save config if you've used it I'm sure you've used it already and I just want to make some alterations to it and I want to load and store it but I don't want to do anything else a state monad gives me that kind of flexibility to write a very clean code to do that kind of thing now typically what happens when you're starting to code a state monad is that you will normally have some abstraction to say that it's a state so when you need a config state like for example over here I'm initialising the config state by passing something from a previous computation I pass it in and it gets stored then after that I can command it to load and store and as I'm altering now how is this important I was writing a DSL funny thing is that it's a XML DSL what I'm doing is that as I'm processing this domain specific language that's written in DSL I need to say oh when I see there will be a default state for this whole language but when I see certain elements I need to say okay I will ignore or override the state and I need to store it so this abstraction gives me the flexibility to store and load it that means if I see something nested within the XML config file and I see that oh okay you didn't override it I will load whatever I last saw and it will be applied if I saw in the XML, nested XML that you chose to override it I will store because you wanted this new state to be the new one so it's that's how it typically works oh this is very simple regular again you can see that I favor this kind of four comprehension style so what I do is that I extract the groups this is basically a whole language that extracts data off from the XML configuration and manipulating the state so that it can be applied in nested elements etc so if you read I mean I think I write reasonable good English what I normally what I did is I extract everything certain things that I'm looking out for in the XML and this returns me a list of something I go through by doing this notation I iterate through all of these things that I was looking for I call it a group and I load the config this is a nested XML configuration I use this function to load it and to get the config and then from there I configure it and then I load again and I do something else with the state so when you're writing later on when you get more familiar with cats you'll be writing in a very similar style I'm pretty sure and what I want the whole point of this slide is actually to illustrate two things is that the reader actually is much more powerful than what we last saw you can actually do lots of different things with the reader monad because remember the reader always takes in one argument of the type and you return a monad a yield always returns a monad so the general structure is always there then what you do inside you can lift it out into other abstractions anything okay so here's the trick the trick is when you do yield the Scala typechecker make sure that all of the types must conform so you will see this notation to option to option option I'm forcing it I'm forcing it to become a monad I'm lifting it remember I was talking about lifting values plane values and I want to be part of a monadic competition I lift it this is a lot of me to lift it when you're doing cats you will know this any more questions okay okay the two things I like about this is actually the separations of the concerns and basically the state management I I find that this sort of way actually I'm not the first one to actually come up with this idea I basically went to read Timothy Parrott's blog about how to utilize state and combining with algebraic data types he came up with this pattern I was like oh okay that's very useful so yeah okay so far everybody still with me or are you like god this is hot okay okay so the next thing is actually called an applicative applicatives actually I did quite understand it at first but basically it's like just now you learnt about functors right you learnt about functors functors basically allows you to map a function over something a container of something applicatives allows you to add function application but allows you to expand the scope which you apply the function which means I can do list of list of function to something functors can't really do that let's look at example in functional programming terminology they always tell you an applicative is actually a functor they always tell you applicative is a functor hopefully you can see why if you flip back to if you can recall right back in the previous slide I actually replace this guy with functor it was actually doing the same thing applicatives are also doing the same thing now this is a slightly more elaborate example that tells you that actually I can apply a function to a list of something a list and list of something so applicatives are good for that when you have two layers functors one layer so for me that was the easiest thing that I could understand when I was going through CAD, SCALOSY and etc now this example over here basically it's actually using one of this thing called the nested function nested datatype in CADs right you don't really have to write this kind of expression using an applicative you can if you want but in CADs they have this thing called the nested the nested structure is basically the same it's a nested functor it's a functor the functor and then you can functor and the functor because sometimes you actually have very complex abstractions built in inside something that can be mapped over in that sense so we have a list of gains and a list of functions or gains to do gains but in this example how to release an element or not date to one element so sorry, come again in this example all the list of one element long what happens if the list are longer it's okay no, it doesn't permittate so when you have so far it's for you are talking about this function here is that what you are referring to? the second list for the functions contains two functions oh the second list oh, then you have to lift it up so you have to so this this will represent the list of the function then you have to lift it up again you can map because functions are actually functors you can map again it depends on how complex this structure is yeah so the one question actually to follow up when it's question the one question I discover is that how come you know those guys only develop until two contexts that means the depth is always two turns out actually there was a logical reason all the applicatives somehow can be reduced and this is the after flattening all the applicatives this seems to be the common pattern that means you don't need to account for the third list of list of list of something because after you flatten the maximum it can go to is always these two contexts so maybe that's why they didn't expand further and say applicative or applicative or applicatives sorry ah yes okay a lot of people use it for different various reasons at least the ones that I've used applicatives for is actually to do validate configuration so let me show you an example okay okay so now there was a purpose to of me putting this slide in but then I realized it was a mistake so I'm gonna skip this slide okay let's jump to this some of my ex-colleagues probably have seen this code before so this is an example of how we use applicatives to validate fields in the configuration let me sort of give you a background we basically programing using the ACA so ACA uses the application.conf is written in a whole con syntax so it's like it looks like a JSON kind of structure we actually have some configuration embedded it's our own custom configuration but I didn't want to use back then typesaves stuff because I was like oh you know I know about applicatives so I might as well use the concept and try it out so having said that basically embedded this is an object that I used to represent something a sort of business domain object in the application now for this thing to create this kind of config I need to make sure the name of the cast is actually string the port needs to be an int string and string and so on and so forth they are very basic stuff so what we've done is basically we have this the usual stuff it's basically just a regular skala object of any name and I want to say get the half config from the typesave config that means I read it off you just tell me you just tell me whether you know is it is it correct or not and what I do what I do is that I give it I give it two types first is collect all the errors for me and if there are no errors give me the the end value the end value actually is the half config itself so I don't want you to be too bothered by the syntax over here but let's try to grasp what was I trying to do I was trying to say go read the configuration inside the application.com lift out all these keys for me and validate that they are strings so there's a bunch of code that is missing over here which I cannot show you but they're pretty boilerplate but what this basically means is that lift this key from the application.com make sure that it's a string because I say it is a string I already explicitly say it's a string and pass it for me there's an object somewhere which I have hit it that passes this thing to make sure that it's a string by checking for it is it A to Z it's a regular expression A to Z capital A to Z that kind of stuff and this is what I do now when this thing is being validated basically when I call this function over here to validated now it basically connects all the errors and all the all the successes and say and what happens is that I collect all these things together and I basically map it out if you see if this configuration is correct you will collect all these things it will tuple for me and I create my case class this is what it is essentially doing don't bother about don't have to be true bother about what does this actually do this basically says that I want to apply five computations to it using applicatives because five here if you indicate let's say six or four etc there will be a compile time error you will make sure that when you say that there are actually five applicatives inside I will make sure that there are five over here so that's how the validation is being done so they are very good in doing this kind of stuff so far any questions on this snippet is it like wow shit what the hell is this what did I get myself into sorry were it all in this one or a compile one that's a good question so what happens is that apply is a base trait which all the applicatives inherit from so what happens is that for the apply what happens is that this thing invokes implicit it invokes implicit for validated now it basically says this is the validated non-empty list it basically says apply all these functions collect all the errors for me and this is the end result this semi-colon refers sorry this question mark refers to this this is what basically means yeah okay I won't go into this what is being mapped so what happens is that in applicatives what they basically do is that they apply this particular function so this function will give it either a left or a right as an either type then to invoking to validated now it will say oh it's a left it's an error it's a right I'll let it pass but these guys at least on theory applicatives have the ability to be parallel so all these things these 5 expressions over here are potentially parallel theoretically it is not in the implementation and not the mapping function yes yes yes yes yes so I should say this is the function because you did the passing function has been hidden so you can see it over here okay it returns either if I remember correctly if I remember correctly it's been a few months sorry sorry come again yes it will return basically it's a validated now and inside inside the map 5 it actually evaluates the 5 validated now and sees whether to return the tuples itself so I should say I actually made a quantum leap here because there's a concept in cats called the Cartesian the Cartesian is basically cross product which the apply is based on so it's like a cross product and it's what happens is it treats this thing like a cross product and it's performing all these computations and then lifting out the values for you I think we have I can't remember who was the guy who created the first validated library but I know that it was a recent invention it wasn't like 60 years ago I can't remember function is almost the same validate things in parallel and then you just go and solve the result if all passes you get the actual form otherwise you get a list of parameters okay so this allows me to quickly find out like for example if I enter a wrong type let's say for cluster name I gave it a number it says oh it's not it's not a valid string or something like that okay so that's the validation oh okay now we come to monad transformers now what are monad transformers just now all the monads actually have a interesting use case is that they are all applied to one particular type of monad that means it's either all of these computations return me an option like this like this option option option option or something else like a list a list a list if you can imagine monad transformers is to allow you to combine different things together let me go through okay monad transformers okay it's going to be a tough one so let's say normally I try to look at the problem and see how it can be applied and try to derive some understanding from that so if we look at so let's say we have a problem statement we say that we have a cat we'll find out whether it's alive either it's alive or it's not alive but in order to find out whether the cat is alive I need to determine whether I can actually find the cat okay so there's this case class represents a cat and then I have a very simple function that says is it alive so ah crap caught me there was a typo this is supposed to be a cat so if there was a cat if it is alive then you return a right and I will say it's a two option that means it's some cat that is alive if not I will say it's dead then I will say you go to the left because it's dead and then this because try an exception always return you a none so it will be a list of nothing so now when you evaluate when you do a look up so this cat is very simple it's just basically says look up a cat and this cat is always alive and it will always return me some cat in a list this is regular Scala syntax and then this is how it normally looks like if you don't use a monar transformer again you notice I favor the four comprehension style I will do something at this I have to do something at this look up give me a cat and I can't compute it on the same expression I have to go inside the yield and then do this now when you have a lot of these different types of functions it becomes a headache because this yield will keep nesting you can imagine it keeps nesting so the monar transformers what it does for you is that you just need to tell me what is the sort of the hole I am going to deal with and I will help you deal with that so what it does is here the only difference between the previous code and this one is that this is an option transformer this one is slightly more of a problematic if you haven't done cats enough in a sense what this type basically means this refers to the type here that means it's looking out for the most inner type while the outer type is left unknown the reason for as far as I know the reason for the existence of monar transformers is because monars in general cannot compose so when I try to nest them they can't compose because there's one layer the outer layer which is always unknown I don't know whether I'm getting a list of something or is it a list of something I never know so what they did so they call this thing the hole they took out this whole thing this hole and replace it with a generic type parameter and they operate based on the inside in this case in this expression the inside is actually a sum so it will take a while so your head is going to hurt so you have to say oh I know the most inner the type of the most inner expression it's going to be a sum and I want to operate on those values you have to give me an option transformer using this kind of construction you need to know the pattern you can write it like this the yield is no longer nested you basically lift everything up into the same four comprehension right? okay I don't like to do it that way so I found another way it's called the F-monar F-monar that's the link over there I know Eric Torbo is one of the key contributors to the project it is an alternative to the monar transformers okay what do I mean by alternative? before I go over there so previously all these transformers what they basically did in the F-monar is that they lift out all this common stuff the either, the option future eval what else there were a few others they lift everything out and make it into a library so that you can write code like this effectively like this it's called the F-monar okay so that's why it's called an alternative to the F-monar so I'm going to give you an example maybe you remember this code so our old project used to have this idea of a workflow descriptor we wrote we basically before the workflow needs to start we need to persist the descriptor of a workflow we tell you what's the workflow idea where to go get the data types etc but that's not really the main story what we did was that we persisted this thing and then we need to lift it up from storage and then pass the whole descriptor so that it becomes readable now in F-monar you can deal with IO you can read data off from storage etc and then compose them together so what you need to do is basically you need to have all this regular this particular import over here and I'm using the success JSON parser so this is the most important thing you need to focus is these types over here these represents what are the potential kind of errors I will face when I'm reading descriptor off from the disk this represents whether I can read read basically the descriptor itself and whether this basically represents the logging that means when I'm reading I want to lock each step of my computation itself and because the descriptor is in an adjacent format I need to have a passing and a coding failure to represent this kind of things so how do they piece together this is the F monar what it does let's walk through step by step first of all I know what are the sequences what are the steps I need to do first of all I need to go find out where this descriptor is on the disk I need to read I need to attempt to read it and then after I reading it I need to attempt to pass it and then after I pass it I will have to construct it into a workflow basically it's a case class basically so what I need to do I want to model this whole thing into again my favourite for comprehension because it's basically flat mapping so what I do first of all you need to describe what is the type of this effects all the types you've seen in the previous slide refers to this this is a notation basically a type notation which lives in the implicit the say is this computation here is expecting 6 kinds of effects 6 kinds of different type of monads and I basically list everything down over here the next thing is I go and describe my computation what I want to do I say that the purpose of this function is to load the descriptor I will give I need to enter a descriptor and I will return you a workflow so over here this refers to that means I ask this function once you created once you created you can actually run it so if I say the descriptor is a number called 1 maybe we saw it as just the number 1 for example with a suffix called 1.txt for example you can think of it like this so when you construct this computation and then I run it I know that 1 becomes the value of this ask itself because I know that it's a string this is a string so what I'm saying is that go read the workflow descriptor called number 1 on the disk and then tell is actually a function inside the writer monad itself so what you're saying is that oh you can notice that I'm actually giving a timestamp of when I'm reading it so the next thing is basically wow can I load the contents I'm trying to do an IO and load it off if I the load contents is a function returns a left or a right and you have this thing called either effect so that's in F monad so all these functions are being written for you already pre-packaged and you can construct the logic the logic basically represents the idea of asking the workflow descriptor giving this idea I do logging at each step and I try to load and I try to pass for example once I finish loading because it's a monad you will only reach this step that means the next step if this one is valid contents then I start to pass and then I say that I'm passing and then I try to decode so this kind of F monad basically the library itself helps you to write to express your launching as clearly as you possibly can with a minimum of I don't know headaches in a sense what? yes so self basically always tell if something goes if everything goes okay right? if something goes wrong what's written in the log what would be written in the log I mean how would you do how would you log error condition error messages so if I can remember correctly so if let's say at a particular step that the logging I mean the previous operation fails you can actually use this kind of expression to lift out what was the last thing that you saw by doing this and called run writer okay successful message yourself until the last successful so it's very convenient for me in a sense it's convenient I remember when I was at Scala Masri Powell was saying one thing which sort of lodged him up into my head he said when he joined a particular company the only thing that the team lead gave him the only task he was being given was how to write read a file with the data inside he basically said just focus on that problem so he actually he saw the problem using F monad but he wasn't very satisfied so he went about writing this thing for recursion scheme and then he talked about recursion scheme about that was what got me interested but you know so to be able to write like this you basically it's like falling back to what Powell was talking about that means breaking down the logic focusing on each step of the particular task reading it how can you like you know how can you assume that the disk you know is readable you know it may be a network connected disk for example so yeah so F monad allows you to do really nice stuff in a sense because it's a very nice monad transformer that's written in a very simple syntax and most of it is borrowed from cats itself yep the key thing to remember about F monad is it allows you to stack stack all your computations okay okay so we are nearing the end of the talk which I don't know whether did I have enough time or not but here are some good learning resources this is the Gitter channel yep so yeah interesting fellas over there so this is Runa's blog so Runa is is one of the co-authors for this book called Functional Programming in Scala very smart guy tight level I advise you to go read up this one this is basically Eugene Yokota's links to herding cats he has a similar website called herding ScalaZ if you want to learn about ScalaZ the format between the two is really similar so you know yeah and last one the top one is actually on monads itself I find this to be pretty helpful I'm not asking you to go learn Haskell itself but I find it has a lot of benefits by understanding how Haskell function because the guys who wrote ScalaZ and cats mostly were from Haskell so they borrowed a lot of concepts from there okay that's it from me any questions the what? the matrix the matrix the matrix you know what type of cats if they were ScalaZ and if I were to start a new project why should I just want to kill them oh okay hmm I find that no type level it's basically a non-profit organisation the guys are generally very friendly so I probably if you want to do a lot of the hardcore stuff in functional programming which I'm not because I'm just trying to you know churn code in a nicer way so you can actually try learning ScalaZ itself and guys actually push ScalaZ to the edge whatever the theory is out last year from ICFP you probably can find an implementation inside ScalaZ so if you want to go for really cutting edge probably ScalaZ it's a good thing but so if you want to do like cats actually I think it's a good community because the ecosystem is expanding there's this guy called Daniel Spelak Daniel Spelak decided to bring in about this thing called IO but it's basically trying to model the IO component that means like for example performing asynchronous computations reading data of persistent storage from Hadoop from big data stores etc there's a lot of projects under the type level that are participating in this effort one if you're interested like Rob Norris Rob Norris is a very nice guy so he wrote this thing called Doobie so if you're into SQL you want to find a replacement for Slick for example if you hate Slick you can try out Doobie maybe Doobie works for you so if you ask me I said the same thing to my manager ScalaZ is now maintained by only one guy Kenji so Kenji Yoshida I mean he's a great guy but he's really just one guy and a lot of the efforts from the open source is moving towards the cats direction simply because there's a much larger community over there they sort of swing over I don't think they're pushing people actively oh so you read the mailing list the direction was to on the cats that's not a term so I mean I would say I used to ScalaZ for like the last 4 years so I went to the library and when I look at cats it's basically you know like a lot of people are saying but even that the police made towards cats I mean like we discussed whether we should ScalaZ or cats if maybe we should just cats mainly because we know that there's active development and I think for you guys to basically start or start that stuff I don't think he's contributing to ScalaZ you probably know Paul Chusano now is off doing his own stuff consultancy Runa started Tech T is a high school house with Gabriel Gonzalez these two high school gods you know they're like somewhere there but never mind these guys are really really nice guys so basically it's like echoing your point the main guys have moved off Tony not sure there was a big hoa about I don't want to get into that but there was a big hoa about ScalaZ yeah yes cats just got the community is nice now they crazy cold and also it's got a nice reputation so I think so in terms of what's there but I don't think that's so different so if you ask him whether it's more functional or not I don't think that's okay at least it's more the case there I think it became too much to do ScalaZ to do ScalaZ development because people are so willing to put in the community and people it's probably easier to come and want to start new year but that seems to be kind of big they are also the conclusion of just cats because it's a little they are doing something to compile it it's the things and they compile it and function of programming or whatever yeah I think my son as my son he did one thing and so this is we you know basically the problem that you give is trying to compile the problem the problem is that you have you have to use compile now to especially you know to kind of to kind of to kind of to kind of to kind of to kind of to kind of as they did I think I also actually say now just to look at what you compile but you have to that kind of that which is really the last list of yeah I think I think there's a lot of I think ideally that should export to the scale of compiler rather than having to use to kind of to kind of to kind of to kind of to kind of to kind of to kind of to kind of to kind of is that all the things that they put into the formula fone point one into the main scan I lied but but for the saving was that was not much you said there you would have some issues that norm u that we recorded at the time you wrote that is B. Yeah so Mao jang came out with the fixed to sort of unify the times to a certain extent by compiler plug in so it's i think it was necessary for 2.11.8 jika anda mahu gunakan cat dengan versi skala tetapi, saya tidak tahu saya perlu gunakan dari kata-kata ia okey dia menggabungkan DOTI Just to give you an example, Dottie has this idea of a union type, you can say A or B, literally using a pipe in Scala, you can't do that, I spent like a week writing my own algebra to emulate this thing, I was like, oh my god. So, I'm looking forward to Dottie actually, to solving the problem with me. Ya, but, ya. Hey, any other questions? No, okay, cool.