Code as
  Data
as Code




@fogus
me

12 years
   XP
me

12 years           AI
   XP           “Real World”
me

12 years                      AI
      XP                   “Real World”


C -> C++ -> Java ->
Python -> Ruby ->
Scala -> Clojure


      Langs
me

12 years                         AI
      XP                      “Real World”


C -> C++ -> Java ->        * Joy of Clojure
Python -> Ruby ->          * Clojure/core
Scala -> Clojure             - Clojure
                             - ClojureScript

      Langs                      Clojure
me

12 years                         AI
      XP
               I❤             “Real World”



               Java
C -> C++ -> Java ->        * Joy of Clojure
Python -> Ruby ->          * Clojure/core
Scala -> Clojure             - Clojure
                             - ClojureScript

      Langs                      Clojure
me

12 years                         AI
      XP                      “Real World”


C -> C++ -> Java ->        * Joy of Clojure
Python -> Ruby ->          * Clojure/core
Scala -> Clojure             - Clojure
                             - ClojureScript

      Langs                      Clojure
am s
     g r ng
 ro iti
P r          s
 W ram
 P rog
Java
create table COFFEES
  (COF_NAME varchar(32) NOT NULL,
  SUP_ID int NOT NULL,
  PRICE numeric(10,2) NOT NULL,
  SALES integer NOT NULL,
  TOTAL integer NOT NULL,
  PRIMARY KEY (COF_NAME),
  FOREIGN KEY (SUP_ID) REFERENCES SUPPLIERS
(SUP_ID));




                                              Data
create table COFFEES
  (COF_NAME varchar(32) NOT NULL,
  SUP_ID int NOT NULL,
  PRICE numeric(10,2) NOT NULL,
  SALES integer NOT NULL,
  TOTAL integer NOT NULL,
  PRIMARY KEY (COF_NAME),
  FOREIGN KEY (SUP_ID) REFERENCES SUPPLIERS
(SUP_ID));




                                              Data
public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, " +
                   "PRICE, SALES, TOTAL from " +
                   dbName + ".COFFEES";

    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            doPrint(coffeeName, supplierID, price, sales, total);
        }
    } catch (SQLException e ) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}
public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, " +
                   "PRICE, SALES, TOTAL from " +
                   dbName + ".COFFEES";

    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            doPrint(coffeeName, supplierID, price, sales, total);
        }
    } catch (SQLException e ) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}



                                                                     Data
public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, " +
                   "PRICE, SALES, TOTAL from " +
                   dbName + ".COFFEES";

    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            doPrint(coffeeName, supplierID, price, sales, total);
        }
    } catch (SQLException e ) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}



                                                                     Code
OR
  M
public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, " +
                   "PRICE, SALES, TOTAL from " +
                   dbName + ".COFFEES";

    try {
        stmt = con.createStatement();




                                   ORM
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            doPrint(coffeeName, supplierID, price, sales, total);
        }
    } catch (SQLException e ) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}
OCaml
from this...
List.map (fun x -> x+1) [1; 2; 3]
to this...
            List.map (fun x -> x+1) [1; 2; 3]




ExApp (loc, ExApp (loc, ExAcc (loc,
   ExUid (loc, "List"), ExLid (loc, "map")),
     ExFun (loc, [(PaLid (loc, "x"), None,
       ExApp (loc, ExApp (loc, ExLid (loc, "+"),
         ExLid (loc, "x")), ExInt (loc, "1")))])),
   ExApp (loc, ExApp (loc, ExUid (loc, "::"),
    ExInt (loc, "1")),
     ExApp (loc, ExApp (loc, ExUid (loc, "::"),
       ExInt (loc, "2")), ExApp (loc,
         ExApp (loc, ExUid (loc, "::"), ExInt (loc, "3")),
         ExUid (loc, "[]")))))
just this...
List.map (fun x -> x+1) [1; 2; 3]
well, this...
      List.map (fun x -> x+1) [1; 2; 3]




<:expr< List.map (fun x -> x+1) [1; 2; 3] >>
the expander
      List.map (fun x -> x+1) [1; 2; 3]




<:expr< List.map (fun x -> x+1) [1; 2; 3] >>




          camlp4::expr
Pure
foil
foil
(x1 + y1) * (x2 + y2)
(x1 + y1) * (x2 + y2)
(x1 + y1) * (x2 + y2)
(x1 * x2)                           First
(x1 * y2)                           Outer
(y1 * x2)                           Inner
(y1 * y2)                           Last
(x1 + y1) * (x2 + y2)=
                    // First
   (x1 * x2)+
                    // Outer
   (x1 * y2)+
                    // Inner
   (y1 * x2)+
                    // Last
   (y1 * y2);
