Monday, November 19, 2012

Monads, shmonads and functional programming


I will be necessarily somewhat long, somewhat wrong and mostly fuzzy, given my own level of understanding, but I am not one to shy away from making a fool of oneself, in the quest for knowledge.

Monads (and related concepts from category theory, like Functor, Applicative etc) are the next level of abstraction in Functional Programming, beyond functions.

Functions

Functions f : A => B allow you to think and program a certain way. Once you get used to this functional programming, you can move on to higher types, higher functions and from there to monads.

A function is basically a transformation: it turns an A into a B. In functional programming, one can take it and do stuff with it.

Functors

Functors F[A] lift a  simple function to something more - the signature tells you their secret:

map[B] (f : A => B) : F[A] => F[B]

... they essentially gobble up your function f and return something higher level - pause and reflect on this for a second.

This map or fmap, you could think of it as taking a simple unix command like "wc -l" and apply it to something, as in "find *.txt | wc -l"

In scala, you are used to a slightly different version of the same idea:

List(1,2,3) map (_ + 4)

Why are they useful? Many reasons

For instance – you are normally ‘forbidden’ from using state between two calls to f – you should know that by now. However, inside a functor you could, for the duration of the entire transformation F[A] => F[B]… you could have some state there… this is the beauty of an internalized iterator versus the one you’re used to. That state would be well encapsulated there in that transformation, so it could be used.

How that transformation is executed, is up to the specific functor you use. Some can optimize it, some can be dumb while some can use state (like cache a DB connection between calls or whatever).

Let’s have a quick random example: I could have my own functor, working on lists, which keeps the elements sorted. If I apply a random function to it, the result has to be also sorted. You can see the problem? my functor will keep it sorted while an externalized loop may or may not keep it sorted, depending on the programmer.

MySortedList (1,2,3) map (rand(_))will always be sorted, while List (1,2,3) map (rand(_)) is not…

Without functions and functors, there is no way you could express and enforce that idea – I don’t think…

Monads

Monads go even further. Monads have certain laws which give them certain properties which are very useful once you get used to thinking in those terms.

Monads use flatMap rather than map, with a signature

flatMap (f : A => M[B]) : M[A] => M[B]

As you can see, they also return a transformation, but a yet higher level one, which includes flattening. But because it includes flattening, it can do it in whichever way it wants.

Why monads are more useful than functors – look at the gobble-able: it’s an f : A => M[B] rather than f : A => B – the functors limit you to the shape of the functor, sort of speak – basically if you start with a list of ID’s you will end up with a list of Johns of the same size (or more or less, if the functor is cheating).

Monads are one better, you can start with a list of 5 student ID’s and end up with either 45 grades in a school year or 2 missing registrations… yes, f : A => B has to return exactly one and the same B for an A, while an f : A => M[B] could return for instance empty (called unit) he he…

Keep reading

There’s a lot more to it, as others are trying to convey – try to read as much as you can – there’s no one angle that makes it easy to jump to monad abstractions…

If you are confused by the signatures I used above, it’s ok – no, you don’t have to learn Haskell – there’s an entire series of blog posts to explain the gap...

Note that the random sample above is not kosher, since rand() is not a pure function: it never returns the same B for a given A... but it makes a good point.

Saturday, September 29, 2012

OMG, scala is a complex language!

This was originally posted in Oct 2011 on the blog.coolscala.com

