Thursday 31 March 2016

The Clojure Programming Language

The Clojure Programming Language
Clojure is a dynamic, general-purpose programming language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a compiled language, yet remains completely dynamic – every feature supported by Clojure is supported at runtime. Clojure provides easy access to the Java frameworks, with optional type hints and type inference, to ensure that calls to Java can avoid reflection.
Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures. When mutable state is needed, Clojure offers a software transactional memory system and reactive Agent system that ensure clean, correct, multithreaded designs.
History & Development Process:
Rich Hickey is the creator of the Clojure language. Before Clojure, he developed dotLisp, a similar project based on the .NET Framework, and three earlier attempts to provide interoperability between Lisp and Java: a Java foreign language interface for Common Lisp (jfli), A Foreign Object Interface for Lisp (FOIL), and a Lisp-friendly interface to Java Servlets (Lisplets).
Hickey spent about 2½ years working on Clojure before releasing it publicly, much of that time working exclusively on Clojure with no outside funding. At the end of this time, Hickey sent an email announcing the language to some friends in the Common Lisp community.
The development process is community-driven and is managed at the Clojure Community website. The website contains planning documents and an issue tracker where bugs may be filed. General development discussion occurs at the Clojure Dev Google Group.While anyone can submit bug reports and ideas, to contribute patches one must sign the Clojure Contributor agreement, JIRA tickets are processed by a team of screeners and finally Rich Hickey approves the changes
 Features:
Clojure has a set of useful features that together form a simple, coherent, and powerful tool.
 Dynamic Development
Clojure is a dynamic environment you can interact with. Almost all of the language constructs are reified, and thus can be examined and changed. You can grow your program, with data loaded, adding features, fixing bugs, testing, in an unbroken stream.

Functional Programming
Clojure provides the tools to avoid mutable state, provides functions as first-class objects, and emphasizes recursive iteration instead of side-effect based looping. Clojure is impure, yet stands behind the philosophy that programs that are more functional are more robust.

LISP
Clojure is a member of the Lisp family of languages. Many of the features of Lisp have made it into other languages, but Lisp's approach to code-as-data and its macro system still set it apart. Additionally, Clojure’s maps, sets, and vectors are as first class in Clojure as lists are in Lisp.

Runtime Polymorphism
Systems that utilize runtime polymorphism are easier to change and extend. Clojure offers simple, powerful and flexible mechanisms for runtime polymorphism. Clojure’s protocols and datatypes features add mechanisms for abstraction and data structure definition with no compromises vs the facilities of the host platform.

Concurrent Programming
Clojure simplifies multi-threaded programming in several ways. Because the core data structures are immutable, they can be shared readily between threads. Clojure, being a practical language, allows state to change but provides mechanism to ensure that, when it does so, it remains consistent, while alleviating developers from having to avoid conflicts manually using locks etc.

Hosted on the JVM
Clojure is designed to be a hosted language, sharing the JVM type system, GC, threads etc. All functions are compiled to JVM bytecode. Clojure is a great Java library consumer, offering the dot-target-member notation for calls to Java. Clojure supports the dynamic implementation of Java interfaces and classes.


Example:
Hello world:
(println "Hello world!")
Defining a function:
(defn square [x]
  (* x x))
GUI "Hello world" by calling the Java Swing library:
(javax.swing.JOptionPane/showMessageDialog nil "Hello World" )
Using Unicode (Hello δΈ– ("World") using the CJK code point for that word):
(println (str "Hello, " \u4e16)) ; to the console
(javax.swing.JOptionPane/showMessageDialog nil (str "Hello, " \u4e16 "!")); using Java GUI
thread-safe generator of unique serial numbers (though, like many other Lisp dialects, Clojure has a built-in gensym function that it uses internally):
(let [i (atom 0)]
  (defn generate-unique-id
    "Returns a distinct numeric ID for each call."
    []
    (swap! i inc)))
An anonymous subclass of java.io.Writer that doesn't write to anything, and a macro using it to silence all prints within it:
(def bit-bucket-writer
  (proxy [java.io.Writer] []
    (write [buf] nil)
    (close []    nil)
    (flush []    nil)))

