 So, you in how many people know or use Kotlin here before ok, just what ok, so yeah I am not very far though, but it is, so this talk is going to be about DSLs, I hope I do not have to introduce DSLs also or people have ideas about domain specific languages, so I will skip that part. And so Kotlin is a language which is particularly suitable for creating very nicely looking DSLs, on the surface they look super clean and sometimes the languages help. So, we will go over that, this is me not the dragon, somebody standing in front of it, so this is in our office. So, I work for a company called Rencode Labs and we mainly create programming languages and compilers and tools and interpreters. So, besides that I wrote few books, so this is a same less advertisement if you will, but yeah, so this is what we do, we write compilers, we do interpreters and all that and we are the biggest individual independent compiler company in that way because you can go to Microsoft and say or you know Google, they have huge amounts of compiler engineers, but you cannot say go to them and say make a compiler for us, they would not do that, but we do that. So, and so this talk is not about my company, but I am trying to introduce because we are studying new here in Bangalore, basically we are a Belgium based company. This is our technology stack and this is our target platforms that we use. So, we do mostly compilers, our written in C-Sharp and they are targeting to Microsoft IL and we have been awarded twice by Microsoft as the legacy modernization team because we help customers to port their runtimes and from the mainframe to .NET and so that is what we do and we are hiring. So, if anybody interested please talk to me afterwards. This is our fancy office in Bangalore. I am just going, you want to tell? So, I will skip this part because everybody is familiar with DSLs. So, why Kotlin? Because to me, it first time it made sense because it is probably one of the same, you know, pragmatic programming language on JVM after Scala and but Scala is too much to digest for many people and so I think Kotlin is a very nicely placed programming language plus it has support for almost any conceivable platform these days. So, I will skip this also because these are the comparison with friends. Like it is almost like Swift but if you want to follow along, I hope that is probably impossible because of the internet. It everybody connected really bad. So, we will do little, I will show some demonstrations. So, I will check whether it is connect or not. So, for you can go here and directly try, oops, just because it is the browser that is a, I will change it. Now, this back. So, I think that is a little bit problematic, but we will manage. So, I think you can see it now. So, this is, there is this, it is a repel provided with command line Kotlin command line repel. So, here you can Kotlin provides multiple features that are suitable for like extension functions. So, if I want an extension, I want to extend the integer class I can say like this. So, what it does is it creates a method called plus on the integer class and I can take an argument which is of type integer and this refers to the calling object which is this. So, at this point it will create that. So, the bad part of this repel is it is also you know just paste it the expression on that. So, which is a little bit annoying, but apart from that it is good. So, I can now say 2 plus 2, 4. So, this is the extension function and it is very handy for situations that we will come across later. Another thing that many C-sharp also has extension function, but C-sharp does not have extension property which is very interesting. So, let us say I want to design extension property called. So, I am designing an extension property called it is probably good to show it here then there or here. So, you can design an extension property like this. So, here we are creating an extension property called factorial and that we will create a property on the integer class and so now if I will try to compile I think I will compile this already. So, let me go back this is sorry about that here it is it is here let me try this. So, okay. So, this is a handy feature you can load this thing yeah. So, basically I can here I it is like a script. So, I have this all the functions and all the properties that I want is here and so now I can use any of these already. Since factorial was defined here and I can have it here. So, that is extension property. Let us go back to slides. Then we have infix operator which is very very interesting. I did not see it in many any other language and it is I it felt very nice that you can do that. So, for example, I can do like this. So, I can define a function called infix then say fun and as an extension function plus other int other plus this. What this lets me do this I can do this. So, I am actually placing the function inside the operator and that is very very you know interesting to see and we will see some nice use cases later on. And then we have variable arguments. So, I can have a function called another extension function say and the variable arguments take this the var args. If you say var args that means your the number of arguments will be variables then you say prefixes and they are all of the type string and then you define it what the hell is going okay not a good day. So, I will let me write it here. So, fun string that starts with any of where arg prefixes string. Now, I say this dot any. So, this is a lambda and say sorry prefix any sorry prefixes dot any it this starts with I hope this will compile it does. So, now I say sudipta dot starts with any of a is nothing I did not return it here. So, I should so this is now complaining because it thinks that it is the return is not I did not provided the return type. So, that static typing is helping us here. So, now give this so it is a point of success moment of pride. Okay. So, let us go back to our so operator overloading is general purpose I will not use operator overloading to this I will skip it it is a huge section but you can use operator overloading with a predefined set of functions and you can overload plus minus increment assignment equal to like multiply you know some application then equal to and then we will use this another very interesting feature in few programming languages like F sharp has this back tick method. So, you can have spaces inside the name of the method. So, that is really interesting to see. So, for example, you can have say say like things like I think it is always coming at the bottom. So, let it go up. So, you say fun say hello and then say name string is equal to then you say hello and then string interpolation. So, you have string interpolation in dot Kotlin. So, you can directly whatever variable you have you can do that. So, now that we have that I can I should be able to say say hello. So, this get little bit interesting because now we want to mix and match a few things. So, we say I am trying to do something very desperate which is I want to cook up one DSL live here which is so this one. So, basically this is that we have the Fibonacci series we all are familiar with it. So, I carefully chosen the example so that the toy examples are such that everybody can relate to it and so that they learn the toy example and back home they can try it. So, that is why the idea is. So, this is the DSL that we want to implement is like let me try to pull it up bigger HDMI. So, Fibonacci number is represented like first two digits numbers are one and one then you go on increasing the number. So, if you describe the Fibonacci series to a five year old you will say like that like start with one and one and then the Fibonacci rule is add these two numbers and then go on. So, if you see here I have carefully color coded it. So, that the start with and then follow and whatever is in the white are the phrases of the language which are static parts of the language it does not move. But whatever is in the green is actually the moving parts of the language like the seeds are the new thing that you can give and the rule is dynamically you can change. So, the DSL that we want to create will look like something like this we will say start with one and one because then we get the seeds then we give the generator we do not want to generate it as right now because it will generate lazily when we say for and then we say that is many times then we go and generate that. Does that ring a bell or have questions? So, let us do this quickly here. So, I think I have the Fibonacci function somewhere carefully kept. So, we do not have the time for everything but we will go over this. So, I will open this in IntelliJ. So, here is what we do. So, we create starts with start with is a method with the back ticks and we take a seeds which is a pair of integer and integer. So, Kotlin gives us a key value pair but there is no key and no value it is like a pair and then we return a mutable list of integers just I could have and then we have another extension method on mutable list of integer which is then follow don't look at the down one I think I am talking about this highlighted piece. So, here what it does is it takes a mutable list of integer and then it gets the rule. So, the rule is the last two integers and you get another integers. So, it is basically taking a functor if you will. So, this rule is taking the rule as the functor we are giving like the integer integer and it is returning a pair of the mutable list of the seeds these are the seeds and the rule. So, at this point we are ready to generate it but we will not generate it as yet because we do not know how many you want for that we have this for method which is here down where is for yes. So, for is a method on extension method on pair of mutable list and a pair and the rule and then we iterate over how many times you want and generate it and using memoization. So, we keep the last two and keep it in the list and then we add the second one and then we go on going adding it and at the end we return the list. So, let us try to run it and there is a fancy little extension this like we have the in dot times basically when we say in English we say do it 10 times we do not say do it 10 we do not stop like that. So, that flow should be there when you express a DSL that it should be as close to the expressivity that we want to that. So, let us try to do that. So, let me say. So, I think it is already copied so try to load it good. So, now I should be able to say for m in start with 1 and 1 then follow feebo for 10 times print l n m. So, it does not really let me let us quit from here create a jar and try to run it please compile good. So, there is still some intricacies with the repel that I have yet to figure out or it is buggy because I do not know that for sure but it works either way. So, if we fail to use the repel I can create a kt dot kt or dot kts file then I can compile it and make a jar and then run it here. So, any question here on this is this interesting or is this little bit different than what you knew no answer. So, let us go to the next one. So, this is Armstrong numbers as we all know basically this is another example from this. So, this time we will do a little bit fancy thing. So, number theorists are always working on these numbers like we are all taught like the sum of the cues and we know that and if we take the closer look of the free form query that we want to support is this is what we want to support and this is the mapping that in like if you see the bold part on the left and see on the right they map although their orientations they are you know their locations does not match up quite well but that is how. So, this is the target internal DSL which will make a developer happy. So, if you are working on something which is super complicated and you get nice building blocks then you can join these pieces together and this will make our mathematician happy because he will probably not be able to is too cumbersome for him to write in that way on the left hand side but he would like the free form query. So, another desperate demo attempt is to create to show the external DSL implementation. So, we will be able to write on the queries in the almost free form English like syntax and we will get the results. So, let us see. So, let us first show the effect then we will go over the since so when I have so many complicated functions and all that I do not try to do it in the REPL because the REPL keeps pasting things and it becomes very annoying. So, here we compile that example. So, now clear the screen. Now for try to so this query it is a now we are running the external DSL. So, now I will write the basically what I will do is for demo sake I will try to copy paste this code because so this code here. So, this is the code so we will say this and go here and paste it. So, it says the sum of the cubes of the digits of the number is the number itself and the idea is to give the mathematician a console where she can express herself in this manner and then we hit enter and we get this back. So, behind the story this and lot of other things are going on. So, we will run it again for another query which is again on the so if some of the factorials again this is the toy example please do not concentrate too much on the complexity of the example itself but it is to prove a point. So, it says the sum of the factorials of the digits is the digits itself. So, there are so many numbers like that and these numbers having you know uses many places. So, so this is the example demo. Now let me go back to the code and try to show you the code. So, this is I Leo taking me rest sorry. So, this is pretty straightforward we have a digits extension method on type of integers and we return a mutable list of integers and we return the number of digits. So, let us break it up now. So, now let us try to load this script and do every little bit pieces in action. So, do like this and then we do that. So, it is already loaded. Now, I have 1 5 3 no I have to go sorry sorry sorry I have to go to the it is not the ripple I am sorry. Now, we are in the ripple now we say load that file and please come back. You have we have so many other things that we cannot do this here. Let me so we have digits here actually it would have been really nice let me copy this piece by piece and now see what happens. So, since this bracket is here it will be terminated. So, I think just like that I mean because I wanted to add the digits one by one I instead of adding it. Yeah, yeah it is just by accident I did not intend to I mean is no problem returning a list essentially we probably will be returning a sequence. So, let me try that. So, now we have the digits function in. So, now we can say 1 5 3 dot digits. Now, it gives us this. So, so ripple gives us a nice idea to how our code is going which direction and all that. So, it is really nice and then we have where are you yes here cubes. Now, we have cubes. So, now we can say 1 5 3 dot digits dot cubes and since cubes is returning as a mutable list we can always call the actual sum method which is readily available we do not have to design it. So, it is 1 5 3. So, behind the scene also some other examples was going on because we are composing the function. So, so this because I want to apply some transformation on the digits because I want to cube or factorial then I want to sum them up. So, when the sum they become integer when they start they become integer in between their list of integers. So, that is a b and c basically a generic composition method that we take a function that takes b to c another one that takes a to b and we return by applying themselves then we get a to c and this is pretty straightforward but looks really you know different and you remember that we had that factorial property of an integer. Now, here we are exploiting that we are having a factorials. So, if we if we do this because here we are actually projecting the factorial and getting a list of factorials. So, that is all other things are like mappings and the magic of that query that I gave and that is happening here. So, I am creating multiple functions that can check for whether they are fact you know Armstrong numbers or other type of numbers using this compose method and then I am creating a map of their keys and thus and then whatever query you give we I look through the tokens and if I find something matching I create a switch. So, and this switch can be easily determined so that I can say okay you instead of cubes somebody wrote cube. So, that matching can be done. So, I sense that it means to get to the factorial number or the Fibonacci number and from there on I look in the keys and then if it is matches I invoke that rule. So, it is basically we are leveraging an internal DSL to create an external DSL. Any question here? No question. No question is always good. So, here it is in the picture little bit under the hood. So, like just for the hammer home that we create it is like Lego blocks as we all know functions are like Lego blocks. So, we combine them together and under the hood we have this compose method demo is over I did not realize that I will hit the grandpa. So, this is the third and the final DSL that I want to show you a glimpse of. It is targeted for writing unit tests in Kotlin. There are several problems that I personally feel it is my humble opinion that exists in current unit testing framework. Whenever somebody introduces a new framework they say there is some problem with the existing things. So, this is my take on the current unit testing framework landscape that my biggest concern is that they can only be used in test like so much work is going down the drain because you cannot use an asset.isTrue inside your common normal nobody does that that's like and then it is not basically very important point is the third one is like no free lunch. So, you did not fill in the message that what is expected if the test fails you would not get you just say asset fail. I mean nobody is giving you free messages like okay you compared like A and B and A and B are different it is expected of the framework to fill that message in for us but they do not do that. So, these are the three design principles that I embarked upon that I say I think are justified for a testing frameworks like you can read it but the last one is very important like you should have sensible autofilled message always okay I will skip it. So, the difference just fill the difference on the slide. So, this first line is a normal asset true statement and the developer did not write the message that should come if this test fails. So, if the test fails no message first thing and second thing is if this code appears in a unit test where another 10 lines are down below that breaks on the drop of a hat if this line goes everything below does not even get executed boom. So, no glue. So, the second I mean it's not geared for gluing pieces together it's like one at a time one at a time and most of the time our unit tests are not exactly testing exactly one piece one you know head of a nail it is like testing a square feet area I mean so it the surface area for testing is too low and then the this is what I am proposing in this framework is you write in this type of language this is readable and if you fail it will automatically populate the error message for you it is glueable because of the extension methods and the infix and all that there is couple of more examples from the library that I am proposing. So, don't look at the numbers that first example this looks stupid because obviously the second does not make sense instead of 34 assume that it's a function call like like some number some number or age should be between or something like that this is this is glueable code. So, this is the problem like continuation is the key so you should be able to chain your methods and are you know the expectations and some demo again hopefully it will compile. So, this is how it is organized can we go to internet? So, all this code that I have shown so far are on GitHub I no matter how small they are so that you can if this if you have access to the slide you can go to this code anytime and so let me go here so it should be a walk like by the lake it should not be difficult and this is an example. So, this please check method you see how fluid it looks and and it says infix function in action so you have multiple conditions and so at the end you are checking like whether the first one is success or second one is success and this is the glue I was talking about this and thing and when you fail everything your failure message will be aptly populated whether the name is failing or that is failing so even if it fails it this method will not crash that is the whole idea and it is on GitHub and I will put whatever I have and later anybody can contribute. So, this is the diction I think I am starting with like should be should not be like you know it is like and it is going to be classified in five different files the grandpa numbers takes date time and utilities the date time numbers takes star almost self-explanatory the utilities are the glue that we use and so that is the most fancy stuff I will show you that and like what is the glue here so utilities five minutes okay I am almost done I just want to say this. So, the idea is to have instead of returning a Boolean for asset true or asset fails or throw an exception idea is to return a type of triple or like the first one if it is successful or not pass it around like for each checks it is like one person is standing and checking okay assume your test okay you are on edges between this it is not or not I will write that and I will pass it to the next guy and okay he will write is it true or false then it passes so basically at the end your the data structure it gets populated with you know fields like that and it has like and you see this is a very fancy example let me find out here yeah so see this see these three lines 78 to 80 so first condition says 22 should be between 4 and 101 and the second condition say in ABFS it is a very like a genome testing or something like this proteins GCTA is made of this and then you will say among C1 we say like that we say among these two condition at least one should be true we do not say like take this list of condition and check if any of them we do not say like that so we say like this so if you see the you know notation for among it is like nothing but like this so it just takes variable arguments of some things as t and just create the tree and just pass it around so most of the times the function will look superfluous superficial superfluous not necessary but they are necessary to communicate on the surface area of the DSC so I'll leave it to that and then question and answer and thank you and we are hiring again and if anybody interested you can and all these are here and this is my coordinate ie sudipta at raincoat.com and we do all kinds of programming stuff so programming languages basically what we do is we so dead we code for 4 gls and dead languages like cobalt pl1 they're not dead they will never be dead we'll be dead but they will still be alive because there is a hemongous amount of code that is written in those languages but the providers are shying away they are stepping down so the customers all over the world they want another vehicle so we come in and we say we provide you a compiler so they we have compilers for cobalt in dotnet the visual studio integrated and it's in dotnet core for linux so and we have compilers for languages you have never heard of because nobody uses them in these days so please any questions no question means two things either we did not understand anything or we are sitting here by respect we'll just get out okay then okay thanks