I keep seeing this and, maybe it’s true. Let’s chase this complexity for a bit and go through some of the biggest scala differentiators (from Java or C++, as major OO languages).
Smart compiler infers types
The compiler can infer the types for the most part, so people have to type a lot less repetitive information, which they used to type in both Java and C++. For instance, since “john” is obviously a String, the type of the variable is inferred by the compiler to be String so I don’t have to type it again:
val someone = “John”
I have seen this feature both praised and held against the language as “added complexity”, so I don’t know what to say. I just love typing less and feeling less stupid, every time I declare a value or variable.
Simplified class definitions
Since OO is all about defining classes, scala made do with a bunch of stuff in one go, so that my domain models are dead-simple:
class Person (val firstName:String, val lastName:String)
This, in Java and C++ takes about one page of code: with constructors, getters, setters etc. Scala observed that people don’t need to type one page to inform a stupid compiler that they want a person with a first and last names, so it’s all condensed in this one line, much like a table would look in SQL.
Is this added complexity? Well, I do need to worry about overriding the generated getters/setters ONLY if I need to, so I don’t really know if it’s more complex.
Me? I love this particular feature so much, that honestly, I don’t care what you think J
Unifying methods and operators
All programming languages I know discriminate between methods with names like “append” and operators like “+=”. Some do not even allow re-definition of some hardcoded operators (Java) while some allow infix notation only for operators (C++).
Scala simply makes do with ALL these restrictions and states that the name of a method can be pretty much anything and all can use the infix notation, so I can have:
ListBuffer(1,2,3) append 4
As well as
ListBuffer(1,2,3) += 4
The only difference would be the precedence rules, which are customary in all languages.
Some people obviously would see this as “more complex” than both Java and C++ since they can now do whatever they can… but I see it as “simpler” than both. Operators have been held against C++ before so it really is not surprising that they are held against scala as well.
This is, after all, what makes scala such as perfect DSL framework, allowing natural language such as:
“Mary” with “a purse” and “red shoes” should look “nice”
Types – variance
In both Java and C++, the generics have certain hard-coded and limited behavior (i.e. non-variance) and allow only a few constructs (like List<T extends Person>).
In scala, there is a default behavior, where List[Person] is non-variant, but everything is customizable. If you want co-variance, just tell the compiler List[+Person] or List[-Person] for contra-variance. Just like Java, I can use List[T <: Person] but I can use the reverse just as well: List [T >: Person].
Since scala supports implicits (with finer control than C++), another construct is available: List[T <% Person].
Is this more complicated? Well, just like the operators – it lifts certain limitations of other languages, so it’s both more complicated, since there’s more stuff to learn and simpler, since there’s less rules to live by.
I personally enjoy the extra control… do I actually use it? Not on a daily basis, the defaults are good enough.
Constructors only?
Most languages only allow data types (objects) to be constructed. This is normal in Java and C++.
Well, there’s the flipside, where I can de-construct an object and I don’t mean de-allocating its memory. Consider this:
someone match {

case Person(first,last) => println (“ name is “ + first + ” “ + last)

}
You can see what I mean by de-constructing: took an already created object, someone, and de-constructed into its components. I know this looks foreign to most OO personnel, but trust you me, it is insanely cool and useful. Think what you would have to type in either Java or C++ to achieve the same thing, with if (instanceof) and then type cast and assign two variables and whatnot.
Is this more complex? Well, this is totally new functionality so I guess it is. But I love having it! Trust me, you will, too!
By the way, the match/case construct is way more powerful than your regular switch/case which can only handle constants… we can de-construct types, match constants, match types… and more! Check this out:
someone match {

case Person(first,last) => println (“ name is “ + first + ” “ + last)

case “John” |”Mary” => println (“hello, “ + someone)

case s:String => println (“name is “ + s)

case _ => println (“don’t know what this is…”)

}
Is this more complex? I don’t know… in Java or C++ this is between one and two pages of code. This looks simpler and more intuitive to me… granted, I got used to it but so can you!
Conclusion
There’s more areas of the language, but these are some of the major differences I have time for right now. If you have others, post up and I’ll get into those as well.
I did not get into the functional areas of the language, since that would require comparing with other functional languages and I’m not an FP guy. C++ comes close by allowing passing pointers to methods to other functions while Java 8 I think has some proposed lambda syntax.
Is it more complex? Well, there’s two ways to look at it:
Yes,
  • There are more symbols and features that one can use
  • There’s more computer science I need to learn (contra-variance, pattern matching, lambdas, closures etc)
No,
  • The real-world problems to solve are the same and
  • To do the same in either Java or C++ is either impossible or takes many times more code… and uglier code at that
