Sonntag, 14. Juni 2009

Practically Functional

A very nice read up by Daniel Spiewak[1].
Scala combines OOP and FP. But how to use FP in practice? Practically Functional gives a fine introduction starting with the following "functional trademarks":

  • referential transparency
  • higher order functions
  • closures
  • immutability

Which then are described as "functional idioms" using a classification that looks like this:
Recursion -> HOF -> Combinators -> Monads
where each Level raises the level of abstraction.

Recursion
If functional programming does not allow loops then how do you do something repeatedly? The answer  is via recursion. To assist the programmer with that Scala offers nested methods.


Higher Order Functions
HOF are functions which take functions as their arguments. Some examples are:

  • foldLeft, foldRight ( catamorphism)
  • map ( "mapping" over a collection)
  • flatMap ( "map" and afterwards "flatten")
Combinators
There are three kinds of Combinators:

  • Sequential ( first a, then b)
  • Disjoint (either a or b)
  • Literal(exactly foo)
Monads
Properties:
  • Typ constructor
  • Single argument constructor
  • flatMap / >>= (bind)

It is really nice to see such eye opening examples which really help you to understand code better and still do the job:

def readPeople(files: List[String]): List[Person] = {
  for {
    file <-files
    props <-readFile(file)
    firstName <-props get"name.first"
    lastName <- props get "name.last"
    ageString <- props get "age"
    age <- toInt(ageString)
  } yield new Person(firstName, lastName, age)
}


[1] pdf file

Samstag, 13. Juni 2009

Ioke

Programming language, that seems like a abstract term because in most cases programmers just use it as a tool in their job. What motivations could be reason for creating a programming language.
Today I watched[1] Ola Bini talking about his creation Ioke. He discusses several properties of Ioke among them how close Ioke normal syntax is to its AST representation or Homoiconicity[2]

[1]http://blip.tv/file/2229441
[2]http://en.wikipedia.org/wiki/Homoiconicity

Freitag, 17. Oktober 2008

Clojure

Clojure is a dynmanic programming language which run on the JVM.  It is also a functional language which offers nice tools for concurrency handling. Since it doesn't give you the imperative fallback like scala it can be hard certain times to express a problem as your used to it. It is also a LISP which means it is essentially build upon a few primitives.


Counting lines in a file


(ns tokenize
(:import (java.io BufferedReader FileReader)))
;# This is a comment?
(defn process-file [file-name line-function counter]
(println " starting from imperative "
 (with-open [rdr (BufferedReader. (FileReader. file-name))]
  (reduce line-function counter (line-seq rdr) ))))
 (defn process-line [acc line]
  (+ acc 1))
(process-file "../../<filepath>" process-line 0)


Simple sum


(defn mysum [base arg1]
 (+ base arg1))
(println " works "(mysum (mysum 1 2) 4))
(time (dotimes [i 5] (println "woha!")))


line counting shorter


(ns tokenize
(:import (java.io BufferedReader FileReader)))
  (defn process-file [file-name]
   (with-open [rdr (BufferedReader. (FileReader. file-name))]
   (doseq [line (line-seq rdr)] (println line))))
(process-file "<filepath>")



Hello Clojure


(println "Hello, world!" (.. System (getProperties) (get "os.name")))
Since Clojure is a functional language and don't offer imperative constructs like loops it takes a bit to get used to but it is definitely  worth a first look. Evaluation concerning how it works out, building bigger systems and what performance issue possibly arise is of course a matter of further investigaion.

Samstag, 16. August 2008

functional vs. imperative

Today I came across this post[1]. It discusses about readability, comparing functional and imperative code snippets. I think its quite hard to tell which reads better because it depends on you think it should work. I guess this shows how important code style guides are when it comes to big scala code basis, involving possibly many different team members.


[1]http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/

Dienstag, 12. August 2008

Scala functions

Scala offers so many different concepts that it sometimes can be quite overwhelming. Scala blends object orientated and functional programming. The very basic block of functional programming is of course a function. Now a functions seem similar to a method but a function in scala can be a value. Today I found another usefull resource from the blogsphere[1]. The essential points discussed:

  • the apply method
  • Closures
  • Partial Functions
  • apply (ied) in many places


[1]http://creativekarma.com/ee.php/weblog/comments/scala_function_objects_from_a_java_perspective/

Samstag, 28. Juni 2008

ECF Team Editor

Today I found[1] out about a very nice tool for cooperative programming. It is based on eclipse and I used the following to get started:


  • eclipse 3.5
  • ECF 3.0 through update
  • google mail account
  • open communications perspective
  • login with google mail 
  • add contact
  • project with same: name, package and files
  • share workspace to collaboration
  • host: share editor with

This enables two computers running eclipse to share a common code editor. It even offers simultaneously coding in the same file! That's pretty amazing. I don't know exactly how this could be used in a professional environment but for a quick hack session this seems ideal.

[1]http://live.eclipse.org/node/543
Some further references on the topic:
http://wiki.eclipse.org/DocShare_Plugin
skype provider
skype provider documentation

Sonntag, 1. Juni 2008

more on Scala

What is interesting about Scala that it offers a lot of powerful features.

Context Operations
Since Methods can be used in infix notation and you can use symbols as method names (like *,/,+,-) , it is important to view them always in context or to be specific in scope of the current code.

Many of those features derive from functional programming. Which seems a vast space to explore. But for now I may start with closures.


No Statics
Scala is in fact more OO than Java, it removes statics completely and introduces another construct the 'Object'. By defining an object in scala you define an single instance module or singleton, which can be referred to without using an constructor.


Closures and Functional Programming
So I stumbled about this blog post[1].  While it only started with the title Closures, it offers quite more than just that. The post discusses concepts which define algebraic structures, e.g.: associativity. The MyClass example seems simple and compelling. In quite a different manner (than so far for me) the author describes functional programming and how it can be useful. I really like that. Functional programming is a broad topic and this particular spot on helped me to get started.

Some more advanced topics from functional programming would include topics like: Monads, Applicative, Arrows and Zipper. Some useful links:

[1]http://aabs.wordpress.com/2008/05/29/functional-programming-lessons-from-high-school-arithmetic/
[2]http://debasishg.blogspot.com/2008/03/monads-another-way-to-abstract.html
[3]http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.html