Purify your
Lambdas
Luis Ángel Vicente Sánchez
Full-Stack Developer
Follow us on Twitter: @WHTechjobs
For all tech roles go to techjobs.williamhill
Purify your
Amazon
Lambdas
Luis Ángel Vicente Sánchez
Full-Stack Developer
Follow us on Twitter: @WHTechjobs
For all tech roles go to techjobs.williamhill
What is Amazon Lambda?
“AWS Lambda is a compute service that runs your code in
response to events and automatically manages the
underlying compute resources for you.”
AWS Lambda Product Details
What is Amazon Lambda?
FAAS
What is Amazon Lambda?
Function As A Service
What is Amazon Lambda?
What is Amazon Lambda?
S3
DynamoDB
Kinesis
HTTP Request
SNS
What is Amazon Lambda?
S3
DynamoDB
Kinesis
SNS
Lambda3
Lambda2
Lambda1
HTTP Request
What is Amazon Lambda?
S3
DynamoDB
Kinesis
SNS
Lambda3
Lambda2
Lambda1
HTTP Request
Using Amazon Lambda
Using Amazon Lambda
Using Amazon Lambda
Using Amazon Lambda
Lack of static typing
Side-effects
Using Amazon Lambda
LLack of static typing
Side-effects
Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Introduction to Purescript
Interacting with an impure world
https://github.com/lvicentesanchez/lambdaworld2015
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
D
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Interacting with an impure world
Purify your Lambdas

Purify your Lambdas