What do I think? I don’t really care. To me it was cool to learn these concepts that I had forgotten since university and my new vocabulary allows me to solve the usual problems in just a few lines of code and head for an early lunch, while my mates are still writing some getter or setter…
What do you think?

P.S. This is a great detaiked discussion of complex vs complicated: http://lamp.epfl.ch/~odersky/blogs/isscalacomplex.html

Quick scala – step 1: setup

This was originally posted in Oct 2011, on the blog.coolscala.com which I'm removing.

If you just want to play with scala quickly – you can use one of the few online interpreters, like http://www.simplyscala.com/or http://www.tryscala.org - these let you run small bits of code interactively. Otherwise, continue reading, to complete a local setup for scala development. You need these:
1.Java
2.Scala (command line)
3.Eclipse with the scala plugin
Installing Java:
1.Download and install a 1.6 JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html . Do not use a 1.7 for this exercise and make sure you install the JDK not just the JRE. The exact link I use is: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u27-download-440405.html
a.I recommend you use a 64 bit version, so you can allocate more memory to it
2.Run and install somewhere, let’s say C:\bin\jdk1.6.0_27\
a.I found it a good practice to keep my development tools in c:/bin
b.Make sure to check the “change destination folder” checkbox before letting the installer run…
3.Make sure the path to java.exe is in the PATH
a.Add C:\bin\jdk1.6.0_27\bin to the PATH system variable
b.Define a variable called JAVA_HOME with the value “C:\bin\jdk1.6.0_27\”.
4.Test it by opening a command line window and typing “java”.
Installing scala command line:
1.Download a distribution from http://www.scala-lang.org/downloads
2.Unpack it in a folder, let’s say C:/bin/scala
3.Make sure the path to scala.bat is in your PATH
a.Add C:/bin/scala/bin to the PATH system variable
4.Test it by opening a command line window and typing “scala”. You should get the scala interpreter prompt and then type“exit” to exit the interpreter.
Installing Eclipse:
1.Download Eclipse Classic from the download site: http://www.eclipse.org/downloads/packages/release/indigo/r
2.Install it (unzip it) in C:/bin/eclipsescala
3.To make sure it uses the JDK you just installed (if you went that route) modify your eclipsescala/eclipse.ini and add this line:
a.-vm
C:\bin\jdk1.6.0_27\bin
4.Start it up (eclipsescala/bin/eclipse.exe)
5.Install the scala plugin
a.Go to Help/Install new software
b.Click on Add
c.Type in name “scala291” and the url: http://download.scala-ide.org/releases-29/2.0.0-beta
d.You can see if there is a newer version at http://www.scala-ide.org/
e.Select this source in the drop-down
f.Click “Select All” to select the three features
g.Go ahead with the installation
6.Restart Eclipse
7.Read the detailed setups at http://www.assembla.com/spaces/scala-ide/wiki/SetupIt is a good idea to allocate Eclipse at least 1G – I use 2G.
8.If you changed any settings, restart Eclipse
To test your eclipse installation, select File/New/Project and see if “Scala Project” is in the list. You’ll use it for the “Hello World”exercise.
Check that the sources for the scala library are setup properly:
1.Open your project
2.CTRL+SHIFT+T to find types and type ‘List’
3.Scroll down the list to find the List from scala.collection.immutable
4.Click enter – you should now see the nicely formatted source code for List
5.You can take a quick look but don’t despair if that code looks foreign – we’ll get there soon enough.
Spruce up your Eclipse – my favorite plugins include:
-The GOTO plugin from, http://muermann.org/gotofile/
-The Color themes plugin, from http://www.eclipsecolorthemes.org/
-The VI plugin, from http://www.viplugin.com/viplugin/
Cheers – you are now setup to play with scala.

Friday, August 24, 2012

Racer kidz and wiki clubs - update

My little R&D into wiki domains at http://www.racerkidz.com seems to work fine - I add a feature evry now and then.

