Introduction to PureScript - Bristol JS - July 2018

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good afternoon good evening my name is Jordan Lord I'm going to be talking about the pure script programming language today just sort of a short disclaimer before I start I've never really done a talk like this before and I am no sort of expert in pure scripts but I do think it's a really cool technology and hopefully I can sort of present that in a decent way tonight so pure scripts is a transpiled programming language a bit like a bit like typescript a bit like Clojure script CoffeeScript live script Elm all these languages are transpiled to Java scripts and pure scripts is another one of those but it makes us the question especially with languages like typescript and coffee scripts they are very they do compile very they are very similar to JavaScript in some ways so it sort of makes us ask the question what is what is wrong with Java scripts is this is this language terrible that we're here we're actually meeting up today to talk about this language and it's actually yeah a bad language is it what why can't JavaScript just be normal I mean in what in what language in what language doesn't an empty object plus an empty array equals zero well whereas the logic behind that there's not really any types of Java scripts not I mean there is sort of like crappy lot types but yeah there's not really any types we've always got to choose between a double equals and a triple equals and again for a beginner the very little subtleties between those two can be really confusing and here's sort of another example there's the comparisons I think this sort of shows that there isn't really any logic behind the empty object for sent your equals zero we have to choose between let var and cons I mean we should know when to use Const or when to use let slash val but the differences between let enviro very very subtle and again for a beginner that can be really sort of confusing we've got two different types of nulls we've got null and undefined we've got we've got not a number nan and we've got infinity again it doesn't really make much sense to a beginner I mean this is a perfectly valid example of me tasting objects but in JavaScript we don't really have any immutability so you can pass an object to a function and it can change it can change the state of that object in that function without you actually realizing it and some libraries actually do this and it can be really difficult to debug what's actually happened we can get kind of immutability I'm gonna sort of justify that last statement a little bit immutable J s of course we can use things like that but it's not baked it's not actually baked into the language again we can have we can have the side-effects in JavaScript a function that does look like an innocent pure function might actually be starting World War 3 which I mean is if this is a piece of code in the in the let's say the North Korean military missile testing whatever and we accidentally execute launch missile then we're starting World War 3 without intending it but it's it's it's all a bit silly all this because these arguments we know to use the good parts of JavaScript we know to use triple equals instead of double equals we know to use I can't remember the other example now but we know to use the good parts of JavaScript so what is what is actually pure scripts I'm not talking about JavaScript today I'm talking about pure scripts well pure scripts is a is a purely functional programming language it's strongly typed it has Haskell Lee's syntax so if you know Haskell then you'd be able to pick up pure scripts very easily and it compiles to sort of readable JavaScript it not kind of like coffee scripts and type scripts but you can sort of reason with the code that has been compiled and the types only exists at compile time so it's a statically typed programming language I mean why don't we just use Haskell because we can compile Haskell down to JavaScript but Haskell comes with a bit of a run time when you do compile it to JavaScript so it's much lighter weight to compile pure scripts to use pure scripts and I'm going to take a little bit of a detour for a second so who actually as knows what this machine is yeah this is for those that don't know this is the BBC microcomputer it was released in the early 80s believe it or not I actually started programming on this machine it was my dad who introduced me to it and he was showing me games like elite and manic miner but I was sort of more interested in how to program it and make it make it do things I was fascinated by how you basically had to program it to do anything that was the interface to it and for a long time for a few years like when I was probably 10 or 11 I was programming in BBC basic and a lot of people told me that this was about it pollutes you mind basic also you know try something like Java so I tried Java for a little bit I didn't really like the ID was too it was it was too complicated and it was too awkward to set up the JDK so so for a long time was programming hidden BBC basic but eventually I came across JavaScript and what I did like about JavaScript was that all you needed was a text editor and a web browser and you could basically reach anybody in the world you can share your JavaScript applications with your friends and it was it was brilliant so for a long time with programs in JavaScript and decided that's what I wanted to do as a career I wanted to be a software developer so I started as an apprentice at a company called sage I was doing dotnet c-sharp stuff and finally I got into Java a bit of Java there but then I decided I wanted to do something a little bit more theoretical so I came to University here in Bristol the University of Bristol and we have a Haskell module and this is where I got into Haskell and I was sort of I was blown away by Haskell the the power of it the expressiveness of it and when I went back to program in Java scripts it was always a bit awkward it was like I want these nice beaches of Haskell in JavaScript and I've tried live scripts for a little bit which is kind of like an extension onto CoffeeScript but eventually I came across pure scripts and I was satisfied and that was the that was the Holy Grail almost and now I'm working for I'm going into my second year at university and I'm currently working for a company called Zuba VR this summer so we do sort of virtual reality applications mixed reality augmented reality applications I've been getting involved in a few really interesting projects there with web AR JavaScript socket IO reacts rxjs so it's been a it's been pretty interesting but back to pure script I don't think the pure script logo displays very well there but yeah this is where the talk gets a lot more boring I'm gonna go through like the the basics of like the language so to get started you need to install the pure script compiler viewers I don't know if that's how you pronounce it but you can install that from the website or with a package manager it's still a pure script actually still uses bower as a as a package manager I believe this is to do with how it handles dependencies I don't know why they haven't really switch to NPM or something else yet but they use Bower and pulp sorry pulp is the build tool for pure scripts that you'll also need creating a new project pulp in its dev service so this is like your web pack dev server and pure scripts repple so I can actually quickly actually I'm not going to demonstrate that because this is a different monitor so you can bring up your pure scripts that'll answer functions yeah functions in pure scripts this is how functions are defined so you have your you have your function name and then you have the functions type so all functions are defined as you have your input type and then you have an arrow and then you have your output type so it's it's very similar to Haskell if you know Haskell but all functions have one inputs and one output here's an example of just adding two you'll see this throughout the talk all the examples are really sort of fun creative but here's obtuse we take a number and we return a number and we're taking the parameter X and then adding two and this is just a normal JavaScript number if if you omit the the decimal point from the end and just write the number two that the the compiler will infer that it's an integer pure scripts as integers and this will not compile but what happens if you want more than one input to a function pure spit-takes i've just said that a function takes one input and produces one output but here we want multiple inputs again this is another really uncreated example literally aliasing the plus operator but all functions in pure scripts are curried it's easier to see here so when you give it a parameter if you if you give the function an argument it will return a new function that we'll be looking for the next argument and then it will keep doing that until you actually return the result so here we're accepting a number and then we're returning a function of type number to number and then give it another parameter you'll finally get your number out and this is just an example of using that function but what this is really useful for is so here we have the map function so in JavaScript where we would have to define a lambda and we would have to have like X arrow X plus 42 in pure script because all functions are courage we can have map and then just omit the other parameter to the function and and then it will just sort of work so creating types impure scripts we can define new types so these these are these work very similar to just normal objects in Java scripts but it checks the types before compile time here we're creating a user with username string email string and here we're actually using the will of we're going to actually print out that user so show use of it's taking you user and returning a string and here we've obviously got the just a normal JavaScript style style syntax for an object and here's username and email and there's the string it returns at the bottom well again all the compiled all the type inference and and the type checking is done at compile time so if I went username equals 42 then the compiler would say no you can't do that so new type new type is a bit like sort of wrapping an existing type in in another type so here we've got we've got an email and we're sort of wrapping a string as a I'm saying an email in the string basically why would you want to do that well it's very useful for validation so here's a function that's passing an email so it's testing our email string that we passed in to the regex and if it is if it does pass then we're returning here we're actually using the data constructor for email and wrapping that string as an email and then if not we're returning an error message this was actually ripped off from a another toe so I think read but this is useful for validation because what we can do here is we can we can export the past email function but we don't export the data constructor and what that means is every time you ask for an email anywhere in your application you know that that email could have only been created through the pass email function so that email would definitely be valid here's an example of exporting email so that's the type constructor and exporting the function part email and importing is exactly the same basically algebraic data types so algebraic data types are a bit like enums but they work a little bit differently so here with its what it's behaving exactly like what and even them how an enum would behave user type can either be a guest a member or an admin but we can use it for more complex tasks so here actually using algebraic data types to create a list so in a list can either be empty nil or it can be a chain of a value and another list type classes so type classes behave very similar to interfaces in normal object-oriented programming so here we're actually defining the show type class and it only has one function which is show and then a tostring and here we're actually implementing the show type class for our user type before and here's a kind of a cool feature of pure scripts we're using pattern matching so instead of writing a bunch of if statements if user is guest if use else if user is admin or a switch statement we can just right well show if we're showing a guest this is what the guest wing would be show member member and so on and again here's an example with our email new type as we described earlier and here we actually unwrapping the email type to get the string out and return that string side effects so you can actually represent side effects in pure script you but you have to sort of specify in the type that we're using we're returning a monad which is here we're actually using the effect monad and yeah so you can actually see if you think you've got an innocent pure function that's going to be starting World War three you can actually see in the type that it's it's not such an innocent pure function it will be returning an effect of something so I'm going to show an example of a an isometric 3d cube written in pure scripts so here's just I hope that's clearing off but that's just a cube that's rotating and this is purely written in pure scripts so I will bring up the code and the cube is defined here so the cube is a new type that wraps a matrix of type number and again this this can be defined however it runt wants but we implement this this again it's a little bit confusing because I've got two different new types here but we're implementing the new type type can the new type data we're implementing new type of matrix number and as long as this cube type here can be unwrapped to a matrix and can be wrapped up again from a matrix that means it's renderable so if I bring up the renderable the renderable type class so for something to be renderable it has to implement a new type of matrix number and again for something to be renderable it also has to be transform all so that's sort of the power of the type system and here's another another type that I defined which is a permit and again this wraps matrix number it doesn't necessarily have to wrap a matrix number but as long as it unwraps and wraps - then that's fine and I go to change cube with pyramid and hopefully that will recompile then refreshing the page to passing that pyramiden will then start rendering the pyramid so yeah I was actually going to do some live coding but it was probably boring enough as it was so yeah but what happens if you already have an application that's written in JavaScript and you want sort of the power of pure scripts in that application you don't want to build your application from the ground up in pure scripts using like that 3d example was but it's actually really really simple to integrate pure scripts into your current application so we can we can run pulp in it or we can just create a we can do bower in it and we can install the default pure script modules like pure scripts prelude but just make sure to remove the default main dot pure scripts but what makes this really easy is is the the web pack loader for pure scripts so installing that and then adding the pure web pack loader to your web config it's then really simple to incorporate into your application so I have an example application yes so if I do NPM starts yes this is just a really simple react app where when you click Add 42 here lad 42 to the total but what happens if we want that logic and shift it into pure script I'll open up the root component so here this ad 42 function is taking the current states and it's returning a new state and adding 42 to it so if we remove that then the application should no longer work at 42 doesn't do anything anymore but I want that ad 42 function written in pure script so I'm going to create a new file source let's let's call the module ad 42 module I'm 42 'this is defining a module and then we're going to export the ad 42 function where and the ad 42 function will take the current state and return a new state at 42 we will then and destruct and just like you would in in a group six get the total out of it which is there and then return a new object where total it's going to be total plus 42 but we also have to define States as a type total is going to be of type int and we'll also have to import valued not 42 plus and then rebuilding the application it doesn't work let's step up why it doesn't work that compiled I think you know why because in here we didn't import it if we import this ad 42 function from ad $42 your script it should seamlessly sort of work and it does so here we've got the pure script code and it's passing it's calling the ad 42 function which is defined in pure script and that's how easy it sort of is to integrate pure scripts into your current application if it's built with web pack I mean pure script does have a lot of disadvantages so you are basically learning a new language from scratch which if you're working in a team that's a big problem because everyone then has to solve learn this new language there are also some breaking changes so with that 3d example that I've showed earlier I wrote that with pure scripts naught point 1 1 7 and a newer version of pure scripts was released that's and things just didn't seem to compile and I was wondering why it works on my desktop but it doesn't work on my laptop and my laptop had the newer version of the pure script compiler and that's why but I guess if if you running this in a production environment you would have a docker container with a specific version of the of the pure script compiler installed so that wouldn't be too much of a problem but advantages it's it's like of the showed it's really easy to integrate pure scripts into your current application it's very expressive it's easy to reason about stay this is a large LAN library so a lot of the the pure script sort of standard library which I'll show in a minute sort of like alias is a lot of the standard browser functions and the type system is designed to work with you it's not like it's not there how can I describe this yeah the touch systems there to work with you it it has a very powerful foreign function interface so if you have an existing peer sips application and you want to use some of your Java scripts in it then it's it's very easy to do that with the foreign function interface and I think the number one reason advantage of using peer scripts is that it's actually fun to use it's a fun language in here are some of the resources that aren't very clear to see so peer skit by example this was written by the guy who wrote pure scripts Phil Freeman this is sort of like the standard book pure script documentation repository is useful try pure script that's a bit like cold pen but it's pure script and pursuit so for those that know Haskell this is this is very similar to Google and it's very sort of easy to look up a function that you don't know so if I don't know what the dollar operator does there is it applies a function to an argument or let's say if I wanted to find a function that took a number two a number to a number and there's there's a list of functions that take a number a number and return a number so the max function and the min function so it's a really really useful resource and that's it so if you want to find the slide it's at Jordan take a forward slash intro to Peter scripts thank you very much for being patient [Applause]
Info
Channel: Pusher
Views: 5,637
Rating: undefined out of 5
Keywords: developer language, language code, libraries, help, tips, forum, meet-up, meet up, learn code, coding education, coding hints and tips, lecture, coding lecture, learn about code, learn a developer lan
Id: He7gQ56vl4M
Channel Id: undefined
Length: 27min 38sec (1658 seconds)
Published: Mon Aug 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.