Editor's Notes

  • #2 Thank you for coming. I’m Luis and I work as a full-stack developer at William Hill… btw we are hiring! In this talk I’m going to show you how to purify your lambdas...
  • #3 … actually, your amazon lambdas.
  • #5 * Read title *
  • #6 Quoting the AWS Lambda Product details page… *read it*What that really means?
  • #7 Amazon Lambda is a FAAS platform…
  • #8 a Function As A Service platform.
  • #9 When an event from one of these sources is triggered…
  • #10 Amazon Lambda will execute the right Lambda function to handle that event.
  • #11 Scaling the underlying infrastructure on demand to adapt to the event/traffic load. Before joining William Hill, my job was to build a real-time analytics platform for mobile gaming; with this background, you can understand my excitement when I learned about the Amazon Lambda service.
  • #12 So… I had to a try it. Nowadays you can now a variety of languages to create your Lambda functions like Java8, Scala…or even python. But... when I was first looking at this service you could only use...
  • #13 Javascript on top of Node.JS. Anyway… I decided to still try it...
  • #14 And I built my first Lambda function… an echo function.
  • #15 And I was not happy… not just because the lack static typing but because you are side-effecting like a boss here.
  • #16 Needless to say that I wasn’t happy so I moved on and forgot about it.
  • #17 A while after I discovered Purescript… a strongly typed programming language with an expressive type system, that compiles to Javascript. This language inspired in Haskell, but it preserves Javascript semantics making it easier to interop with existing Javascript libraries.
  • #18 You can clearly see the Haskell influence in this language; we can assign names to literal values…
  • #19 You can create simple functions like this one that implements the pythagoras theorem.
  • #20 To avoid repeating ourselves we can create definitions or functions within the scope of our function, like this sqr function. And we can do it without polluting the global scope.
  • #21 Functions are curried by default in Purescript…
  • #22 And you can partially apply them to create new functions like this function that will add 2 to its argument.
  • #23 We can also define recursive functions… with pattern matching.
  • #24 We can rewrite this function using guards.
  • #25 Here we have a more advance feature… type classes. Type classes are used in Purescript to achieve ad-hoc polymorphysim, also known as overloading. Here we have Equality type class that defines an operation eq that tell us if two elements of type a are equal or not.
  • #26 To be able to use the operation eq with Int, Boolean or any other type, we would have to define an instance for each of them. In this case, we have the equality instance for booleans. Purescript has also some interesting features that doesn’t exist in haskell like extensible records and extensible effects.
  • #27 Here we have a record that represents an user, with two properties a name of type String and a age of type Int.
  • #28 We can write functions that modify one or all properties of this user. But if we want to be explicit about which fields are modified we could use extensible records…
  • #29 This new version of the double will accept any record with a property named age of type Int; the | r means that this record may have other properties… but we don’t care. If the input of this function were an user... |r would be the name property. Given the output type, the only think we can do is to modify the age. Purescript supports higher-order functions… that is, functions that receive other functions as parameters. We can rewrite this function this way...
  • #30 We introduce a modify function that allow us to apply any function to a record that contains an age property…
  • #31 And rewrite our double function using it.
  • #32 I think is time to try and use it to write a lambda function.
  • #33 We are going to write a “time machine” that will take a test subject as an input...
  • #34 And make it travel in time…
  • #35 As a result our test subject will become older… and maybe wiser.
  • #36 Remember that a lambda function is nothing more than a javascript function with 2 inputs, an event and a context object, and one output. But which are the types of the inputs and the output.
  • #37 We could be tempted to use the User type we defined before, as the type of the first parameter…
  • #38 but this parameter is coming from the outside world, and we can’t trust it.
  • #39 We could instead the Foreign type…
  • #40 This type is defined using the foreign data interface and represents data coming from 3rd party services whose structure cant’ be guaranteed.
  • #41 This would not be useful if we couldn’t extract an User from it… that why we have the IsForeign type class. We need to provide an instance for our User type, if we want to use the read function to extract an user from the first parameter of our lambda.
  • #42 To define this instance of this type class, we need to change slightly the definition of our user type…
  • #43 And then we can write the implementation that will extract the name and age properties from the input.
  • #44 And return either an User or an error; this error could be a missing property, a property with incorrect type or… even... that the input is not a valid JSON.
  • #45 Now… we need to find a way to represent the type of the second element.
  • #46 Let’s call it Context…
  • #47 And define it using the foreign data interface. If you remember from the echo function, the 2nd parameter is a context object that is used to complete the request with a success or a failure. We can’t interact directly with that object, but we can define some functions that will help us to do so, using the foreign function interface.
  • #48 We will write this functions using Javascript.
  • #49 It seems sensible that the first parameter of these functions, would be the Context object.
  • #50 And the second is either an Error, when the request fails, or an object when the request is successful. But… which would be the output type?
  • #51 If we take a look to how these functions are implemented, we will see that they are not returning an empty object… and that they are performing side-effects. Purescript is a pure functional language and we would like to use a type that captures both the fact that we are side-effecting and the return type.
  • #52 For that we could use the Eff type constructor… this is a let’s say a function that accepts a row of effects as its first parameter and the return type as the second parameter, and produces a type that represent a native effect
  • #53 We can use this to represent the output of our functions...
  • #54 We just need to create an effect, that we will call LAMBDA...
  • #55 And use the type constructor to create a native effect that interacts with amazon lambda to produce a Unit. You can see here that we are using an extensible record to represent the effect... and that’s why we say that Purescript supports extensible effects.
  • #56 As the last thing our lambda function is going to do is to call the success or failure functions…
  • #57 We can assume that the output our lambda is going to be the same native effect.
  • #58 Fitting all pieces of the puzzle together… this is the source code of our time machine. As you can see we are not only peforming the LAMBDA effect but also the CONSOLE effect, as we are logging the result of processing the input object. That’s why our lambda has an additional effect in the output type.
  • #59 Finally… as purescript functions are curried by default, we need an adapter that would allow Amazon Lambda to call our function.
  • #60 If you find Purescript an interesting language, and want to learn more… I recommend you to check this book from the father of Purescript, Phil Freeman, that is available for free at leanpub.
  • #61 Thank you for your attention and... If you have any questions, now it’s the time for them.