Mostrando entradas con la etiqueta Clojure. Mostrar todas las entradas
Mostrando entradas con la etiqueta Clojure. Mostrar todas las entradas

lunes, 26 de octubre de 2015

(Defn SAP HANA [OData Clojure])

This post was originally posted on (Defn SAP HANA [OData Clojure]).


Clojure is a dynamic programming language that targets the Java Virtual Machine. It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multi-threaded programming.


Also…Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system.
So, before we move on…I want to share an XKCD comic that I really like about Lisp -:) And you will realize why after you see my source code -;)


So…no example or demonstration would be complete if we didn’t hook it up with SAP HANA, right? So…let’s go and do it -;)

First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.

First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.

Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.

Then create a Projection object and select only PRICE and CARRNAME.

On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.


Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.

Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.

Create an empty “.xsapp” file.

Create a file called “.xsaccess” with the following code.

.xsaccess
{
          "exposed" : true,
          "authentication" : [ { "method" : "Basic" } ]
}

Finally create a file called “flights.xsodata” with the following code

flights.xodata
service {
          "Blag/FLIGHTS_BY_CARRIER.calculationview" as "FLIGHTS" keys 
                                                        generate local "Id";
}

Activate your project and launch it on your browser, you should see something like this…


The SAP HANA part is done…so we can move into the Clojure part…

The best and easiest way to use Clojure in to install Leiningen, you can find the installation process here.

Once installed, simply create a project like this…

Create Project
Create Project
lein new clojure_hana
cd clojure_hana
nano project.clj

And modify it to looks like this…

project.clj
(defproject clojure_hana "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
[clj-http "1.1.0"]
[cheshire "5.4.0"]]
:main clojure-hana.core)

This will download and install the clj-http (To deal with web pages) and chesire (To deal with JSon) libraries into your project.

To code for Clojure, you can use any editor that you like…

Inside your clojure_hana folder, do the following…

Create source file
cd src
cd clojure_hana
nano core.clj

Copy and paste the following code…

core.clj
(ns clojure-hana.core
                (:require [clj-http.client :as client])
                (:require [cheshire.core :refer :all]))
 
(defn show_info [results len ctr]
                (cond
                                (or (= ctr len) (< ctr len))
                                                                (concat ((results ctr) :CARRNAME) " : " 
                                                                ((results ctr) :PRICE) "\n"
                                                                (show_info results len (inc ctr)))
                                (> ctr len)
                                                (concat "" "\n")))
 
(defn -main
  "Reads information from SAP HANA OData"
  [& args]
                (let [json (:body (client/get "http://YourHANAServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json" 
                                  {:basic-auth ["YourUser" "YourPassword"]}))
                                  parsed_json (parse-string json true)
                                  results ((parsed_json :d) :results)]
                   (do(print(apply str(show_info results (dec (count results)) 0)) (symbol "")))))

Now you see it…Clojure is all about parenthesis, just like good old Lisp -:) Anyway…to run the project simply go back to your main clojure_hana folder and run

lein run


I have been interfacing SAP HANA OData with a lot of languages…and so far…Racket was one of my fastest implementations…but I have to say…Clojure was even faster to implement and the code is even shorter -:P…while it’s hard to get used to all those weird parenthesis…it’s a very nice and powerful language…and thing get done faster that one could imagine…

Greetings,

Blag.
Development Culture.

viernes, 22 de mayo de 2015

Clojure in Action - Book Review

I just finished reading this book a couple of minutes ago...so here's my small review...


The book is kinda big...with 434 pages...and it's the first book and my first approach to Clojure.

I have to say...I'm not a big fan of Java...I don't even actually like it...but Clojure...it's something else -:) With its Lisp like syntax and it's functional orientation...it's a delightful programming language...

The book itself it's a great introduction to have us ready for something else...but...as Clojure is still a young language...some of the examples don't work mainly because some keywords or libraries had become obsolete...gladly most of the examples work just out of the box...

There are also many examples that help to fully understand what Clojure is all about...





If you haven't heard about closures, recursion, higher-order functions or currying...the this book is for you...

While I'm planning to read more Clojure books, I can say that this book is the perfect way to get started...

Greetings,

Blag.
Development Culture.

miércoles, 1 de abril de 2015

LED is my new Hello World - Clojure Time

The more I learn Clojure...the more I like it...and I'm liking it so much that I couldn't help myself and start working on my beloved "LED_Numbers" application...

After making the Fibonnaci Generator app work...this one wasn't as hard as I expected...actually I think I'm slowly getting used to Clojure...which is always nice when learning a new language -;)

Here's the source code...

LED_Numbers.clj
(def leds {"0" (list " _  " "| | " "|_| ") "1" (list "  " "| " "| ")
           "2" (list " _  " " _| " "|_  ") "3" (list "_  " "_| " "_| ")
           "4" (list "    " "|_| " "  | ") "5" (list " _  " "|_  " " _| ")
           "6" (list " _  " "|_  " "|_| ") "7" (list "_   " " |  " " |  ")
           "8" (list " _  " "|_| " "|_| ") "9" (list " _  " "|_| " " _| ")})

(defn toList [number]
 (map str(seq(str number))))

(defn get_led [x n num]
 (cond 
  (> (count x) 0)
   (concat (nth (get leds (first x)) n) (get_led (rest x) n num))
  (and (= (count x) 0) (< n 2))
   (concat "" "\n" (get_led (toList num) (+ 1 n) num))
  (and (= (count x) 0) (= n 2))
   (concat "" "\n")))

(defn showLED [num]
 (do (print (apply str (get_led (toList num) 0 num))))(symbol ""))

Wanna see it in action? Of course you want to -:)


Well...let's go back and keep learning -:D

Greetings,

Blag.
Development Culture.

My first post on Clojure

My good friend and Programming Languages adviser Chris Whealy, send me tweet saying this...


I knew then that I needed to learn Clojure...after all...Chris was the one who get me into Erlang -;)

So...I started to read this book Clojure in Action so yes...you can expect a review later on...


I have experience with Functional Programming and specially with Lisp-like languages (Racket) so I guess my Clojure experience was going to be an easy one...nope...it's being fun but not easy as I thought...and not because Clojure's learning path is hard...but just because I keep thinking that it's pretty much like Racket when indeed its not so...

Clojure is a Lisp-like language...but being based on Java (on the JVM to be exact) it has some really "weird" things that take some time to digest and assimilate...but of course...that has never discourage me...I have learned Haskell after all...so no challenge is too big enough for me -;) (Ok...maybe ASM...but I'm planning to learn Fasm one of this days...)

As always...I couldn't get myself happy by just reading the book...I needed to write some code for it...and what better than a Fibonacci generator...

fibonacci.clj
(defn fib [num a b]
 (cond 
  (and (> a 0) (> num 1))
   (concat [(+ a b)] (fib (- num 1) (+ a b) a))
  (= a 0)
   (concat (conj (conj [a] b) (+ a b)) (fib (- num 1) (+ a b) b))))

(defn showFib [num]
  (fib num 0 1))

Here's the screenshot..


Obviously...you can see that I based my code on my previous Racket code...but the only similarities that remain are the parenthesis...being a Lisp-like language...you can't expect no to use parenthesis, right? -:P

Greetings,

Blag.
Development Culture.