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

miércoles, 23 de septiembre de 2015

(Defining SAP HANA (OData) with Racket)

This post was originally posted on (Defining SAP HANA (OData) with Racket).

Racket is a full spectrum programming language that goes beyond Lisp and Scheme with dialects that support objects, types, laziness and more. And that more means Functional, Procedural, Object Oriented, Logic, Reflective and Meta.

Basically…Racket is the new name of PLT Scheme. And PLT Scheme comes from the Lisp family.

So, before we move on…I want to share an XKCD comic that brings some light about Lisp -:)


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 Racket part…

Racket is a very complete programming language…so no external packages are needed…

To code for Racket I use the best Racket editor “Dr. Racket” that is actually embedded in the Racket installer…so again…no need to worry -:)

Copy and paste the following code…

Racket_HANA.rkt
#lang racket
(require json)
(require net/base64)
(require net/url)

(define make-auth-header
     (list (string-append
       "Authorization: Basic "
       (bytes->string/utf-8
        (base64-encode
         (string->bytes/utf-8
          (string-append "SYSTEM" ":" "YourPassword")))))))

(define (get_path path params)
  (port->string(get-pure-port path params)))

(define json (string->jsexpr (get_path (string->url "YouServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json") 
                              make-auth-header)))
(define results (hash-ref (hash-ref json 'd) 'results))

(define (show_info results len ctr)
  (cond [(or (= ctr len) (< ctr len))
         (string-append (hash-ref (list-ref results ctr) 'CARRNAME) ": "
                        (hash-ref (list-ref results 0) 'PRICE) "\n"
                        (show_info results len (add1 ctr)))]
        [(> ctr len)
         (string-append "" "\n")]))

(display (show_info results (sub1 (length results)) 0))

Now you see it…Racket is all about parenthesis -:) Anyway…press the run button and you will have this…


I have been interfacing SAP HANA OData with a lot of languages…and so far…Racket has been one of my fastest implementations…while it’s hard to get used to all these weird parenthesis…it’s a very nice and powerful language…and thing get done faster that one could imagine…

Greetings,

Blag.
Development Culture.

miércoles, 29 de octubre de 2014

How to Design Programs - Book Review

I just finished reading How to Design Programs, a book meant for beginners that want to get into the wonderful world of programming...and for that they use Scheme and DrScheme (which is now called Racket).



Of course, they don't focus on Scheme but on the design principles of programming...which makes this one a really important book.

The book itself is pretty big...with 565 pages...

There's a lot of really nice examples ranging from really basic to somehow complex, so there's a good learning curve attached to it.

Using Scheme as a starting point for learning programming is in my opinion a really smart choice...the syntax might be kind of weird sometimes but the good thing is that there are not so many keywords so learning it it's not a hard task...



Even if you have experience in programming...this book is really helpful and it goes beyond the simple act of programming as it teaches you how to design, build and test your application...making it more robust and less error prone...

If you have the chance...read this book...you will not regret it -;)

Greetings,

Blag.
Development Culture.

jueves, 16 de octubre de 2014

Realm of Racket - Book Review

I just finished reading Realm of Racket a book about Racket -:)


The book has 316 pages...so it's a big book...

It starts with the basics of Rackets but very quickly jumps into creating games with Racket...which is something really cool...specially if you want to build games with Racket...

The book comes with some nice and funny comic strips telling us the story of Chad and how using Racket he manage to solve some interesting challenges...

One thing that I didn't like too much about the book...is that I believe that some games are too long and even complex which kind of defeat the purpose of being a beginners book...

Anyway...the book comes with a lot of useful information and will make you be right on track with your Racket learning -;)

You will learn about Recusion, Lambdas, Loops, GameUI and Networking...





Racket is a powerful but strange programming language...so this book will help you to get more comfortable...

If you want to learn Racket in a fun way and built some nice games in the process...then this book is for you -:)

Greetings,

Blag.
Development Culture.

miércoles, 15 de octubre de 2014

LED is my new Hello World - Racket Time

If you thought I was going to miss the change to build an LED Number Generator in Racket...you got it all wrong -;)

I have to admit...it wasn't easy...Racket is nice but weird...takes some time to get used to it...but when things work...they just work -;)

Here's the source code...

