Code Monkey home page Code Monkey logo

func-architecture's Introduction

Functional Programming Patterns

(For the pragmatic programmer)

In this talk we will see a pragmatic approach to building a purely functional architecture that delivers cohesive functional components. We will cover functional patterns such as Free Monads, Transformers, Kleisli arrows, dependently typed checked exceptions and types as well as how they can be glued together to achieve pure functions that are composable, context free, dependently injectable and testable.

Download the presentation

  1. Interactive : Clone this repo and run sbt console
  2. PDF Download
  3. MD
  4. SpeakerDeck

Ultimately one can achieve functions whose dependencies are injected, free interpreted and potential exception may be accumulated and simplified through the use of monad transformers:

Given some dependencies and a module containing them:

case class Converter() {
  def convert(x: String): Int = x.toInt
}

case class Adder() {
  def add(x: Int): Int = x + 1
}

case class Config(converter: Converter, adder: Adder)

val system = Config(Converter(), Adder())

We can define some functions defining deps as input params, that perform some ops and capture potential Exceptions.

def service1(x : String) = Service { converter: Converter =>
    ask(Result.catching[NumberFormatException](converter.convert(x)))
}

def service2 = Service { adder: Adder =>
    ask(Result.catching[IllegalArgumentException](println("serv2 invoked"); adder.add(22) + " added "))
}

We may compose our monadic functions and lifting potentially dispair dependencies to a common module

val composed: Service[Config, String, NumberFormatException with IllegalArgumentException] = for {
  a <- service1("1").liftD[Config]
  b <- service2.liftD[Config]
} yield a + b

And run them through interpreters that replace our Free Algebra by actual types separating interpretation completely from program definition

composed.exec(system)(TestInterpreter) // yield the Id[Value]

composed.exec(system)(ProdInterpreter) // yields a `Task` or `Future`
``

func-architecture's People

Contributors

raulraja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.