(x + 3) * (x + 5);
//=> x*x+x*5+3*x+15
x*x = x^2;

(x + 3) * (x + 5);
//=> x^2+x*5+3*x+15
Code as Code.
Data as Data.
Code as Code.
Data as Data.
#}({_({}{[)#[]]}}{}{#(#(]#_}#([()_]}[_}{[_]#}]_{}{(](#[)#_#()(}{[#{_{)[])#[[_}[_{]((_#_(_{(#_#_{)}}#}{{){]([)]({_[))][][#}()}_[##)_#]]#]#)
_(]}_{)]#}_##(#]]_(}))(}#{#(_#)(]))#}{{_{[([(_]{)]})_([]}_{[#_][{{[_(_}[_])]]]]_#[((}{{#_)#[#(]{#__()[{#_}))_))]()]{()#}((](]_}(#][{(}}[[_
#]_]}#{}{][)[[_#__[)}]}]]__##[(_{#}#}[)({)}[)(}](_(}{_[_{)){#___]{{](]#(#])_[)[([({{]#)}#(}[]#])##_}{_#{[_))#{)}}_(_#[[{[}#[](([(_((){_[#]
[[[#_}}]#_([#]]_}_]({_]){#{[]((]#}}{[)_{([#]{#({)#{(}###](]{)]{]}_()#]}{{]}_##){)()[#})]]_(}](#)(#]]#]__]]#][_(}]}{{__(_)(()()()[(#(#_}]}_
{##]{{__()#{(_)_()#(#[])_[[)[[[{[(_[()]{#[}(][()}_{(}{)((}[])[_#{}][#[[])[[_(]}]}}[)#{{#{##]{]#_[})(}}(})((#))[)(}}_)#]]{)}_]))_]#](}_{)})
{)]#_([[]()()({(}]}_{{#([(__#_}]{[]}}}#({]#)#{}_)}){)#]([#}))(##(}([[[[}[#{_(}}}[{{(_}_[#)]()}}{#}#(]}}#}[{#}([_)(#]]}#()){)_#()]}(}}}{[{}
){[#((]}#_){[[#{#]]]([}[{}()#{{{[})({_[[__)}#(_)[[][_[)[([{}((][#}]((#[{(([}[#][})(}()}[[(}]]))##}{{)#({##[}{{[_]_}]}}[{)))#{])#}{_[}[_(]]
]_{][#__(_)#_({_{(}})(]{}]{[_({{#}_((_{){{#][)()_{__[{[[}[(((()[}(])[{#()#((}#{{(#}[#((])({}{([__]_{]{{_#{[)(]]}}]))}{##})#)}#{_]{{{#_(]][
{#))_[[({][[}{}{#}})}#[_)__#_](}([]{}(#)##}}[{_{])___{_))]{#]{_[#(}))##__))[)_]_[_]{[][[)]{[{])){({#_]}_{((([]##)}(}]#))}]{(#}(}]}_{[}[(]_
(_[#[{[(({##}#__##}][_[)[}#)]#{{_)__(((((){#]})_)#{{)_{})}(__)}_}_#{))[)(]])}#_](#){}_]_#]]#])(])[(]_[_}}(#[#)_#[]))_{}#]{#[[#{}([(}}_#__)
]}[}}#(}){][_]])}}_]})[#{}}))]#}})(]#}_{)#[}}__))[[[[)##)})#_]##)])]##}}[{{[)#}[)(}_({(]]}[(}_]_[#[(#({})}{}}_}{}][_{]]_#_[(_}[]_[)[{()}#}
))({[]{##)}[}}#))(#(]{{[})_{#[[##[]__#(}{)[)}(})#[{)](_}[}#(}(([[(([#){)#{)]){{}{{]}(()(][_)]##{[}#){[_}_#{(#}_}(_#)[{_[})_{{#_{]{{}}##)#]
#[]##_[(}{(}[_{]){_](__)]][(#[})]{#)]_]}([)[]]){)_[)_#)}_}[()_]]#[[}]][#{[#(_{[)}{)#(})##[(_(_##[](]]_})}{)#[))]_##([[)}{)])_#]#]}(_#(](){
[({#)_()_}([#{[][_)([]}_#[#_)}(](]([(){})}](}{}#_]]}_(#}#__[[_[]]#()#}(([(_{]())]{){_]{((__}}[[[}]_#{{)]]#(#[{}((##(_(])([#{[(_{_(#_){}{[_
#(})}{]}]]][[__[[((]]]}(}]{()#)[(#]}){{[}}_]{#}_{)]#[_))(}](_}#)}}_{))(}]_[__([{))[#}__((]_]}]#}]){}({}#[][_[{}}}]]#(({__}#)#[[_]_]#_}}{](
#}#_{({}_)(_}[}((](##){)}#_([([]_#}[[{[([)_(})_{()(_{}[)#(#_][_##}[}[([[#{)[}{{#}[(_]_()])#[[__(#{}_)))#{)[([{_]_[(]]_#[((_})___(_}[{[#[]_
[](}_#{[]{_][_})])}#]]__]#}_(()){}}]#]}#)[(_[##([(#_[)[#_#__{{}}#[}[}(}#]{}_[)[}{}_}#[)#[{[([){}({(){(){]_[[]}{)_)(({[(])#[_(}]#[](}{[[#]#
{}{{{[([]{}{#_)}(])#)___))}[){[_})[#{##_])#}})[)(]#{[){)((]}_]{]_(){{{])[}]_]{{_)]_){}#{}#({{)[)}#}(_()[[[([(_]#___#([)_{[#][[#)_[{))]])[)
(_}{]]]({#{#)]][[)_{]#(){_}[)({}{#})#_]{]#_[{][]])](#[}##}[)})](]([{)]](]#{)}(#)]{(([{[}_)#()#___]#}#]]})[]#[}](_#_{_[_}[[][__]__#]_{((}[#
#_[}})_]]#[{])_{#){})[]_)_]#({#({){(_{_{_(]){(((#)[[_[{}){}(#]#{#])]]__[}]_[}]}}(}#[{{_}#[{[)(({{)])#[}([#{[((}{}###[}(]{}]}})}(#](#}}_{[{
{}#{{})){(}((){})}]}__{###(_[()(([]{[(##[([#]_#}(#}](}]{(#]_{](}{)}{_})[]{[{_(#(}{}]}}){]{])()_(()([}#_##[}]){{{#())#)_(#{)][]]{[})(_#{])}
_##){_(]({]][(]){}{}(((}#]]})]#({#_]}_#{)]})[{}[}{}}{(_{{[]))}{}([(]_(#)(}(#)_#(_}{[)[}_#{_[]()_#(){_(()(#_}())[]{))#_}}(#}))(}]){#_)(#[])
}#[_({]]([)#(]((}##)[#([_#})({(#(}#_}_#(}(}]{#)]_})}([))#()[_(][#}[{#)}}}[##)}#)_]_{]}}#)#{}()))#[_)}#[_##_(())}##_#)_){[##{]#)_)}][))([[{
#{#{##_[#({(}{}_(){}{[({}[]##_][}#([}}{[#]]#}__{({)][{}_#(_][_)}#)])]{#]]]][{(){][[((](][#[{)(}{{[({[][}(}]_(#[]]_){({{{})(}#{_)))_(_[#]{(
[{[})#[#]})}]#{_#[{])([[#})__})(}#(][)}({)_)#[[_)_{(__{)[{#_()}{[{(#{]}]](_[])]_)]]{])#([[](){}#[_[)}#)[#_]){{}_{{{#}#}{[][)){)[{_}})){[_]
{}#{})(]({)#)]_}[[[#]##(#[{((##[([###]{[]_[{[}{(]_}_[]}{()_#()(]]_}#]{}#{__]))][_)(}{_{)#}]#(#_((#)#_[}_}[{{((]){](([]_{))})[{]]]{#}[)_]))
#]]_[{#}]##()}})))]])#{{)[((]{)]#}[}}{)][#_[{[_{(})]#(_#]#)_}[}##([[{#]_}{]{(}]][}[[(#}#_}_#[((#](_#([]{}}([]((([[)())(_)({#)})]_]]{{)]][}
(#})((]](_[_[{#}##){{)(]_#]_(}#}[([)(_##(_(_[{(#)}(#}_(#{})##(#][[#({}[{(__{#[{}[){]#))_][#}({(_{##[[}]{_([#}]]{]((_{##}]))))__)#[]#(()}{(
]]#([_[##}{)[][[)[[{{_{{_[_)[]{#{{{}]#{##{{){_]][{_})}_})]}#{{{}}]#)#_]#))[)]([#{[)_{#{__[[}]}[)[[]{_][])(](}}#({}}]#}__{)[_((_{{[[}}[_)#_
})#}#_#{[{]}#)_(#}#()){#[(#({][)}})_(}}__{()###[}]]_})({#}]#_#(({_])#[(]{#}{{]{])(_}{#][#[_(})[}##](]{{{{)[]}]#[_{#][]#_#))_}[#(#_]_[_{[[_
(]#(_}}][[{_)_}](]()([][_{_{__[#{){]{)(}(_{#)#__(##(]_])]]#[[]#[_{(()#)[]({_][](_{{[]{#)({#]][]{{}[__)}{#{{}_{][)}(#[][]{[#{[])#}{()#___[#
}][}(}###_]{}{])]])]}]](__()){)(}}[}{_[([}(_]#}#[{[#]{(_{}]_]}}}#](({#[_}_#{{}][{_}#}}(##)_#))]([_))[]))#[#([(]]{[]{]])_{])_(((}]#[({{_]})
[{#}(}[]](#]}}}}]_#[[]{[(]_#____#((](][}(}_)__)[{(([)]})]{#__{{{}#(})#)[#}))}(__[[{]#__([](()()[_]])((_#(]}##(}(_[{{(##[[[}#)#_[)#{{]]](])
#])(}_([]#(}#(##])]_({#)#[)][(#)}__]{]})_#{#}_(({{)]{([##]}}[([_#[}()__[}_)]_[_])##){({()_}#{}[))(]((_]({_###]}#){[[#{[(]_)#)}{{}()#){]([{
}][)))#])[]{[[_}({}__){##}[_])#](}(}#_}_)[#}{_[[[#{{}]([{#]#){[}##{}_{_})([}_(_)#{})(_((#}#_(_[_}(#{}__)}}]_](){####{}]#[({#)#])]{)##}{}]#
){))]{}([(#}_[#_#(#){[_[_](}(}(#{{#[()()((__])]]#{()]##))]]{(}{_#]]()[}[(##[(_({{(#}[})[])])]()_)__{[{][#{)]#}([_(#{#{#{})[])_](#_}[}[][((
_#[({#}(#_]#_)}###{}#({_[]}[)__}}}{{#}{#])[](}[(]}__{{(#(({_)]_{(#])__[{}]()_[#[}_[#]{}#]]{{()#)_[[[{{]}#(}](##[]]}[_][({_#{[)]}]}(]#]{}_[
_[[[[[_{#(#](}#}_}){[)()#([]{()#)_][_#})(}__))}]_})(]{)[#(#]#_#)[)]{##{{[{)#{(}_}([})((#))}__#)_)_{[#((}_(#{}_{]]_[}##{#))_{(#()#][{][})#_
[[{}))[]_[)#_)}#_[({}(({]{)}{))(}})})((([[{){[{())](}]_]}}(}}[#{_](([({)]{}##{_{)}{(__](]#_}#){__}]_)][__{}#_{_[]([)_#{([]}}}{_)}_[[_)#)(#
}}(]]{[}){(][[)#){][{)#[(_}{}##(]_]#{}][]){()}[{))_{_[#{[_[]({{#(]]#_]]}(([((#_{(#)]][#{__{]{}#{_##}}((#([(}][[(((([(#_)(}))##{]_{])[}{[{)
##_#[#_}([_(]]_{)[([#[][[))_(#_{[}]#])}[(](]]}(##[((_}_#()#{#_}_[#}]#_[)[_{_){)#[#___[[__)}(#]{[)]]()}[}_(}({_(#{}[](#{#){(]{[(}[{(]}(__]{
)#_){{}[{_]][{[#)(]][#{#[}([)]{){})}[##)#)]_[()}{(]#)#}#([}[#[]{]]}{]]{{}#{}]__{)#)_(){)#{#}{#[###({{[)(}[}({])]{}_{[]{}#_][_]}((]{[[_}[#]
}_]{_[((#([#__{([#)(])]{#)_())#}__{_##}]_#{##])}}}[##)[(][_})#}_{()(_)]_#]{[_(){)}][}[[]_{#([#[()[()}))_#[{([[}}){(_{((]#[()__}]_#)_{}#{[}
]_]}[__{{[([]#))]}[[(][{])((}[)}))((]}{[[[)#[]{{#][[}]{(_]}]]((_#(_}#(_{_({])__}]###]){_[}#__[)(_([[}[#}{]{__#({]}}(_{[}{)_#{[{(}_]({#))[#
})#}{{[())}[[}{}(])]]({_][{]{(_{[([#}]_)_)#)]){#_{()(_{)#)({{()(#_}{}])[_(}#_}#((_[_(_#[})]##_(__]]}])((){{#)){{([_)({}{(}}#]}}]()](##{(]_
_]}_(_[]#}{{)__{)(](__{##[({#]_#]_}{][#_[##[}))_})[}#({]_}#)}_()(}#[}{_)[)]#][#{_{_{#[({}({_(}[(({}}]#]_)_#[_{]#}[])]}]{})#(]}([{]_([])]_[
}_{#}([](}(({){][((}(({[{[(){#}}#]_(}#[{{#](}}(#])}{)})#(])({)}}_}{})]{]{}(([}}[})[{]#[)[#{[)][}){])]]}]](]{)][){{]}[{___[_#[)]#}}{#_))#)_
][#})(_{#))__][)[[[))[_}}##()]_))}#_)}#[{)([__)]#][}#)]{}#(#{}[)]_]_[{))[](](#{[[_]{])[#}_{###{]([[)[]#}])__[_)#(){[))}})){])]()(_[]#[}}]#
}}]}___{{]#_#}{}##___}_]__]#[([)_}](}_)]}__[[{){[{]}#(][}]_)#(][(#(]))__{{)_]}#){_{]})](}#(#[)#{}{(}[_{)##(][){}(}(]#[{}_)[[({]_#]]}{])}_#
}[{[(__(_]_({([#{_)}#[{##{_#)[[}]}[()]]({{##)_({](_}_{_##_#{#_})_{[}}[(}{))][_{#{[_##[}(}_#}([[{{((]#])}_(][[#)]_{[})(#()]{_[{_){}#((}[_{}
_[#{}[]][}#[]]]{_(}[)([_[)})()##}(]_[][)}__]_###)[#{_[[_(_}#(_)}#_((]]]))]_()_)#}#][##_)##)(]{(_[#_](_)}#){]{{)([({}[(})))[[))[_{#{)}{(}((
]_)[##))#_][#}_#[)_}[#_][]_)((#(}_(]_)#])[}}_##][(({#}])}()))_#(}}_#){[)}[}])()]{]}}){{})[(])#()}})}_{(}}[#]}_[_])(((_#{[##_(}])#)_{])]((}
{})_]#[(])({{({##[{[)__(__#(#(#{()]{[}#}(})]_#[[))]]_}{__{##[({[[}]})#(__(}{(_]]}(_){)[)#)}}[[}}#]])}#]#}_[}}]{((]])[{#]}){[#}{__]}{(#]_][
##{](]}_}#][}_({}(##]#[{#]))}[]_)##}##}{[#[#_][)[]_){)]}#_[##]___)}_#]})]#]#}_{[})}]__){]}({((])(})[(])_]]_{{}#(][#(({}{])]#}[(_[#}_]}]{{{
[}](_#_{}]))[}][#(}[{##)#(_{])}_]_})}#{()_(_}}}})_#([)([#]##]}({[{({{{[))#_{)_(]]({_]}_#}_])[)][(}{}(____{}(}__]}[#){#[}}[})}_{))]([[{{{}}
(_]}()}]]#{[}[)}_{){]]#_(({_})_}]_#)[)#_])}(__[]](]_[#[_[{{})_{}[]]#[{}}}{{]}[#}#}]]]))[[]((]#(#[{]})(][#_()({[[(]#}_{{[((](])[{{}##}{}}()
){}])[#][)][)([]]){)#[]#{__[{}[[}#])_)][}#[]]][_{#[]())[#)]({){()]_}(]_))#])}([)[})[)(#){()[}(_]#](_{{(]#{_#)}}([})})))}}##:F(})({)#]_(({{
)}{_[[(_([__[#}(([#){{][_)]]]}{#])#{_)#}}{)#_]_#_({{[([#)#[]{[]{[#{[(]](]_{_([[{)[##]#(}(#]}_](#{{][{){][_({]){){]))(]{([](]_)]{(_](({]{}_
[]](([}]#[]}){##(]})((]{[][([_]{}_}{#]_#}()[[#[_(_)))#}]((){]]{[(]{[)#)_{(]([][#_[#)]({#}_]((({#{))][#]{(]#)[{}({#[__})[)_)#]#({({[)#[{}[)
Data as Data.
scalar data types
       type         example      java equivalent
       string        "foo"            String
     character        f            Character
       regex        #"fo*"           Pattern
      integer         42        Int/Long/BigInteger
      double        3.14159          Double
     big double    3.14159M        BigDecimal
      boolean        true            Boolean
        nil           nil             null
       ratio         22/7              N/A
      symbol        foo, +             N/A
     keyword      :foo, ::foo          N/A
32
composite data types
      type      properties

                singly-linked,
      list
               insert at front

                   indexed,
     vector
                insert at rear

      map         key/value


      set            key

33
data type literals
     type          literal


      list       (1 2 3)


     vector      [1 2 3]

                 {:a 100
      map
                  :b 90}

      set        #{:a :b}

34
Client   ?    Server




         35
JSON

Client          Server




          36
JSON is...
• Maps (Objects)
• Arrays
• Numbers
• Strings
• true/false
• null
                   37
Client   ?    Server




         38
Client        Server




         39
clojure data are...
•   Maps              •   Keywords

•   Lists             •   Symbols

•   Vectors           •   Strings

•   Sets              •   Chars

•   true/false        •   Integers

•   nil               •   Floats

•   Metadata          •   Ratios



                 40
XML
ML
as Code.
Data
Engine




  44
Specification   Engine




                 45
Specification   Engine




                 46
I’m really
                           doing
                        something
                            cool
Specification   Engine       now




                 47
specification of source

     (defproject marginalia "0.7.0-SNAPSHOT"
       :description "lightweight literate programming for clojure"
       :main marginalia.core
       :dependencies
       [[org.clojure/clojure "1.3.0"]
        [org.clojure/tools.namespace "0.1.1"]
        [org.clojure/tools.cli "0.2.1"]
        [hiccup "0.3.7"]
        [org.markdownj/markdownj "0.3.0-1.0.2b4"]]
       :dev-dependencies
       [[lein-clojars "0.6.0"]
        [jline "0.9.94"]]
       :marginalia {:javascript ["mathjax/MathJax.js"]})




48
awesome.jar
project.clj




              49
specification of sound

     ; Roots of the chord changes, 2 beats   each
     (def gs1-changes
       [:B4 :D4 :G4 :Bb4 :Eb4 :Eb4 :A4       :D4
        :G4 :Bb4 :Eb4 :F#4 :B4 :B4 :F4       :Bb4
        :Eb4 :Eb4 :A4 :D4 :G4 :G4 :C#4       :F#4
        :B4 :B4 :F4 :Bb4 :Eb4 :Eb4 :C#4      :F#4])




50
giant-steps.clj




                  51
specification of behavior
     <project name="clojure" default="all">

       <description>
         Build with "ant" and then start the
         REPL with: "java -cp clojure.jar clojure.main".
       </description>

       <property name="src" location="src"/>
     ...
     <target name="clean"
               description="Remove autogenerated files and
     directories.">
         <delete dir="${target}"/>
         <delete verbose="true">
           <fileset dir="${basedir}" includes="*.jar"/>
           <fileset dir="${basedir}" includes="*.zip"/>
         </delete>
       </target>

     </project>

52
I’m really
                           doing
                        something
                            cool
Specification   Engine       now




                 53
awesome.jar
build.xml




            54
?


 build.xml
<xtra-thing/>




                55
xtra.jar




 build.xml
<xtra-thing/>




                           56
xtra.jar


                                awesome.jar
 build.xml
<xtra-thing/>




                           57
build.xml
<xtra-thing/>
                !=   xtra.jar




                58
app   59
something
                else
              entirely
(foo)




        60
(foo)
macros
(foo)
(foo)
Code
       as Data.
function call

     semantics:    fn call            arg


                   (println "Hello World")



     structure:              symbol   string

                        list

63
function definition

          define a fn   fn name
                                   docstring
             (defn greet
               "Returns a friendly greeting"
               [your-name]
               (str "Hello, " your-name))
     arguments

                         fn body


64
it's all data

          symbol      symbol
                                   string
              (defn greet
                "Returns a friendly greeting"
                [your-name]
                (str "Hello, " your-name))
     vector

                          list


65
metadata

     prefix with ^       class name or
                        arbitrary map
         (defn ^String greet
           "Returns a friendly greeting"
           [your-name]
           (str "Hello, " your-name))




66
code as data benefits
easy to parse
regularity simplifies metaprogramming
data library = metaprogramming library
"language" features are library code
metadata
query-able
Code as Code.
Data as Data.
Code as
Data as
 Code.
Data as
Code as
 Data.
Programs
 Writing
Programs
 Writing
Programs
Minderbinder
http://futureboy.us




           (Specification)
Primacy
                              of
                          Semantics
I also regard syntactical problems as essentially irrelevant to programming languages at
  their present stage ... the urgent task in programming languages is to explore the field
                                                                  of semantic possibilities.
     — Christopher Strachey - “Fundamental Concepts in Programming Languages”
Unit Conversion
   (defn meters->feet [m]
     (* 1250/381 m))

   (defn meters->yards [m]
     (/ (meters->feet m) 3))

   (defn meters->inches [m]
     (* 12 (meters->feet m)))

   (meters->feet 10)
   ;=> ~ 32.81

   (meters->yards 10)
   ;=> ~ 10.9

   (meters->inches 10)
   ;=> ~ 393.7
Unit Conversion

     feet→meters
  inches→meters
   yards→meters
Unit Conversion
      centimeters→shackles
  ramsden-chains→fathoms
                 feet→meters
              inches→meters
               yards→meters
             feet→kilometers
          yards→centimeters
old-british-fathom →meters
Unit Conversion
       centimeters→shackles
   ramsden-chains→fathoms
                  feet→meters
               inches→meters
                yards→meters
              feet→kilometers
           yards→centimeters
 old-british-fathom →meters
Unit Conversion
CM->MI
MM->M          centimeters→shackles
 F->IN     ramsden-chains→fathoms
 IN->Y                    feet→meters
 F->M
                       inches→meters
 Y->M
CM->Y                   yards→meters
F->MM                 feet→kilometers
M->MM              yards→centimeters
CM->M    old-british-fathom →meters
  F->Y
bit->                     t e
      nibbl
     Unit Conversion    y
                 e ab           t e
   CM->MI
   MM->M
                 eg
     me centimeters→shacklesb y
    F->IN
          gm efeet→meters
             ab g        a
         ramsden-chains→fathoms
     >e > minches→meters
    IN->Y
                   yte
    F->M
        -xa > yards→meters
      e t-b             -
     t e y feet→kilometers
    Y->M

    y t            te
   CM->Y
   boc
   F->MM
                 yards→centimeters
    bit->byte →meters
   M->MM
       old-british-fathom
   CM->M
     F->Y
Primacy
                      of
                    Syntax
     one could say that all semantics is being represented as syntax
... semantics has vanished entirely to be replaced with pure syntax.
                                    — John Shutt - “Primacy of Syntax”
Length Specification              - NIST Special Publication 330, 2008 Edition
                 - Checking the Net Contents of Packaged Goods - NIST 133



 Unit of length
 Base unit: meter
 The meter is the length of the path travelled
 by light in vacuum during a time interval of
 1/299,792,458 of a second.

 1 inch == 0.0254 meters
 1 foot == 12 inches
 1 yard == 3 feet
Length Specification              - NIST Special Publication 330, 2008 Edition
                 - Checking the Net Contents of Packaged Goods - NIST 133




 (unit-of ‘length ::meter
 “The meter is the length of the path travelled
 by light in vacuum during a time interval of
 1/299,792,458 of a second.”

  ::inch ‘== 0.0254 ::meter
  ::foot ‘== 12 ::inch
  ::yard ‘== 3 ::foot )
7x6
42
Primacy
   of
 Data
 Acceptable or not, sir, it is the truth.
   — Data - ST:TNG “Coming of Age”
From the DSL

(defunits-of length    ::meter
 “The meter is the length of
the path travelled by light in
vacuum during a time interval
of 1/299,792,458 of a second.”

  ::inch 0.0254
  ::foot [12 ::inch]
  ::yard [3 ::foot])
To a map


{::meter 1,
 ::inch 0.0254,
 ::foot 0.3048,
 ::yard 0.9144}
To another macro
   (defmacro unit-of-length
   [quantity unit]
     `~(*
          (case unit
            ::meter 1
            ::inch 0.0254
            ::foot 0.3048
            ::yard 0.9144)))
To a use

(unit-of-length 1 ::yard)
0.9144
Questions?
                          • Thanks to
                           • Rich Hickey &
                              Clojure/core

                           • Gray Herter
                           • Clojure/dev
                           • Relevance
                           • Manning Publishing
                           • Wife and kids
                           • You
http://joyofclojure.com
        @fogus

Code as data as code.

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 \n
  • #8 \n
  • #9 * This allows you to deal with data in terms of a language-level construct.\n\n
  • #10 Here is a data table.\n
  • #11 Here is a data table.\n
  • #12 * Here is the code to read the data.\n* Where is the data in this picture?\n
  • #13 * Right here.\n* Code _works on_ data. \n* Where is the code?\n
  • #14 * Right here.\n\n
  • #15 * CoffeeClass\n* How to unify the notion of a ResultSet and the Coffee class?\n* Rhymes with &amp;#x201C;misinform&amp;#x201D;\n
  • #16 * This allows you to deal with data in terms of a language-level construct.\n\n
  • #17 * Stitching together of the two models\n* Not here to dis ORMs, heck I&amp;#x2019;ve written my own.\n* The point is code as data.\n
  • #18 \n
  • #19 \n
  • #20 but dealing with this is a pain\n
  • #21 you really want to deal with this code itself\n
  • #22 runs through pre-processor that substitutes the AST in place\n
  • #23 * camlp4 is the preprocessor. As a macro system this is actually not bad. \n* I didn&amp;#x2019;t mention everything (e.g. term-rewriting) \n* But... we came here to talk about...\n
  • #24 * To understand how Pure works is to understand FOIL\n
  • #25 first, outer, inner, last\n
  • #26 * first, outer, inner, last\n* product of two binomials of like terms\n
  • #27 * multiplying 2 binomials\n
  • #28 \n
  • #29 \n
  • #30 \n
  • #31 \n
  • #32 \n
  • #33 \n
  • #34 * With a severe line between\n
  • #35 * with Clojure there is no line\n
  • #36 \n
  • #37 \n
  • #38 \n
  • #39 \n
  • #40 \n
  • #41 \n
  • #42 \n
  • #43 \n
  • #44 \n
  • #45 \n
  • #46 * XML is near ubiquitous\n
  • #47 * Clojure data type are currently not extensible\n
  • #48 \n
  • #49 \n
  • #50 \n
  • #51 \n
  • #52 * This is the conceptual model for data as code\n
  • #53 \n
  • #54 \n
  • #55 \n
  • #56 \n
  • #57 There is nothing particularly unique to Clojure about this... Java example...\n
  • #58 \n
  • #59 \n
  • #60 \n
  • #61 \n
  • #62 \n
  • #63 * The stuff in one is not the stuff in the other.\n
  • #64 but we&amp;#x2019;re dealing at the level of the app. what if the engine was the language itself.\n
  • #65 \n
  • #66 But how do macros work?\n
  • #67 But how do macros work?\n
  • #68 * The key is that in Clojure code is data\n* Homoiconicity a fancy term\n* Sometimes verbosity is fine\n
  • #69 \n
  • #70 \n
  • #71 \n
  • #72 \n
  • #73 \n
  • #74 * With a severe line between\n
  • #75 * The punchline of the talk\n
  • #76 * What does this buy us?\n
  • #77 \n
  • #78 \n
  • #79 It started with Frink.\n
  • #80 I dug into the NIST specifications.\n
  • #81 * The meaning is king. \n* This is the root of all Turing completeness arguments\n* What is the best &amp;#x201C;programming language&amp;#x201D; to describe minderbinder?\n
  • #82 \n
  • #83 What about the inverse?\n
  • #84 And some more?\n
  • #85 And what about abbreviations?\n... And what about other kinds of conversions? You see where this is going?\n
  • #86 And what about abbreviations?\n... And what about other kinds of conversions? You see where this is going?\n
  • #87 And what about abbreviations?\n... And what about other kinds of conversions? You see where this is going?\n
  • #88 And what about abbreviations?\n... And what about other kinds of conversions? You see where this is going?\n
  • #89 And what about abbreviations?\n... And what about other kinds of conversions? You see where this is going?\n
  • #90 All useful semantics become syntax and some langs include unit conv as syntactic elems. But this tightly couples the semantics with the syntax. People say Lisp has no syntax, but that&amp;#x2019;s not true. It has a lot of syntax... in fact it has an infinite syntax.\n
  • #91 If semantics are key then anything that implements the spec is fair game right? The best language is the specification from the manual.\n
  • #92 It&amp;#x2019;s all data. This fact lies at the heart of macrology. So I could generate all of those functions as before, BUT maybe there is a better way.\n
  • #93 what&amp;#x2019;s the fastest and most efficient way to multiply 2 values?\n
  • #94 don&amp;#x2019;t multiply it at all.\n
  • #95 \n
  • #96 \n
  • #97 \n
  • #98 \n
  • #99 \n
  • #100 to a value. now that is cool. now I can autogen the m-&gt;ft and yard-&gt;miles fns and their bodies are constants.\n
  • #101 \n