 So I get started right so today we're going to be talking about building a restful API with Scala Z. So how many of you know what Scala Z is one. All right, so so I'm Yashun Kumar platform engineer Megan systems. So we are a startup. We are a cloud startup from Chennai and so we are like such a past platform where you could launch applications. Services and you bind them together something like Heroku but we also do the monitor them and you have countries integration all this stuff right. So the agenda is pretty simple. So we will look into Scala Z library quick intro and Scala Z library and then like what it is and how to use it and then rest API the gateway architecture of the API server. Okay, so this API server. So say R is a cloud platform. So we have an API and API server that takes that does the full heavy lifting there. Okay, so it's a loosely coupled architecture. So we have this API which does pretty much everything from the rails on top. We have this API and it's connected to you know a message broker and then a pass engine and stuff. So this does pretty much all the work. So we got so that is written with Scala play and we've used Scala Z library to work with and they also show you some real time use case Scala Z like basically the code of how we wrote it and stuff. So why functional? Okay, so why functional? Right. So we know software is getting complex hasn't has gets really complex. You know you want the software to be well structured. Right. So and then well we all know that immutability. So we all know that there are no assignment statements in when you're writing functional code. Right. So you don't. So it's immutable. Right. And fine side effects. So we all know functional programming is all about not having any side effects. So what do you mean by side effects? So say says a function. Okay. It takes in a type A. It gives a result B. That's it. Right. So it doesn't do anything else. It's that state straightforward. It takes a type A type B. That's it. So in. So what happens is that you won't have any side effects. Okay. And there are no order of execution. So it's irrelevant. I mean you could do which order you want basically that particular function is going to one thing. Right. That's it. It's pretty elementary stuff. I mean anyway. So pure functions. You all know what pure functions is right. Something that has no side effects. Something that has immutable. And something. Yeah. Something is well structured. So that's that's pure function. Like I said A. And all this gives you concise code. Like I said if it's well structured. The code can be concise. And if the code is concise. It increases readability. So any programmer who comes up. So you know functions are so small like your controller. Whatever you write. It's going to be like few lines of code. And well first when you look at the code it's going to be a little hard to understand. But once you know sort of know how functional programming and everything works. This is the real time. This is the actual experience I had. So once you figure out how it all works. It's going to be so simple for you to work on top of it. Okay. So today. Oh yeah. Because it's fun. It's a new paradigm. Right. I mean. I wouldn't I wouldn't say functional programming is better or anything like that. It's just purpose driven. So you want to build something for that particular purpose you pick that and you choose and you do it. And you see what that you always have trade off. So something does something better than nothing. So you pick for whatever you know it's whichever use case is suitable for. Okay. So. Okay. I think I messed up the slide thingy because I was doing a lot of change in the last minute. All right. So. Some function fp with Scala Scala people know what it is. Okay. So quick introduction Scala. How many of you code in Scala. Great. Okay. So Scala is an object oriented language. It also supports functional programming. Right. And then it compiles to JVM. You all know that. Right. So but why Scala is suitable to write pure functional code. Right. Why Scala is suitable for that matter is it has first class suppose first class function. Fine. Immutable collection library. Right. Supports pattern matching now every language supports pattern matching like they like across everything just I don't know they have pattern matching now. So pattern matching. Right. So Scala is like the perfect language to but the problem is when you start if you want to write function like you take Askel it's easy to write because you have to be pure. You don't have a choice. But with Scala that's not the case you can actually sort of you want to write pure you want to write pure functional way you can't you might just abuse it and then you know start writing in a different way. So so that's where Scala see comes in Scala see helps you code in pure functional way. Right. So let's see what a map is. So a map is basically say your function which given an end it returns a list of whatever increment one. Okay. So you create a list which has one comma two list over there and you map it apply the function you get a list of list two list three. Right. You know you all know what a map is. Right. Fine. So now flat map. It's pretty similar. So what flat map does is you take a list of one comma two and then you you apply it. It flattens the inner type list of list two comma list three would become this two comma three. You follow. Right. So this is the basic introduction of of functional like how Scala supports functional programming. Right. So we'll move on. Yeah. This I don't know. I mean a lot of people actually sort of whenever I tell them to pick up Scala see or and try to you know help them. Yeah. Okay. It's like you could sort of okay you could put the function in a value and you could carry it over anywhere in the place as a value. Right. So it's like see I don't know see I'm not an expert but how I see it how I see functional programming is that just a personal. Is that you could sort of do put everything in a value and carried over carried everywhere. Right. So you could. So everything is basically a value. Right. So it's a lot easier for you to in terms of error handling in terms of you know passing functions. Right. It helps. It helps a lot. So yeah. So Scala see is a library to write functional code. It's actually an extension from the core library to write functional code and it's not hard. I mean if you if you get into all monads and all that stuff. I mean if you theoretically you could. I mean a lot of people keep telling you know honestly I don't know but how I see it is that I just look at the library and see you know. It's quite a type class. How can you possibly use this in the code and try to solve a particular problem. Maybe maybe you know it's not a good process. Maybe tomorrow day after I would probably learn how it actually works. And does not require super human power. So. So yeah type classes. Right. I mean we just start with type classes. So what is type class. So. It's right there. So type class a. Okay. It's not a class. Like how in the programming. So type class a defines some behavior in a form of operations. Okay. Which is supported by type class any type T. Okay. Then. Type T is a member of type class A. We look at examples now. I mean it's it's. If you can be using but like an example. So. This is like the hello world of Scala Z. I mean like. Talk about. Say you have. So you fire up repel and then you type. A string any string. Equal to operator to one. It's going to compile and it's going to give you. It's going to say I mean it's going to come back. It's going to give you bull balls. Right. But that shouldn't. That shouldn't happen. So but in Scala Z. Import Scala. Okay. Scala Z. It's actually going to give you a compilation. Okay. Note that type needs to be the same. Right. Fine. So this is like simple example of order type class. So this is. Another. Type class. So. This is the way we are. Scala Z has a lot of weird symbols like for. Either or they have this back. And for order they have this question mark. Question mark. Right. So what this basically is pretty. It's pretty simple. So you put three. That two. Right. So it's going to. It supports like greater than less than. Max men and all that stuff. So three. So it's going to actually. Compile. And give you gt. Because it's greater than. But if you change the type you give a string. So you put. Three. Two. No. Type. So type needs to be. Right. The triple equal to is basically the equal operator in Scala Z. It's another way of. But yeah but. But you can actually use that. Say for instance you want to check an equal to. Like see how do I. These are. No. You do. See. Okay. That's. The thing is you put triple equal to and you actually do. So say when you are you write an equality right and you actually check. Right. Whether and it's going to throw an error at compile time itself. You follow. I think I didn't put enough code. So show. Show type class. It's just. Well so a new thread of show is actually going to. Throw an error. You put an implicit value show shows of thread and get name and now you put new thread of show is actually going to show the name. So these are just small simple type classes which you can use on your code. So the reason the point I'm trying to make here is. Adding type safety becomes a lot. I mean type you get your code becomes type safe. That's the whole point of. The whole you know the examples is it becomes a lot it becomes type safe. So what happens is you figure out the bugs in the compile time itself which is great. Right. That's something great. So let's get into lens. So we talk about. Okay. We all know getter and setter. Right. Yeah. Okay. So we talk about lens. Well getter is fine but setter. I mean everything is immutable here. You can't set something. I mean nothing much is basically you create another value. That's it. So you write a lens. It pretty much sort of makes another creates another value creates another object in such another object and creates another value. So it puts it puts up another value. That's about it. Right. But as you can see here. So that's a case class playlist takes artists and ranking. So we create Paul and Ringo over there and then you actually create a rate lens. Okay. A rate lens. That's a lens. So lens.lensu of you give a base type and a feel feel type. Okay. So that's basically playlist and end. And you basically copy use a copy and then you change it. So what happens is when you use it over here increment set rate set of that setter it makes it basically creates and stores in the value. That's about it. Right. So that's another weird operator in. Yeah. So that basically increments by one. Okay. This might look simple. This might you might just ask what is the big deal? You just you know instantiate and then you just own a value. What's the big deal? But it helps to compose multiple lenses. I'll show you how. So if you have two lenses address lens and a and a street lens and you also you also have a person street lens. Right. Lens of person or that that's actually lens. Okay. I've not put the code much smaller way. So and then you compose this street lens compose others lens. Now you can actually apply get and set it. You see it. People street L get person actually get it. Person street L said you follow. So say you have say you have a nested data structure and you have a key. Right. You can take it out. I mean it's it gets clumsy. But with lenses you could compose and take it out. Like you have a so you have a map of map. Right. So you have a key. You can sort of take the like you can use that lens and other lens inside compose together and then take a particular value for key. Right. So. Yeah. So that's basically by national transformation. So you make transformations. That's basically it. Yeah. So a neat way of updating deep in nested data structure. Right. So it doesn't get clumsy. You can get set update change using multiple lenses. Right. So validation. Okay. Let me just. So validation. If I don't talk about validation. I mean I think that's like the most. Most used when it comes to scala. As far as I know. So it's pretty good. It's pretty simple. Also say you have object. Sometimes the extension essential. Now what we are basically doing here is by the way that's a far comprehension. I'm sure all the scala people know what. Okay. So you basically check food, check booze, gasoline and everything is fine. You yield. Right. Okay. Now how do you. The basically validation is left and right. So the left is the failure and the right is a success. Okay. You could like left map, flat map, take the value out. So right. Okay. Now say you know all three is fine. All three things. All three are there checking everything passes. It's going to heal. Fine. You're good to go. Say check food. There is a problem. It throws an error. Okay. Now. Okay. It's fine. Check booze is fine. Now gasoline. Not there. It's going to throw an error and it's going to fail. It's going to exit right there. Okay. But a problem with just validation. Now say you have two failures. Check booze or check gasoline and say and the gasoline or say there is gasoline, but it's less than particular amount or there are two failure basically happening to fail is happening in that particular function. But what happens is when the first one. So when the first failure comes, it fails, fails far. Okay. But what do you what if you want to aggregate all the failures and give it to the user? I mean you don't want to throw exceptions. You want to aggregate all the failures and give it to the user. Right. So use something called validation. So it's a non empty list. Okay. So validation is pretty much the same thing. But only thing is you put them in a non empty list. Link the linked list to aggregate all the errors. Okay. So an applicative builder. How do we use an applicative builder? Okay. So that's it's pretty it's pretty similar to well when you're using applicability is pretty similar to sort of for comprehension. Right. What you can do is say over here, we can actually change that and write it this way. Pack food dot to additional to additional is actually help a method which converts it converts it and sends it as a no empty list. That's it. So what happens here is it will return a non into list of no food. Let's say you could use that applicative builder and you know build it. So that's validation. Now we look into an actual example like how validation is used on the on the server. So let's move to the overall architecture. So it's pretty simple. We have a front end, like I said, and we have a Ruby wrapper and then API and then that's our API gateway server. Right. We use react and use snowflake ID that creates you ID generates time for the time stamp. It generates a unique ID to and stores it and you know. So yeah. And how we're all started. It all started actually when we wrote. Yeah. So when we wrote, so when you want to write an API, right? So we just chose play and so scala. We were never into scala. We never thought of adding a scala Z. But we were moving to react. No SQL database. And when we actually use Scaliac, Scaliac is a library by the stack stack mob guys. And it was pretty good. And that's when we were forced to sort of use Scaliac library and we actually liked it. We implemented a layer on top of it. A library on top of Scaliac, which actually talks to our code. And that's when we actually start using Scala Z. Actually, I, you know, it's actually called Scala Z. But I like to call it Scala Z. Fine. So let's talk about the gateway server. Okay. So what happens? So, you know, from the front end, you have an HMAC. Right. And what do you do is you check before even before it gets into the controller, you check whether, you know, that it matches with the one in the database. And then it allows the person, whatever. So we play to use this stackable controller. And we were just, you know, trying to build. Remember, we were not using Scala Z here yet. Okay. So we were like using a lot of trades. And then we decided to remember this is like the first entry. This is like the first, you know, whenever you basically the controller, right? So you call the controller. Now we removed all, we were able to remove all the trades and actually use stack action. Now what happens? What stack action basically does is that you could, what? So there's something before. Okay. There's something called, we have a transport layer. That's basically a funnel request and a funnel response. Okay. Are you lost? Sure. Okay. Basically a funnel request and a funnel response. Okay. So what happens? So anything that comes in, it gets funneled. It gets funneled into, you know, whatever data you feed into it gets funneled and, you know, put in the proper, so that it goes on to the rest of our API from the controller to the model to the database. Okay. So stack action basically checks. It checks whether that user is, it's an art, right? It checks whether the user is right or wrong or whatever. It authenticates them and then the process goes for it. So that's just a simple, and then you have to call the model. So this is what happens when you do, when you do all the action. So we've written, we've overwritten a method and what happens is the request comes in. Okay. We have an implicit function which actually builds a funnel request builder, right? And then it calls this authenticated function and this is where we use validation L, right? We would have used validation L everywhere, pretty much. So everywhere, anywhere we call, we get validation L off. So we get a left map and a flat map. We either get a failure or a success. Okay. And you can see request funneled since it's a funnel request funneled and if it's success, it does checks in, you know, and if it's a failure, it just throws a validation of failure off the string. Okay. So, yeah, so let's look into how we've used it like a more full. Hold on. Yeah. Okay. So that's, that's how you, so account of result, that's a model right there. So account of result is basically a case class. And so that's, that's the account model, right? So you get an email, you get an input and it turns the validation of the global option of event result. Okay. So event result is a case class. This is the event data. And because they call gunny sack. Okay. So gunny sack is basically another function. What it does is that it takes the data. It, it marshaled and marshaled the serialization. It takes the data as the UID or whatever and then gives a gunny sack, a gunny sack type. Okay. As you can see there, MK gunny sack or whatever you left map and he checked whether you're getting any error and you get an error. You actually throw serves unavailable error. Okay. And, and to addition, you send back to addition. And if you don't, you actually flat map and take the option of gunny sack on the line, line four, you follow. Right. Now you actually have a gunny sack object and you actually store it on the database. Rear dot stores is that get once you store you again check, you actually do a left map and check whether you're getting any error and you flat map and you match and you extract to events results. You follow? So that extract. Okay. That extract over there. What happens? It's basically serialization. You know what it does is that it takes and it parses it. You know writer, we have a reader and a writer method. Right. But one good thing here is that you have to get a valid scheme. So you can't just store junk into your database. Okay. So yeah, if it's some, you get some and you send it. Some. Successful. So you basically turn. So the extract event result. Some. Change it into an option. Successful. You actually send it to the right side. Right. Okay. So this is the gunny sack we were talking about. So, so when you get the model, right? So that the input, the data is given to the gunny sack over here because the email, the input again, it throws like we said, we did a flat map over there. Right. Guess the data. As you can see, Val of events input validation L, it actually parsed the input extracts it to events input. Okay. So now you're getting a data, you're getting a bunch of data and you're actually extracting it to a case class. Okay. Use a for comprehension to say you do a chain of computation and you get the UI are the, the ID and then you yielded, you set an index for the, for the react for the database and then you create another, you create a case class, case class and then you do JSON. You call the two JSON. Right. So what happens when you call two JSON and then you set it back. So over there, like we saw previously, we take the gunny sack. So left map, if it throws an error, fine. You throw it. I mean, you're going to, you're going to tell the user. If it doesn't, you're going to go ahead storing it because you have a perfectly built up gunny sack object. Right. We follow. Right. So that basically you can see the fourth line to flat map. We get an option of gunny sack. That's what is the, the one which is returned here over there. All right. So, so yeah. So what does two JSON do? Talk about JSON civilization. That's the, that's the impulse we do. That's the Scala Z. So we have a case class called events results and the two JSON function there. Right. So if you see we have an event result civilization that invokes that and then you have an implicit overwrite writer and a reader. Okay. So as you can see the reader, it gets an event results. It converts. Actually, I think is that it would go along. I mean like Val ID, ID and whatever data you give, right? You could keep adding to it and just put it, made it smaller. And event results and it changes J object, J object, J field and it checks. Okay. We use the lift Scala Z library, the framework that's there. I mean we just use that library and then a reader is basically you get a J value. You use, you use applicative builders there and then you've created a new event result. Right. So this is basically marshaling and marshaling. Right. Yeah, they do but well, what we do here is we make sure the schema is validated. Okay. Yeah. So, see, the thing is we are protected by ID key. Right. So it checks when you, when you're converting it, when you get event result, it actually checks ID key to JSON, whatever. So you put in a J, J field and you actually check whether you know it's getting. So you can't put junk value into database. That's about it. Well, you could, you could do it. Yeah. That's, it's possible but well, it, it, the thing is it's, it's easy to also build on, build on it and which is far easier to sort of do it. So the either, so yeah, it's pretty, you know, you know what an either in Scala is right. So you could take a left and a right, left and a right. That's a simple example there. But in Scala Z it has that backslash, forward slash symbol for either and it supports and it implements is, is left is right swap. Get all, get all is what we used before. It basically gets the, it flatmaps and or else what do you need to do? Get the right part or else what do you need to do? Success or what do you need to do? See that. And this is, okay. So this is, so we, this is actually like a far attention, but we use sort of either T and we, you know, like what we do there is we get accounts of find by email, we get the email and then we, you know, calculate the HMAC. The one we saw there, the basic of the full authentication of how it happens, right? The authenticated actually calls this. The thing is I put in the last because it has a far comprehension. It's pretty. So yeah, so the character HMAC it checks and if the authorization is successful, that's some changing the option, right? Now I'm close to throwable. Okay. And if it's not successful, it's going to throw in authenticated error. Authentication error. That's about it. So, so yeah, I mean, you can shoot me a lot of questions, but yeah, I'm still learning. I'm still learning, like, you know, still looking up, like I'm not even implemented lenses, still figuring out, figuring, you know, ways to use it. So that's about it. I mean, right? No questions? All right. Thank you.