You can register a club, describe a racing schedule, then racers can add it to their profile and automatically, via the magic of wiki xpath, see the racing calendar and the map...

You can give it a try without an account from the home page.

I also experimentally used it to create simple blogs, like this enduro school. A blog has posts and that's that, see its domain definition.

Cheers!

Sunday, March 28, 2010

Killing an annoying warning

The most annoying warning ever has been haunting me for a while.

[javac] /home/razvanc/workspace7u6/razxml/src/razie/base/data/RiXmlUtils.java:160: warning: com.sun.org.apache.xpath.internal.objects.XObject is Sun proprietary API and may be removed in a future release


I refuse to include and move around large unnecessary libraries to deal with XML and XPATH just because Sun considers themselves the center of the universe and their internal libraries somehow more important then mine. In short, they figured there should be no way to remove this warning, although, in this case, they copied code from apache into their own libraries.

Well, the solution is rather simple. Most likely everyone has specific XML wrappers. Just put them in their own project, disable the java/scala builders. Then, build it manually, create a jar file and check it in.

Then, in all the other projects, use this jar directly. Since the jar is already compiled, the annoying warning is no more.

See for instance my xml utilities, at http://github.com/razie/razxml

What's more, I don't actually use the XML stuff directly. I wrapped it all in pretty much one single class, using XPATH for access. It's simple, fast enough and makes for very simple code:


Reg.doc(MediaConfig.MEDIA_CONFIG).xpa(
"/config/storage/host[@name='" + Agents.me().name + "']/media/@localdir"
)


For all xml data access, you should limit yourself to the xpe/xpl/xpa methods described in an earlier post (http://blog.homecloud.ca/2010/02/one-xpath-to-rule-them-all.html). So, use something like this instead: http://github.com/razie/razxml/blob/master/src/razie/base/data/XmlDoc.java

Cheers.

Saturday, March 13, 2010

The Option monad pattern thing

This informative post is consolidated in the new blog, at:

 http://www.racerkidz.com/wiki/Blog:Cool_Scala/Post:The_Option_monad_pattern_thing

... where we take a closer look at the Option type of Scala, the monadic aspect of it and see how we can use it to inspire new patterns and new ways of looking at the code we write.

(Sorry - original content was removed from here, to avoid duplicates).

Monday, March 1, 2010

Scripster - interactive scala REPL using telnet, http etc

As mentioned before, I think that all apps must alllow scripted/programatic access to their objects.

I created a simple interactive gate to acces the scala REPL in any scala process. It only uses one port and supports telnet, http and swing:



The graphics are based on my 20widgets project, with the addition of a ScriptPad widget.

Content assist is only available in the telnet version (sic!). I will work to find some nice syntax-colored controls for the web version. Switch to character mode ("mode character" on ubuntu or default on windows) and use TAB to see a list of options. Right now it's a demo only, I need to find the parser's APIs to get the real content assist options ;)

You can download the single scripster-dist.jar file from my razpub project download, at http://code.google.com/p/razpub/ and run the example with this command line (replace /host/bin/scala with your $SCALA_HOME) :

java -classpath ./scripster-dist.jar:/host/bin/scala/lib/scala-library.jar:/host/bin/scala/lib/scala-compiler.jar:/host/bin/scala/lib/scala-swing.jar razie.scripster.JScalapSwing


To use it in your process, put these jars in your classpath and use this:
razie.scripster.Scripster.create(4445)
where 4445 is the port you want to use, see the razie.scripster.MainScripster for an example. To enable the swing version, use the swing jars as well and see the razie.scripster.MainSwingScripster class for an example.

For further tweaking, look at the code yourself, at http://github.com/razie/scripster/tree/master/src/razie/scripster/

If you want to keep in touch with the evolution of scripster or my other related endeavours, subscribe to the RSS feed or twitter/razie.

Scripster's main page, kept up-to-date, is at http://wiki.homecloud.ca/scripster. There's also an online live demo at http://scripster.codewitter.com.

Enjoy!