"In computer programming, boilerplate is the term used to describe sections of code that have to be included in many places with little or no alteration. It is more often used when referring to languages which are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs."
http://en.wikipedia.org/wiki/Boilerplate_%28text%29#Boilerplate_code
bool iguales=true; for (auto i=c.begin(); i!=c.end(); ++i) if (*i != 0) { iguales=false; break; }
// funcion definida en alguna parte bool es_distinto_de_0(T i) { return (i != 0); } // Y luego en el código... bool iguales = (c.end() == std::find_if(c.begin(), c.end(), es_distinto_de_0));
(def iguales (every? #(= 0 %1) c))
// 800 líneas (literalmente) de definiciones después... #define BOOST_FOREACH(VAR, COL) \ BOOST_FOREACH_PREAMBLE() \ if (boost::foreach_detail_::auto_any_t _foreach_col = BOOST_FOREACH_CONTAIN(COL)) {} else \ if (boost::foreach_detail_::auto_any_t _foreach_cur = BOOST_FOREACH_BEGIN(COL)) {} else \ if (boost::foreach_detail_::auto_any_t _foreach_end = BOOST_FOREACH_END(COL)) {} else \ for (bool _foreach_continue = true; \ _foreach_continue && !BOOST_FOREACH_DONE(COL); \ _foreach_continue ? BOOST_FOREACH_NEXT(COL) : (void)0) \ if (boost::foreach_detail_::set_false(_foreach_continue)) {} else \ for (VAR = BOOST_FOREACH_DEREF(COL); !_foreach_continue; _foreach_continue = true) #endif
Alexander Stepanov (coautor de la STL) dijo una vez:
Lenguaje de programación diseñado por Rich Hickey, basándose en cuatro grandes ideas:
Algunos datos sobre Lisp:
Clojure tiene muchas de las características de los lenguajes funcionales:
...pero no es un lenguaje funcional puro al estilo Haskell
1234
22/7
3.0
"Hola, mundo"
#"patron"
es un objeto de tipo java.util.regex.Pattern:cosa
cosa
true
,false
(1 2 3 4)
[1 2 3 4]
(def v [1 2 3 4])
(v 0) => 1
{:a 1, :b 2}
({:a 1, :b 2} :b) => 2 ({:a 1, :b 2} :x) => nil
#{1 2 :a "blah"}
(def name val)
: crea una variable global(fn name? [params*] exprs*)
: crea una función(if test then else?)
: evaluación condicional(let [bindings*] exprs*)
: crea "variables" (aliases) locales (quote form)
: devuelve form sin evaluar(do exprs*)
: evalua expresiones en orden, devuelve la última(ns wordcount (:gen-class) (:use clojure.contrib.str-utils)) (defn num-lines [text] (count (re-split #"\n" text))) (defn num-words [text] (count (re-split #"\s+" text))) (defn num-characters [text] (count text)) (defn -main [file] (let [text (slurp file)] (println (num-lines text) (num-words text) (num-characters text))))
if
es muy sosa: (if cond then-expr else-expr?)
cond
(defn f [x] (let [resto (mod x 3)] (cond (= resto 0) "cero" (= resto 1) "uno" (= resto 2) "dos")))
cond
es una macro :)clojure.core:
(defmacro cond "Takes a set of test/expr pairs. It evaluates each test one at a time. If a test returns logical true, cond evaluates and returns the value of the corresponding expr and doesn't evaluate any of the other tests or exprs. (cond) returns nil." [& clauses] ;argumentos variables, clauses es una lista (when clauses (list 'if (first clauses) (if (next clauses) (second clauses) (throw (IllegalArgumentException. "cond requires an even number of forms"))) (cons 'cond (next (next clauses)))))) ;definición recursiva
'if
es lo mismo que (quote if)
,
es decir, el token if sin evaluar(doc mifuncion)
user=> (def naturals (iterate inc 0)) #'user/naturals user=> (take 10 naturals) (0 1 2 3 4 5 6 7 8 9)
user=> (def zeros (cycle '(0))) #'user/zeros user=> (take 10 (interleave naturals zeros)) (0 0 1 0 2 0 3 0 4 0)
struct Animal { virtual void encounter(Animal& other) = 0; } struct Lion : Animal { void encounter(Animal& other) { if (Lion* lion = dynamic_cast<Lion*>(&other)) { fight(); } else if (Bunny* bunny = dynamic_cast<Bunny*>(&other)) { eat(); } else { /* default handling here */ } } } struct Bunny : Animal { void encounter(Animal& other) { if (Lion* lion = dynamic_cast<Lion*>(&other)) { run_away(); } else if (Bunny* bunny = dynamic_cast<Bunny*>(&other)) { mate(); } else { /* default collision handling here */ } } }
(defmulti encounter (fn [x y] [(:Species x) (:Species y)])) (defmethod encounter [:Bunny :Lion] [b l] :run-away) (defmethod encounter [:Lion :Bunny] [l b] :eat) (defmethod encounter [:Lion :Lion] [l1 l2] :fight) (defmethod encounter [:Bunny :Bunny] [b1 b2] :mate) (def b1 {:Species :Bunny :other :stuff}) (def b2 {:Species :Bunny :other :stuff}) (def l1 {:Species :Lion :other :stuff}) (def l2 {:Species :Lion :other :stuff}) (encounter b1 b2) -> :mate (encounter b1 l1) -> :run-away
(new Clase)
o simplemente Clase.
(. clase miembro)
(. "hola" toUpperCase) "HOLA"
(.. System getProperties (get "os.name")) "Linux"
(.run (Thread. (fn [] (println "Hola"))))
(def PrintElementHandler (proxy [DefaultHandler] [] (startElement [uri localName qname attributes] (println (format "Saw element: %s" qname)))))...es equivalente a...
public class SomeNewClass extends DefaultHandler { public void startElement(String uri, String localName, String qName, Attributes attributes) { System.out.println(*stuff*); } }