Deploy early and often I want to send this into ‘PROD’ bc running locally is only ½ the battle or less After spinning my wheels reading a dozen things. Including using a makefile? oh boy. I needed to reset. Pretty sure I got wrapped up in the ‘what i don’t know’ and lost sight of the “oh yeah”, i’ve done something like that a bit, right? Resources zeit Creating your own Scotty Haskell Dockerfile failed makefile fpcomplete building haskell with docker Scotty Example with DB docker slimming multistage Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
Returning actual Data in Json Building out some hardcoded users need to supply User before the data don’t forget the = sign We could manually build the encode and decode to/from JSON, but Generics, lets you automatically do things based on the data type adding {-# LANGUAGE DeriveGeneric #-} and import GHC.Generics don’t forget to add instance instance ToJSON User instance FromJSON User Now we are able to see the output of having the generic encoder Ok, two modules loaded. *Main Lib> import Data.Aeson (encode) *Main Lib Data.Aeson> encode bob "{\"userName\":\"Bobby Boy\",\"userId\":1}" And now with that it looks like Scotty has a function json for use to return a json payload. sweet get "/users" $ do json allUsers Resources Following this guide a bit Aeson Scotty Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
tackling route params This is really nice that it’s a lot like express We can add route parameters with :xxx in the url, just like Sinatra and express. To read it in our handler, we use the param function. Edit the /hello route to be this get "/hello/:name" $ do name <- param "name" text ("hello " <> name <> "!") Tried this, got some errors. realized that Haskell has a ton of ideas about strings. Like Text. Text errors ‘Text’ is defined in ‘Data.Text.Internal’ ‘Data.Text.Internal.Lazy.Text’ is defined in ‘Data.Text.Internal.Lazy’ Resources Guide Text Types Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
Building on our Server API, we want to break down the function called by the route into something more compose-able Route Type get :: RoutePattern -> ActionM () -> ScottyM () That’s a get request Resources docker quick Sean Hess original post Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
So, following the book is hard. I don’t really learn well that way. I have just enough knowledge that the first part feels boring and repetitive, but I don’t know what I don’t know, so where do I pick up, chapter 2 or maybe 3? So let’s take this from a real JS perspective. Building an API So we know how to get and return json with HTTP requests in Node, how do we do it in Haskell? Ok, now build on that. Make it something relatable. stack new hs-webapi Go into /package.yaml and under dependencies add ‘scotty’ and ‘aeson’ Then run stack build … then wait (forgotten the compile times :eye_roll:) also 67 dependencies. I mean it’s not 867!!!! ..still waiting. Resources Building an API scotty Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
03: Lists Handling Lists Before we get started a couple tips for shorthand in Prelude * > GHCi is an interactive Haskell REPL (Read-Eval-Print-Loop) that comes with GHC. At the GHCi prompt, you can evaluate expressions, load Haskell files with :load (:l) (and reload them with :reload (:r)), ask for the type of an expression with :type (:t), and many other things (try :? for a list of commands). * using let in the GHCI Words are are a list of chars In Haskell, lists are a homogenous data structure. It stores several elements of the same type. One of the most basic data types in Haskell List comprehensions? Like set comprehensions, normally used for building more specific sets from a larger general set * List Constructing we should mention type signatures too NOT AN ARRAY ugh, what’s this mean intListLength (x:xs) = 1 + intListLength xs Resources CIS 194 Real World Haskell Learn You a Haskell Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
02: Haskell Tools Install stack looks to be the sort of NPM of haskell reproduceable builds, YAML file config. cabal config too Command Line stack build and then stack exec hello-world-exe the build will create an executable in the ./stack-work and look there for executables. cool The Setup.hs file is a component of the Cabal build system which stack uses. It’s technically not needed by stack, but it is still considered good practice in the Haskell world to include it. The file we’re using is straight boilerplate: Ok so cabal is a build system that stack uses, but good practice. It is also important to remember that stack is built on top of the Cabal build system. Therefore, an understanding of the moving parts in Cabal are necessary. In Cabal, we have individual packages, each of which contains a single .cabal file. The .cabal file can define 1 or more components: a library, executables, test suites, and benchmarks. It also specifies additional information such as library dependencies, default language pragmas, and so on. So it looks like cabal is the actual package builder and stack is a sort of management tool for haskell language, cabal, and all the other bits/pieces Which ghc is it using? jtomchak@MeowBook:/c/Users/jtomc/Documents/Haskell/hello-world$ stack exec -- which ghc /home/jtomchak/.stack/programs/x86_64-linux/ghc-8.4.3/bin/ghc Macros ? {-# LANGUAGE OverloadedStrings #-} not sure what the line means ? appears to be some sort of compile macro ? Resources * [Install](https://docs.haskellstack.org/en/stable/install_and_upgrade/) Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
Episode 1: * Learning path github.com/bitemyapp… * Functional * Two values of functional * Functions are first-class, that is, functions are values which can be used in exactly the same ways as any other sort of value. * The meaning of Haskell programs is centered around evaluating expressions rather than executing instructions. * Pure Haskell expressions are always referentially transparent, that is: * No mutation! Everything (variables, data structures…) is immutable. * Expressions never have “side effects” (like updating global variables or printing to the screen). * Calling the same function with the same arguments results in the same output every time. * Lazy * Statically Typed * Helps clarify thinking and express program structure * Serves as a form of documentation * Turns run-time errors into compile-time errors * Themes Types Abstraction Whole meal programming Follow JavaScript to Haskell Twitter: @jstoHaskell() Site: JSToHaskell microblog: @jtomchak Jesse Tomchak Twitter: @jtomchak
We cover the why of trying to move into Haskell, the format of the show, and what to expect moving forward.