(defmacro noprint
  "Evaluates the given expressions with all printing to *out* silenced."
  [& forms]
  `(binding [*out* bit-bucket-writer]
     ~@forms))

(noprint
  (println "Hello, nobody!"))
10 threads manipulating one shared data structure, which consists of 100 vectors each one containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two random positions in two random vectors and swaps them. All changes to the vectors occur in transactions by making use of Clojure's software transactional memory system.
(defn run [nvecs nitems nthreads niters]
  (let [vec-refs (->> (range (* nvecs nitems)) (partition nitems) (map (comp ref vec)) vec)
        swap #(let [v1 (rand-int nvecs)
                    v2 (rand-int nvecs)
                    i1 (rand-int nitems)
                    i2 (rand-int nitems)]
                (dosync
                 (let [tmp (nth @(vec-refs v1) i1)]
                   (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2))
                   (alter (vec-refs v2) assoc i2 tmp))))
        report #(let [derefed (map deref vec-refs)]
                  (prn derefed)
                  (println "Distinct:" (->> derefed (apply concat) distinct count)))]
    (report)
    (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap)))))
    (report)))

(run 100 10 10 100000)
Output of prior example:
([0 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19] ...
[990 991 992 993 994 995 996 997 998 999])
Distinct: 1000
([382 318 466 963 619 22 21 273 45 596] [808 639 804 471 394 904 952 75 289 778] ...
[484 216 622 139 651 592 379 228 242 355])
Distinct: 1000

For more information, please visit : www.programmingyan.com


Wednesday 30 March 2016

Opa Programming language

Opa Programming language


Opa is an open source programming language for developing scalable web applications.
It can be used for both client-side and server-side scripting, where complete programs are written in Opa and subsequently compiled to Nodejs on the server and JavaScript on the client, with the compiler automating all communication between the two. Opa implements strong, static typing, which can be helpful in protecting against security issues such as SQL injections and cross-site scripting attacks.
The language was first officially presented at the OWASP conference in 2010, and the source code was released on GitHub in June 2011, under a GNU Affero General Public License. Later, the license changed to the MIT license for the framework part (library) and AGPL for the compiler so that applications written in Opa can be released under any license, proprietary or open source.
Design & Features

Opa consists of a web server, a database and distributed execution engine. Code written in Opa is compiled to JavaScript using Node.js on the server side and to JavaScript using jQuery for cross-browser compatibility on the client side. The advantage of the approach compared to certain Rich Internet Application (RIA) platforms is that users are not required to install a plugin in their browser. Opa shares motivations with web frameworks, but takes a different approach. Its designers assert that this helps Opa to avoid many security issues, like SQL injections or cross-site scripting (XSS) attacks.
The core language is functional and has a static type system with type inference. Opa also provides sessions which encapsulate an imperative state and communicate using message passing, similar to Erlang processes. Opa provides many structures or functions that are common in web development, as first-class objects, for instance HTML and parsers, based on Parsing Expression Grammars.Because of this adhesion between the language and web-related concepts, Opa is not intended for non-web applications (for instance desktop applications).
The 0.9.0 release in February 2012 introduced database mapping technology for the non-relational, document-oriented database MongoDB, similar to object-relational mapping. The 1.1.0 release in February 2013 also added support for PostgreSQL, paving the way for the support of several SQL databases.

Examples

Hello world
The traditional Hello world program, producing a web server that serves a static page with "Hello, web!" as its content, can be written in Opa as:
Server.start(Server.http,
  { title: "Hello"
  , page: function() { <h1>Hello, web!</h1> }
  }
)
It can be compiled to a stand-alone executable JS file with:
$ opa hello_web.opa
Running the resulting executable JS file launches the web application:
$ ./hello_web.js


Top 10 Reasons to Develop with Opa
1. One Language to Rule Them All
Write simultaneously the frontend and backend code, in the same language, within the same module. Even better: the Opa Slicer automates the calls between client and server. No more manually written AJAX calls or value serialization!
And when auto is not enough, add hints to the code to enforce client or server-side, privacy, synchronous or asynchronous.
2. Access All JavaScript Libraries
Opa generates and is fully compatible with standard JavaScript code.
Reuse tons of existing JavaScript libraries and frameworks, such as JQuery which is by default part of the standard library.
3. Robust Runtime
Opa generates JavaScript on the server too, using Node.js and MongoDB.
Applications built with Opa can be deployed in most cloud straightforwardly and scaled up or down easily.
4. Database Automation
Database queries are also written directly with Opa.
Opa currently supports both the SQL database PostgreSQL and NoSQL databases MongoDB and CouchDB. More databases are planned for future releases.
Opa provides many unique advanced operators and automates the database queriesfor maximal productivity.

5. Typechecker
Opa unique feature is its advanced typechecker that automatically verifies your application code, looking for bugs and inconsistencies, and crunching debugging time.
The Opa Type Checker was designed to bring static verifications to dynamic programming experience. Type checking is incredibly fast and features type inference: Application code stays lean and clean.
6. Truly Non-Blocking
Modern applications use a lot of asynchronous calls. Dealing with callbacks manually can be painful, and failing to do so properly blocks the application runtime.
To make asynchronous programming easy without blocking the application, Opa-generated JavaScript code uses smart continuations.
In the following example the Opa compiler automatically takes care of everything.
7. MVC Support
Opa support MVC (Model-View-Controller) programming and provides a scaffolding mechanism to get started instantly.
Just minutes away from creating a real application.
8. HTML5 Native Support
Opa is built for the modern web. HTML5 fragments can be inserted directly. No more messing with single and double quotes!
CSS(3) elements including selectors are also easier than ever to use.
9. Powerful Syntax
Opa is JavaScript on steroids with many syntax and feature enhancements.
Database, types, pointers, autoclose, higher-order functions, etc. The list is way too long to fit here. Find it on the reference card.
10. Power Rows
The core of Opa uses Power Rows: A powerful, statically-typed, extension of JavaScript objects.
Power Rows is one of the features that makes Opa safe and fun at the same time.


For more information, please visit, www.programmingyan.com

Monday 28 March 2016

Dart Programming Language

Dart Programming Language

Dart is a general-purpose programming language originally developed by Google and later approved as a standard by Ecma (ECMA-408). It is used to build web, server and mobile applications, and for Internet of Things (IoT) devices. It is open-source software under a BSD license.
Dart is a class-based, single inheritance, object-oriented language with C-style syntax which compiles into JavaScript or native code. It supports interfaces, mixins, abstract classes, reified generics, and optional typing.
History:
Dart was unveiled at the GOTO conference in Aarhus, Denmark, October 10–12, 2011. The project was founded by Lars Bak and Kasper Lund.
Standardization
Ecma International has formed technical committee TC52 to work on standardizing Dart, and inasmuch as Dart can be compiled to standard JavaScript, it works effectively in any modern browser. Ecma International approved the Dart language specification first edition in July 2014, at its 107th General Assembly, and a second edition in December 2014.
Usage:
There are three main ways to run Dart code:
Compiled as JavaScript
To run in mainstream web browsers, Dart relies on a source-to-source compiler to JavaScript. According to the project site, Dart was "designed to be easy to write development tools for, well-suited to modern app development, and capable of high-performance implementations. When running Dart code in a web browser the code is precompiled into JavaScript using the dart2js compiler. Compiled as JavaScript, Dart code is compatible with all major browsers with no need for browsers to adopt Dart. Through optimizing the compiled JavaScript output to avoid expensive checks and operations, code written in Dart can, in some cases, run faster than equivalent code hand-written using JavaScript idioms.
In the Dartium Browser
The Dart software development kit (SDK) ships with a version of the Chromium web browser modified to include a Dart virtual machine (VM). This browser can run Dart code directly without compiling to JavaScript. It is intended as a development tool for applications written in this language, rather than as a general purpose web browser. There were originally plans to include Dart support directly in Chrome, but these were cancelled.
Stand-alone
The Dart SDK also ships with a stand-alone Dart VM, allowing dart code to run in a command-line interface environment. As the language tools included in the Dart SDK are written mostly in Dart, the stand-alone Dart VM is a critical part of the SDK. These tools include the dart2js compiler, and a package manager suite called pub. Dart ships with a complete standard library allowing users to write fully working system apps, such as custom web servers.

Runtime Modes
Dart programs run in one of two modes. In checked mode, which is not the default mode and must be turned on, dynamic type assertions are enabled. These type assertions can turn on if static types are provided in the code, and can catch some errors when types do not match. For example, if a method is annotated to return a String, but instead returns an integer, the dynamic type assertion will catch this and throw an exception. Running in checked mode is recommended for development and testing.
Dart programs run by default in production mode, which runs with all dynamic type assertions turned off. This is the default mode because it is the fastest way to run a Dart program.

Snapshots
Snapshots are a core part of the Dart VM. Snapshots are files which store objects and other runtime data.
Script snapshots
Dart programs can be compiled into snapshot files. These files contain all of the program code and dependencies preparsed and ready to execute. This allows fast startups.
Full snapshots
The Dart core libraries can be compiled into a snapshot file which allows fast loading of the libraries. Most standard distributions of the main Dart VM have a prebuilt snapshot for the core libraries which is loaded at runtime.
Object snapshots
Dart is a very asynchronous language. With this, it uses isolates for concurrency. Since these are workers which pass messages, it needs a way to serialize a message. This is done using a snapshot, which is generated from a given object, and then this is transferred to another isolate for deserializing.

DartPad


The Dart team created DartPad at the start of 2015, to provide an easier way to start using Dart. It is a fully online editor from which users can experiment with Dart application programming interfaces (APIs), and run Dart code. It provides syntax highlighting, code analysis, code completion, documentation, and HTML and CSS editing.

For more information, please visit : www.programmingyan.com