 Alright, so this talk is titled JS you can reason about, so this phrase reason is a very important thing when we program, when you write a piece of code, you should be able to reason about it. Let's say I write a function called add or a very simple function called add and I actually do multiplication inside and let's say you know what, I am able to like run it for, I mean I asked my developers to do it and after some time I come and integrate and find that you know instead of passing one and one I get one, two back I get one back and then like so the fact about reasonability is if something works, it works as expected. That is what I mean by reason here. So we will see how pure script makes Javascript reasonable, reasonable in a very broad way. Okay, so let's check your Javascript skills, can anyone tell what is the output of this? 10, alright, yeah nothing, no that's it, this one, yeah, yeah, alright so now copy this and run it on our console, are you able to see this? When I said, oh okay I don't know how to like, okay, right, so when you said parse 10, it did one thing when you run it over a list of 10 repeated four times, this is what you get. So this is what I am saying, it's not being reasonable, you say one thing and later when you do something else, you're like kind of like saying no it's 10 and something else, right? So yeah, so coming back to what where we were, alright so then yeah we have all this, I don't know how to like, so this is like the falseness of this thing, this is like the very debatable thing, really this thing, whatever and it's all like summarized in this one, this picture kind of summarizes it, right? So have you guys have you, I mean everyone in their lives would have faced this, right? So, but the thing is Javascript is actually a good language, the base, I mean like today I don't have to like build an app, I don't have PowerPoint, this is like Linux, I don't have any other way of presenting, right? I mean the web is a very good place to be, Javascript is a very good language and it's a platform itself, but coding in Javascript is a problem. So that's where like all these compiled to Javascript languages came about and one of them is PureScript, so PureScript is a pure functional language, so when I say pure, it is able to separate the side effects from the parts of the code which will do a side effect versus the parts of the code which will not do a side effect. That's what I say it's pure, so that's where the pure script comes from. It is inspired by Haskell, people know about it, like it's another pure functional language that's a lazily evaluated language, when I say lazily, it's like it won't even evaluate some parts of the code, if you like write 100 lines of code and like finally not use and do it, you kind of like short circuit and say hey, everything's fine, but that's, this is not that, I mean like pure script actually behaves like Javascript. Compile to Javascript has easy Javascript foreign function interface. So that's it, so that's about pure script. Okay, now there are a lot of JS compiled to JS languages today. Just take two, closure script. Closure script is not static, it's closure, it's list, it gives a lot of flexibility but it actually doesn't avoid a lot of bugs, so types can actually avoid a whole class of bugs. We'll see it later during the talk as to how it can avoid some bugs. Pure script is pure, when I say it's pure, yeah, when you are doing a side effect, when you're saying like I'm going to mutate this global variable, you just cannot have a function and then call it, I mean, the name itself, the type signature itself will like kind of like shout saying that this is a very impure function and the way you use impure functions is different. So we'll see about typescript versus pure script. So this is from real code on GitHub. This is the JavaScript version of it, this is the TypeScript version of it. So this is TypeScript. So sometimes you can get more verbose than JavaScript itself. And it's actually, I mean, why do we need to write, I mean, I'm not a big expert on TypeScript itself. I've not used TypeScript myself. I just like done some research to figure out for this talk how I can compare it against. So it is verbose. And this is a quote by Helen Keller. The only thing worse than being blind is having vision but having sight but no vision. So in pure script, you have types, but the types do not help you a lot. So let me like show you some code snippets. So this is some TS TypeScript. I don't know how many of you have seen TypeScript. So this is a function. So I don't know. I've talked to someone in the morning or why it's actually valid but I couldn't get a... Here I'm actually saying that person is a question mark person. Not a normal person. It's a question mark person. I can't pass null. But here I'm actually calling hello person. But the TypeScript compiler doesn't... The TypeScript compiler doesn't actually tell me when I compile that it's actually wrong even though I've enabled strict null checks, strict proper, I mean, like a lot of other things. So I mean, and there are other things as well. So that's... And for this example, let's say X, you kind of like say 01 and user. I mean, this is okay for hacky code. Let's say you are doing something. You're just trying something on gig school and stuff like that. It's fine. But it's actually like not okay to push this in production. I mean, like if you push this to production and it's... I mean, of course it's a couple of three things. It's a number, probably the age, it's just one number. But you cannot push this to production. I mean, code like this. And I mean, this is the thing about arrays itself in JavaScript. And okay, so this is something around properties not being initialized properly. But I think this is a very optional parameter flag you have to set while you're doing checking. The other one important thing is like this. So here I've declared D1 as any and I've said D1 is two, the next line. And I'm saying let D is equal to D1 minus two. It compiles. It actually gives you zero for some reason. I don't know what will happen if you like change this to F or something like that. So I mean, I don't even want to try that. Let's assume it's two. But this one, this gives me a compilation error. This is a compilation error. If I say two minus two, it's a compilation error. But if I... This is fine. I don't know. I mean, like I'm not the type two expert. Probably there are some reasons why they did this. And yeah, similarly this one. This is a problem. But the next line is not a problem. So this is what I'm saying. You have sight, but no vision. I mean, probably the... I'm not blaming the TypeScript compiler guys. They did it for a reason. They did it all. But like I'm saying like it can be better. That's what I'm saying. So the TypeScript, you trust the developers to write the proper types. Which are... Yeah, and you trust someone else to make sure that they're actually correct. So and immutability is only by choice. Immutability is not built into the language itself. So there is any and unknown or like new things which can have some problems. And what is the surprising thing is like it is actually a super set of JavaScript. When you say JavaScript is a very unreasonable language and you're building something on top of that, you're not solving the underlying problem. You're just like adding something. You're adding some parts of it. What you're saying is this thing. But like I can just cannot like finally say that, okay, this compiles. Okay. I'm reasonably sure it will work. Right. And the last part is like, yeah, there is no production protection against side effects. That I'll see. So, so this is a piece of code and JavaScript and this is the same thing. And yeah. So, this is how we do add to numbers in JavaScript. This is the same as X and Y. Same Y. And yeah, something like this you do. Let's say tomorrow for some reason. You get a new developer and that guy decides, okay, let me like cash the results of the addition. And let me save it and then like, instead of doing an addition, I look in the cash. All right. And this cash could be like a global cash. It could be like a over your API or whatever. And he does something like this in, in TypeScript. Your type signature still doesn't change. If you still think about it, it takes two numbers and gives you back a number. It still hasn't changed one bit. But you're doing much more than what you're supposed to do at, right? You can't do this in JavaScript. When you're doing this in JavaScript, you know how to do it in JavaScript. So you're like, I'm like, yeah, and like, you know, you say the maybe, and maybe say, blah, blah, blah. When you're doing this, your type signature changes from end to end to end. So don't worry about it to end to end. Just think about in comma. I'll tell you why it's arrow that way. It's int comma int to end to int comma into effect of end. Because you're actually doing an effect. This code won't even compile if you call it into end to end, right? And that is important. Now, looking at the type signature, I can kind of say whether this code is going to do something, not what it's supposed to do. I mean, of course it can't still do add and multiply and things like that because they're still going to give you numbers anyway. But you can kind of like do, I mean, they have not the big problems. Today, if you're going and changing something in the database, or you know, you're making an API call or something like that, that's going to be like bad. So this is called an effect. So whatever IO you do is called an effect. So there is a, I mean, so in fact, there's a better way of doing it. So this actually takes two integers. And this is something called a state monad. Don't worry about the code. So basically it takes that state, using the cache filter as a state, it gives you back an edge. Basically, this function is going to access something in a cache, which is a type cache result. That's what it means. All right? But it's finally going to give you an int. So don't worry about the details. We'll go over this later. And you'll, so more brownie points here, you get more concise syntax. The compiler doesn't let you push incomplete functions through. So I'll give you an example here. So let's say, okay, how many of you have heard of the fizz bus problem? Okay. So basically it's like given integer, if it's a multiple of 15 print fizz bus, if it's a multiple of three print fizz, if it's a multiple of five, say buzz, otherwise just print the number. That's what is the problem. It's a starter problem sort of thing. And this is how you enumerate. So basically this fizz bus is actually, the outputs can be enumerated as fizz or buzz or fizz bus or non-fizz bus with an integer. That's what it means. And let's say, so this kind of does everything. So this is how you write. So this is a pattern where you say, if the remainder is with three is zero and five is zero, blah, blah, blah, blah. So let's say for some reason, and you have this like beautiful type annotation, it says it's a int to fizz bus. And basically this is the compiler. We'll look at this guy. And there will be like some mark here which will go right when you think of the break. And let's say we don't handle this case. We don't handle this case. What happens is that the type signature itself changes. The type signature says a case expression could not be determined to cover all inputs. So and what it says is like plus the same thing and it says alternatively add a partial constraint. So you can just say, apply the suggestion. Oops. I think so now it's happy. So now it's a partial function. What this actually means is that, let's say you have a array of numbers and you want to map over that array and you want to run this function fizz bus. So we'll ask the PRCIP compiler what it thinks is the type signature of this. And it will say it's actually partial because this guy is partial. So now let's complete. I mean, so you understand, right? I mean like every, let's say there is something which is incomplete. Anyone which calls something incomplete is definitely going to be incomplete because you don't know at runtime what numbers will come. Let's complete this and we can remove the, let's come back here. I've removed it. Okay, so this is sorry. So then we say P add type. So now it's fizz bus. So we'll go and ask type. Oops. Okay, fine. So you can say it's like a list array of, sorry, right? So make sense? So we've completed the case here. This guy called it as the correct type because the partial is gone. Now this guy really comes, I do know the function. Now no more partial. And the thing is you can't like push a partial function to main. Main expects a real function. If main has a partial function it's kind of like pros and cons. I mean you'll have to use some weird stuff to get rid of it, but don't do that. All right. And the next thing I want to talk about is type hold driven development. Right? So now let's say this, so it's not about just type checking. You can actually ask the compiler what functions I have to write at this point of time. Let's say you have, so you have this, you tell that X is going to be an array of fizz busses. All right. And you have the function fizz bus. All right. And you're going to like run it over the array one. You have the input array one to 100, but you don't know what function to actually add here. So the, the, the, the compiler will actually tell you that this is the function. You have the, this is the function signature you have to add. It has to be like, think of it, think of it as like two functions, two arguments. It takes an array of integers, the second argument, the first argument is a iRotter function, which is like from int to fizz bus. Right. And it finally returns an array of fizz busses. This is actually map. Map actually like does this, right. So, and it, and it has told you that it says map is something you can actually use. I mean other things are there, but finally, yeah, I mean, like, they're all like versions of map. So you can actually like go and like, so, yeah, so, so this is how powerful the Piasco compiler is. Right. So I just given you a gist of what it can actually do. So, so when I said the AST and you know, you can go and ready with a part of the AST and all those things, this guy actually has it. So you can actually like go and like, it knows that this is in a huge 100,000 lines part of project. It is optimized so that like, you know, if you change only one part of it, it knows that that is the only one part of the subtree it has to change and all those things. So, yeah, so, so, and the thing is like the type inference mechanism is strict. When I say strict, no uncaught values, no side effects escaped. All those things are embedded into the language. You don't need an external framework to tell you that. You don't need immutable data structures as a library. All those things are not needed. Everything is baked into the language. Right. So here are some more points. Okay. This is a quote by Oscar Wilde. A good friend will always tab you in the front. So, so TypeScript will be a good friend to you. See, because like, it'll tell you that this is wrong. You cannot, I mean, it's like not you can like, you know, you can cover it up. You can push it to production, you know, later when things break. Yeah. See, sort of thing. Like this is like, say, you know, you can't push this. This is not what you say it is. Right. That kind of strictness is available here. And something like this, I mean, so in JavaScript, let's say you're writing employee name is Paul company, just pay and say log the employee's email. Right. This is actually a compiler. I mean, TypeScript also probably allows you to do. I'm not sure. But yeah, when these, these can be caught. I mean, these are not the only things these are, I mean, like there are much more farther things you can do in JavaScript. So, you can find it is better debugging as well. So, as soon as you type, you type something, you can kind of like know the compiler will tell you, like, you know, this is not expected. This is not expected. She's basically stabbing you in the front. And he's like saying, you know, so and it's like, better development cycles. So, the compiler guides you. All right. And let's finally talk about at just pay, how we use pure script. So, looking at the type signature, it tells a lot about the function itself. So, so what happens in just pay is like, we are a payments company. I'm sure most of you have heard of, we do a lot of integration, a lot of banks, a lot of merchants, a lot of gateways, there's so much of transformations. Right. And you know what? We actually have a lot of interns working on this. We have a lot of interns, guys who are like just out of college, who can just hack things and get things done. But our type system can, I mean, but you really can't do really that bad in a pure function. When I say pure function, a function doesn't have effects. Right. So, you just can't like, you know, go and like modify some global mutable variables and you know, things like that. So, of course there are things that are like, so that, so this has been our three pronged approach. So, we write tests for our pure code, we review our impure code and we just keep the impurities to a minimum and at the fringes. Right. We write production ready code. We are not like saying, okay, we'll do this today and it'll probably not work tomorrow and all those things. When something works today, it is guaranteed to work tomorrow. That's the guarantee which I'm sure like the Pyosco compiler can give. Oh, the next point is something very interesting we did. So, this is a, I mean, to be honest, like we don't have a lot of tests. We don't have a lot of test data because like things come and go and like keep changing. So, what we do is like we have a layer through which we have separated out the pure and the impure functions. Right. Think of it as a wall through which like everything goes here and comes out and we kind of like put a tap and we kind of like captured all the data which goes through this on production. And we've got all the data we wanted and those are actual test cases. So, now we have like millions of test cases which we can run on our local laptop without even writing one line of test. So, this is because we were able to separate out the pure functions and the impure functions through a clean interface. And this is, I mean like let's say in any other language you need a lot of discipline to do this. Right. But this is like right. And, okay. And another thing I just want to like show to you. So, there is this thing called pursuit. It's a it's an online documentation viewer for JavaScript. So, here you can just search for names anyway. You can just search for map. We'll tell you what map is. You can also do something weird. So, you can like do something. I don't know if I just give me all the functions. I mean, then it says power, repeat, power, add, append, const, all those things are there. So, let's say you I mean like most of the times I work on Haskell and PureScript. Sometimes I don't remember the name of those functions. So, sometimes it's like this is a very this is a very common function which you have to use. You have a number which is a decimal and then like this thing. So, I just type the type signature I know what function I have to use. I'm not sure like if any other language has this. You guys should like give the type signature and you can get all the functions exactly because like this is like it takes types so seriously. So, so this is one of it. So, like I just go through types in PureScript what they actually mean. So, they are all like derived from the set theoretic background. So, there are mostly three types of three kinds of types. So, one is called a some type. Some types is like an enumeration. So, your fizz buzz was an enumeration. So, you can do something like this or its product types. This is basically tuples or your records. It's basically like Cartesian product in your set theoretic parlance. Also, you have the function types. So, these are actually you can think of it as tabulations. The good thing about these things is like you can define these in your application. And this is some, the function type itself is provided by the compiler. You can't like define your own function type. You can define these yourself. The either and the tuple because there is something called let's not go there. So, okay let's let's view functions in details. So, I told you to forget that arrow and set comma right. So, how many of you heard of this term Kareem? Okay. Oh, most. Okay. Good. Very good. So, yeah. So, then I don't have to explain this. So, all functions are Kareem. For those of you know. So, Kareem is like it always takes one argument and returns back one argument. So, what are the return argument could be actually a function. Right. So, that's how you do multi parameter multi argument functions. So, what you get as a side effect is that you get partial application for free. Partial application is like where you have applied only certain arguments other arguments are actually free. So, so for using this add I can now define increment as just say add one. I've just partially applied the first argument. So, basically like if you look at just why right. So, and for example, the last one. So, this you have your ready I mean like sure you've heard of the reduce. So, think of folder less reduce. And so, you have a function which takes a list of integers and gives you a sum. So, oh by the way this is actually the function which actually adds. This is the function it's actually the if you if you look at it type of add is if I forget this semiting and all it's like think of semiting is a as int. So, it's actually like a function it takes to right. So, basically that's a function you don't need any extra syntax around it extra things around it. So, you don't have to like say a function add or plus. So, so, so defining a function is very lightweight in cluster just like one word that's it. So, most of the time code looks like this. So, you look at the standard it's the okay let's let's deep down deep dive. So, then we'll look at pattern matching. So, when you have immutable data you can kind of like look at their how they were constructed you can kind of instead of just constructing data you can actually kind of deconstructed data and then you can like see how they were constructed and then use them to run their functions. So, this is a so this is an enumeration of direction two things two integers and yeah look not the currying here so let's say I want to define a function move which takes a direction and returns a coordinate to coordinate function. So, this is what I mean by pattern matching you ask the direction what I mean are you a north or south or a east or west and this is also pattern matching using them on the right and left equation. So, this is what is pattern matching so and pattern matching has to be complete. So, when you don't handle cases like we did in FISBUS we will actually get a partial error and you have to fix the partial error before pushing to perfection. So, now let's say we want to define a function called move north and you just apply the north thing right and then let's say the composition operators. So, this one is like apply F first and then so and let's say you want to define a function called north east move north east you can just say move north composed with move east so it will still be a coordinates to coordinates function right it's so easy to create new functions from after all that's what we code for right I mean like we just need more functionality we just need so I don't know like how many of you have heard of this like maybe and optional in other languages yeah yeah so like I mean even TypeScript has something in it yeah okay okay yeah yeah a lot of languages actually have this like Scala has it probably Java is also getting it I think they have the optional I think some library I think like they have it so this is so so maybe are are are used to tell that this value can actually be null so maybe a is not the same as a let's say you do a API call to get some data you get some data you don't know if it's done you decode it as maybe a instead of saying decode directly to the a because like at the end and the thing is like you take this function exam and the thing is you do something else so when you use the just constructor to deconstruct it you know for sure that there is a value so there is no null point of exception there there is no nulls in JavaScript there is no nulls no undefined all these things are just not there so when you write a function like divide which can divide two numbers so this is how you can actually do you can actually have your internal you cannot do a have undeclared there is no variables as such once a value there is no variables there is no mutation it still compiles to JavaScript it's like JavaScript is the compiler right so it's like your Kubernetes still runs on real AWS machines it's still immutable right so think of it that I mean like don't worry about the implementation like I can always go back I mean I can always like brute-force we generate some code which says that if it is nothing do this just do this JavaScript JavaScript is this thing the underlying assembly is even worse right I mean like finally like yeah I mean it's there I mean like but this is just a more higher level language see okay the there is nothing inbuilt in the pure JavaScript compiler everything is like add-ons the base compiler probably has only like functions and applications everything else is like libraries you can define so okay I just move on so this is a pure function a pure function is something which takes A and gives a B so but if you want more there are something called effects so when I broadly said about effects so when I say effects it's that a function can do more it can take a different set of arguments when I say just A to B it's just like this I want I'll give you a list list is actually an effect I'm saying like I'm not giving you one value the B here is not a single value anymore it could be a list when I say list of B it's actually an effect saying that you know I have multiple values you choose whatever you want or it could be a maybe when I say maybe I mean I can give you an A but sometimes you I might not give you an A you're just being very explicit there or a tuple when I say tuple of A and B I'm giving you an A there are other things like there is a reader one of the best things here is the app is an abstraction over callbacks I'll have a slide on how it actually works so how many of you have heard of this word monads okay alright so I'm not even going to go there so monads are basically computation builders almost all programming patterns can be explained in monads and think of it as like the basic version of the distilled version of callbacks so and do blocks are a syntactic sugar on top of monads I mean people have used Scala there is this thing of for comprehensions and things like that so you can do some like so basically you're saying like elements from to where to then bound from the element something like this syntax it's a function basically it's a function it's actually a function which takes two numbers two functions that gives you a list dot dot is actually a function so pure and empty everything is function if then else is actually a keyword but yeah so this is over and maybe so this we did over this so every time we had a list operation we were like operating on a list this is over operating on a list this operate over a so this is like something from our action for the function code this is like let's say you have a string you've got a string from a little bit set it's a log line and you want to get the time out of it so what you do is like you pass the object it could be an invalid GSM so that time it could be done or nothing as well it could be so it'll just short circuit there it doesn't execute the rest of the part of code so now we will discuss later as to how we can short circuit it or not so this is like this is like you're putting up a key called metadata which it also could be maybe that can't be there and it looks like imperative code looks like imperative code but actually it's like doing a lot of other things in between I'll show you even more fine stuff this is actually these calls are actually let's say real they actually have a success callback failure callback all these things but this will be complete only if it is a successful value none is actually checking if the account is empty this none is actually like a it's a function which actually it takes a list and tells whether the list is empty it's not the JavaScript it's a string and it asynchronous asynchronous you get me a first you say what do you do you make a callback you get accounts on on the return back you find it to account to function it tells you account is empty empty meaning like the empty list like part it works it's part along the structure of a function that I'm not worried about you get empty return nothing otherwise you make another empty call you get an onsite thing for every account and you know get the maximum by comparison all the time basically you read it like English and discuss it later I still don't have time no no no macros or this this is something this is a framework we actually had so this is like called this is an entire app entire app on running on the mobile I mean like there are other things beneath but the thing is like this is the actual flow of the app so we have created this flow on app like the thing which actually interacts with the user with the on and on let's say it's a built payment so when you show the operator there are many clicks like a operator you capture that in the variable operator and like ask for the mobile number ask for the mobile home and then like make a report to this and then so this is an actual app we have and I mean like you can do I mean there are a lot of players all this thing but like the the gist of the entire app can be done here in fact product managers can actually write this kind of code this is like the quality yeah it's like this way yeah it's like UI dot means it's like this way sort of alright so interacting with JS I don't want to get too much so you can actually write foreign function interfaces so if you have a carried function which actually is weird you can import it otherwise there is a type called fn2 which takes to this thing I don't know how else let me oops oops alright so so basically like which can take two integers and give back an integer you can import types from JavaScript into PureScript yes yes you can you can export and import functions so it's not yeah it has so most of things are not in pure script you can actually import if I say I if you want to write from the you can just like write the same file name that's all you have like main dot pure you have main.js the convention yeah yeah you can kind of like export I mean like UI and dot is like a module UI is a module yes yes it's a single file finally you get a big file that's it you run your script objects kind of like transfer to this named tuple this one and arrays cannot be multi types they have to be like a single type so there is a library called foreign which this works when the types are unknown it actually handles the cases so this is like return multiple names that's what it means yeah so finally you can export so this is fine so and this is how you import effects so when you say it's an effectful function you just return a tongue so instead of like saying like tag so let's say there is a format log logging function which like says with a tag and a message log it you return a tongue which has like the tag and message tied but it can be called at a later part there are so many web frameworks available you have Elm like web frameworks there are component based there is a pioskup react which has good interrupt with react there is FRP based Presto is something we at JustPay have developed to write mobile and web apps yes yes yes they are already supported in pioskup no this just allows you to work in their libraries not in the language but in all libraries you can pick and choose whatever you want so in our back end we have this flow in UI we have this Elm like thing we have like events coming in like user events and all those things sorry I am going out of time so we have like virtual DOM available you can manipulate your virtual DOM at JustPay this is how our stack looks like we use it both for the front end and mobile UI so back end and mobile UI so that's it last slide and we use it for markup and business project we write English like code and we have something called free monads and we write DSLs in that and get away with it that's it super questions in pure script can we like whenever we use JavaScript so we take the values from the UI like text box or anything is it possible this importing like you can import it as whatever you want and then like yeah that's it this is how you like import functions so like say you have but make sure that anything which you think is a side effect is actually imported as a side effect that's the only thing it's pure add is pure but logging is not pure because you are actually like logging with the date and time so so date and time change every time you log it so there and importing it so you can import whatever you want you can export whatever you want it's fully interrupt with JavaScript you can have your own node modules interact with them as well you can require them in your JS files and import it everything is fully supported so we can use like normal JavaScript so initially when we started off we had a lot of JavaScript code we didn't we didn't start off with JavaScript we had a lot of JavaScript code we finally like wrapped everything then main changes here main changes here it's slowly complicated that's how you can use this independently in any small module if you are able to take the decision thanks any other one last question how is the community in JavaScript? it's pretty helpful Slack is there with good Slack community documentation is good on first there are a couple of good tutorials and I think between Slack and documentation there is a couple of books there is try.piastrup.org you can like check it out try. so this is like an online place where you can try so yeah so this is how you write markup and so this is FRP so just like you can kind of like add the sliders so this is FRP so you have a pure function called markup so you can change your I mean like these are all like you know this is FRP flat is a FRP based behaviors is another FRP based thing thank you so community is okay I mean like it's not very big but people who are there are really committed I would say