What language features can I add to Clojure through libraries?

For example, pattern matching is a programming language feature that can be added to clojure through macros: http://www.brool.com/index.php/pattern-matching-in-clojure

What other language features can be added to the language?

+4
source share
3 answers

There are two examples at the top of my hat, but I'm sure there are more.

+2
source

I think this is a stupid question to ask, what can I add, what should you ask, this is what you cannot add. Macros allow you to connect to the compiler, which means you can do almost anything.

You cannot currently add your own syntax to the language. Clojure does not have a custom reader extender, which means that you do not have any reader macros ( http://dorophone.blogspot.com/2008/03/common-lisp-reader-macros-simple.html ). This is due not only to the technical problem, but also to the wide variety of Rich Hickey (creator of Clojure).

What you cannot do is implement functions that need support for virtual mache, for example, add tail tail semantics or goto .

If you want to see some things that were done: Is there a Clojure DSL?

Please note that this list does not correspond 100%.

Edit:

Since you seem to be using a match pattern (this is a really good example for macros), you really should look at the match library. His probe is Clojure's fastest pattern matching library. http://vimeo.com/27860102

+1
source

You can effectively add any language features that you like.

This follows from the ability of macros to create arbitrary code at compile time: as long as you can figure out what code you need to generate to implement your language functions, this can be achieved with macros.

Some examples I've seen:

  • Query Languages ​​( Korma )
  • Logical Programming ( core.logic )
  • DSL image synthesis ( clisk )
  • Infix notation for arithmetic
  • Algebraic manipulations
  • Real-time declarative definition of data streams ( Storm , Aleph )
  • Music Programming ( Overtone , Music as Data )

There are a few caveats:

  • If the function is not directly supported by the JVM (for example, tail call optimization in a mutually recursive case), you will have to imitate it. It doesn't matter, but it can have some effect on performance.
  • If a function requires syntax that is not supported by the Clojure reader, you must provide your own reader (since Clojure does not currently have an extensible reader). As a result, it is much simpler if you stick with Clojure syntax / forms.
  • If you do something too unusual / uniomatic, it probably will not be picked up by others. There is great value if you follow standard Clojure conventions.
  • Beware of using macros where they are not needed. Often, just using ordinary functions (possibly higher-order functions) is enough to implement many new language functions. General rule: "do not use macros unless you absolutely need to."
0
source

All Articles