简体   繁体   中英

Using functional language concepts with OO - is there a language?

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, MVC 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?

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.

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

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.

I see my favourite language has not been mentioned yet, so I'd like to jump in and suggest Dyalog APL as a language for 100% function programming. APL has a looong history and was developed when there was no Internet - but Dyalog is the most active provider of APL-Implementations and they also have a fully function webserver that is available free of charge. (The interpreter is also available free of charge for non-commercial use.)

我不认为它正是您正在寻找的,但Scala尝试在一种通用语言下集成OO和功能特性。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM