views:

288

answers:

5

Dear gurus,

I was recently thinking how I'm not always using the beautiful concepts of OO when writing Pythonic programs. In particular, I thought I'd be interested in seeing a language where I could write the typical web script as

# Fictional language 
# This script's combined effect is to transform (Template, URI, Database) -> HTTPOutput

HTTPOutput: 
    HTTPHeaders + Maintext

Flags:                              # This is a transform URI -> Flags 
    value = URI.split('?').after
    refresh = 'r' in value
    sort = /sort=([a-z])/.search(value)

HTTPHeaders:                       # This is a transform Flags -> HTTPHeaders
    'Content-type:...' +  Flags.refresh ? 'Refresh: ...' : ''

Maintext:
    Template.replace('$questions', PresentedQuestions [:20] )

Questions:
    (Flags.sort = 'r') ? RecentQuestions : TopQuestions 

PresentedQuestions:
    Questions % '<h4>{title}</h4><p>{body}</p>'

RecentQuestions:
    Database.Questions . sort('date')  

TopQuestions:
    Database.Questions . sort('votes')

See what happens? I am trying to make as many objects as possible; each paragraph declares something I call transform. For example, there is a transform HTTPHeaders. In an imperative language that would be a declaration of class, object and function combined:

class HTTPHeaders_class
{
     public char* value
     HTTPHeaders_class() 
     {
         value = ... + Flags.refresh ? + ... // [1] 
     }

}

class Flags_class 
{
     public char* flagstring;
     public bool refresh;
     ...
     Flags_class() 
     {
         value = ... /* [3] */ 
         refresh = ...
     }
}

Flags = new Flags_class (URI)
HTTPHeaders = new HTTPHeaders_class (Flags)   // [2]

However, I want to have no way to specify that an object should change unless the inputs from which the objects is made change; and no way to have side effects. This makes for a drastic simplification of language. I believe this means we're doing a functional programming ("a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data").

I certainly try to use things like Python classes, M-V-C framework and Django (thanks to the answer), but I don't think they have the concepts above and below.

  1. Each object has a value field that can be referred just by writing the class name.
  2. If HTTPHeader is referred somewhere, this means that a static, unchangeable object HTTPHeader is created as soon as possible. All references to HTTPHeader then refer to this object.
  3. Suppose I want to repeat the program with the same URI object while the interpreter is still in memory. Since Flags depends only on URI and HTTPHeaders only on Flags, those are not recalculated. However, if Database is modified, then Questions need to be recalculated, and thus the HTTPOutput may change too.
  4. The interpreter automatically deduces the correct sequence of initializing the classes. Their dependency must form a tree for that to happen, of course.

I believe this will be a useful models for programs like web scripts where there are no side effects. Is there a useful language where one writes program similar to this already?

+4  A: 

If you really want to delve into web application development with Python, then look at Django. You are better off using a MVC architecture in this case and Django does a very nice job of supporting MVC applications.

What you are probably interested in is more of a Declarative programming approach than a functional one. Functional programming is more concerned with mapping an input to an output as a pure (mathematical) function. The declarative approach is all about stating what should happen instead of how to do it.

In any case, dig into Model-View-Controller and Django. You will probably find that it fits the bill in a completely different manner.

D.Shawley
Ye, Python classes and Django cover much of what I trying to say... Though I believe they are stateful?
ilya n.
You Django response is created by a function -- you can use any programming style you want for that function. If you want stateless FP-style transforms, that can be made to work.
S.Lott
Yes, but if I am a function and the compiler doesn't know I am pure, he won't know that I am thread-safe and cacheable.
ilya n.
+1  A: 

I don't think it's exactly what you are looking for but Scala tries to integrate OO and functional features under a common language.

Scala covers such a wide range of language features that I'd be really surprised if you didn't find the language features you want--but possibly not a framework you find acceptable.
Bill K
I'm looking into Lifr, http://demo.liftweb.net/interactive
ilya n.
+3  A: 

Take a look at F#. It is specifically designed as a functional language (based on OCaml) with OO support utilizing the .NET stack.

bsruth
Interesting, though not purely functional.
ilya n.
I think you're being a little too picky. ;)
musicfreak
+1  A: 

Your code looks like a DSL for web applications and Sinatra is such a DSL. Sinatra does not do exactly what you do there but it's in the same ballpark. http://www.sinatrarb.com/ - it's written in Ruby but hey, let's all be friends here in dynamic languages land.

Jonas Elfström
I'm all for Ruby, just don't know it yet :)
ilya n.
+1  A: 

This actually feels very much like Haskell, except that you're not using pure functions here. For example, Flags doesn't have the URI passed into it; URI is a separate definition that is presumably not producing the same URI every time it's called, and so on.

For URI to be a pure function, it would have to have a parameter that would give it the current request, so that it can always return the same value for the same inputs. (Without any parameters to work on, a pure function can only return the same result over the life of a closure.) However, if you want to avoid explicitly giving URI a parameter every time, this can be done with various techniques; we do this with monads in Haskell.

It seems to me that the style of programming you're thinking of might be based on "combinators," having small functions that are glued together inside a framework to produce a large, complex function that does the overall processing.

Curt Sampson
URI could be function but my language would automatically cache its value the first tie its called and never recalculate it again!
ilya n.
That's called memoization, and is indeed a common technique in many languages. I expanded the answer a bit to talk about pure functions in more detail.
Curt Sampson