LED.rkt
#lang racket
(define (showLED num)
  (display(get_led(toList num num 0 1) 1 num)))

(define (toList num num_o start end)
  (cond [(number? num) (toList(number->string num) (number->string num_o) start end)]
        [(and (string? num) (> (string-length num_o) 1)) (append (cons(substring num start end) '())
                                                                (toList num (substring num_o 1 (string-length num_o)) 
                                                                        (add1 start)(add1 end)))]
        [(and (string? num) (= (string-length num_o) 1)) (append (cons(substring num_o 0 1) '()))]))

(define (get_led x n num)
  (cond [(> (length x) 0) (append (cons(make_led_digit (string->number(first x)) n) '()) (get_led(rest x) n num))]
        [(and (= (length x) 0) (< n 3)) (append '("\n") (get_led(toList num num 0 1) (add1 n) num) )]
        [(and (= (length x) 0) (>= n 3)) append '()]))

(define (make_led_digit a b)
  (cond [(and (= a 0)(= b 1)) " _  "]
        [(and (= a 0)(= b 2)) "| | "]
        [(and (= a 0)(= b 3)) "|_| "]
        [(and (= a 1)(= b 1)) "  "]
        [(and (= a 1)(= b 2)) "| "]
        [(and (= a 1)(= b 3)) "| "]
        [(and (= a 2)(= b 1)) " _  "]
        [(and (= a 2)(= b 2)) " _| "]
        [(and (= a 2)(= b 3)) "|_  "]
        [(and (= a 3)(= b 1)) "_  "]
        [(and (= a 3)(= b 2)) "_| "]
        [(and (= a 3)(= b 3)) "_| "]
        [(and (= a 4)(= b 1)) "    "]
        [(and (= a 4)(= b 2)) "|_| "]
        [(and (= a 4)(= b 3)) "  | "]
        [(and (= a 5)(= b 1)) " _  "]
        [(and (= a 5)(= b 2)) "|_  "]
        [(and (= a 5)(= b 3)) " _| "]        
        [(and (= a 6)(= b 1)) " _  "]
        [(and (= a 6)(= b 2)) "|_  "]
        [(and (= a 6)(= b 3)) "|_| "]
        [(and (= a 7)(= b 1)) "_   "]
        [(and (= a 7)(= b 2)) " |  "]
        [(and (= a 7)(= b 3)) " |  "]
        [(and (= a 8)(= b 1)) " _  "]
        [(and (= a 8)(= b 2)) "|_| "]
        [(and (= a 8)(= b 3)) "|_| "]
        [(and (= a 9)(= b 1)) " _  "]
        [(and (= a 9)(= b 2)) "|_| "]
        [(and (= a 9)(= b 3)) " _| "]))

Here's how it looks like when we run it...


Well...gotta keep learning...so...see you soon -;)

Greetings,

Blag.
Development Culture.

lunes, 13 de octubre de 2014

My first post on Racket

In my eternal quest for weird programming languages...I came out finding Racket...which is basically the new name of "PLT-Scheme"...from the Lisp family...


Racket can be installed on Windows, Mac and Linux...so it's pretty cool -:) And also comes with a nice IDE called DrRacket...


I gotta say...Racket is like nothing I have ever seen before...that's why I love it -:D

And here comes the most weird thing of all...Racket is...
  • Functional
  • Procedural
  • Modular
  • Object-Oriented
  • Logical
  • Reflective
  • Meta

Crazy, huh? Right now I'm reading an awesome book called "Realm of Racket"...


And after reading the first 61 pages (yep...no more) I was able to translate my Fibonacci Haskell app into Racket...so here goes the code...

Fibonacci.rkt
#lang racket
(define (showFib num)
  (fib num 0 1))

(define (fib num a b)
  ( cond [(and (> a 0) (> num 1)) (append (cons (+ a b) '()) (fib (sub1 num) (+ a b) a))]
         [(= a 0) (append (append (cons a (cons b '())) (cons (+ a b) '())) (fib(sub1 num)(+ a b) b))]
         [(= 1 num) (append '())]))


Now...if we run it...we're going to see this...


Cool, huh? Works just as expected -:) Couldn't be happier -;)

Greetings,

Blag.
Development Culture.