Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Brighton Meet up: what functional programming means to me.

Functional Brighton Meet up: what functional programming means to me.

Ff619670d30ebdeefd49cf10af8e3292?s=128

Richard Dallaway

September 27, 2011
Tweet

More Decks by Richard Dallaway

Other Decks in Technology

Transcript

  1. Three things I’ve noticed about functional programming while using Scala.

  2. happy birthday to me happy birthday to me #lol #omg

    oops #lol oops #lol #omg 㱺 㱺 ⋮ ⋮ What have we have? What do we want?
  3. #lol, #omg & some text Some text 㱺 What have

    we have? What do we want?
  4. #lol, #omg & some text Some text 㱺 What have

    we have? What do we want? List[String], String 㱺 String
  5. def appendTags(tweet: String, tags: List[String]) = tags.foldLeft(tweet) { appendOne }

    def appendOne(tweet: String, tag: String) = if (tweet contains tag) tweet else tweet+" "+tag
  6. “It’s the mutable state, stupid.” – Göetz et al.

  7. val results = List(yahoo _, google _).par.map(_.apply)

  8. import scala.actors.Futures._ val results = List(yahoo _, google _) map

    ( f 㱺 future{ f() } ) map (_.apply)
  9. Option, for comprehensions, flatMap & pattern matching are astonishingly useful

    every day and I’d never heard of them before learning Scala.
  10. for (int i = 0; i < resultFeed.getEntries().size(); i++) {

    ContactEntry entry = resultFeed.getEntries().get(i); if (entry.hasName()) { Name name = entry.getName(); if (name.hasFullName()) { ...
  11. for { entry ← resultFeed.getEntries name ← Option(entry.getName) full_name ←

    Option(name.getFullName) birthday ← Option(entry.getBirthday) } yield "%s: %s".format(full_name.getValue, when)
  12. 1. Thinking in terms of A 㱺 B 2. Concurrency

    3. Option (& friends) change everything Summary
  13. StringBuffer b = new StringBuffer(tweet); if (tags != null) {

    for(String tag: tags) { if (tag != null && b.indexOf(tag) == -1) { b.append(tag).append(“ ”); } } } return b.toString();