• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Groovy Monads
 

Groovy Monads

on

  • 3,793 views

 

Statistics

Views

Total Views
3,793
Views on SlideShare
3,709
Embed Views
84

Actions

Likes
4
Downloads
10
Comments
0

2 Embeds 84

https://twitter.com 83
http://www.linkedin.com 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Individual -each has its own identityAutonomic and independent - interactions are only apparentCan change itself according to some intrinsic laws -but the change comes from insideHierarchical and aggregated – complex monads can be composed from simple monads

Groovy Monads Groovy Monads Presentation Transcript

  • Yetanotherpresentationon MONADS
    MarcinGryszko
    @mgryszko
  • whyam I doingthis?
  • accordingto Brian Marick…
    http://www.vimeo.com/20717301
  • Thereisanancient Mayan profecy
    thatstatesthatsomedayevery single human onearthwillhave done a tutorial aboutmonads
    and onthatpointthepurpose of theuniversewillhavebeenachieved
    and allhumanswillascenttoparadise
  • I’ll try to do my bit
    so thathumanity can reach nirvana
  • origins
  • XVIII century
  • Gottfried
    1714
    Monadologie
  • eternal
    indecomposable
    individual
    hierarchical and aggregated
    autonomic and independent
    can change itself according to some intrinsic laws
    infinite
    space, time matter, and motion are phenomenal
    atom
    human being
    soul
    god – first monad
    ultimate elements of the universe
  • XX century
  • categorytheory
  • abstractview of mathematical concepts (which are already quite abstract )
  • categories have:
    objects
    morphisms or maps or arrows
    see a relationship with
    classes, objects and methods?
  • defines monad
    howitisrelatedtofunctionalprogramming?
  • functionalprogramming
  • what are monads?
  • containers
    generalized interface for sequential computations
    pattern for transmitting state using functions without mutation
  • what defines a monad?
  • monadictypeM
    java.util.List
    unitoperation
    value -> monad
    constructor/factory method
    [1, 2, 3] (Integer -> List<Integer>)
    bindoperation
    monad -> next monad, exposing its internal value for a transformation function
    any method in Groovy taking a closure as param
    [1, 2, 3].bind({ x -> [x, x + 1] })
    == [1, 2, 2, 3, 3, 4]
  • what does bind?
    (typically)
  • Take a function and apply it to all values inside the monad
    Each invocation returns monad M<U>
    defintermediateMonads =
    collect { x -> f(x) }
    Extract U values from all intermediate M<U> monads and assemble the final M<U> monad
    intermediateMonads.flatten()
  • monadic laws
    (or when a monad is a monad)
  • 1. identity
    m.bind { x -> unit(x) } ≡ m
    transforming to unit doesn’t change a monad
  • 2. unit
    unit(x).bind(f) ≡ f(x)
    unit must preserve the value inside the monad
  • 3. associativity
    m.bind(f).bind(g) ≡
    m.bind{ x -> f(x).bind(g) }
    order of monad composition doesn’t matter
  • monadic zeros
    (optional, empty monad)
    []
  • 1. bindingwithzero
    m.flatMap({ x -> mzero})≡ mzero
    2. unit
    mzero.bind(f) ≡ mzero
    3. plus
    mzero.plus(m) ≡ m
    m.plus(mzero) ≡ m
  • examples
    ListAsMonad
    IO
    State
    Result
  • sources
    https://github.com/dsrkoc/monadologie
    http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.html
    http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html
    http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-3.html
    http://james-iry.blogspot.com/2007/11/monads-are-elephants-part-4.html
  • Thank you!