Pages

Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, March 12, 2018

Daily notes

Learning Spring

I'm following the "Spring 5.0 Project: Building a Travel Website" video course from Pack Publishing.
Today, I've learned some new things from the example used in that course:

STOMP

The example from the chapter related with programming web-sockets uses the so called STOMP protocol.
A quite similar example can be found on the Spring Framework website at https://spring.io/guides/gs/messaging-stomp-websocket/

It is the first time when I hear about STOMP, of course is also the first time programming with web sockets.
So, what's STOMP?

STOMP - Simple (or Streaming) Text Oriented Messaging Protocol

The purpose of this protocol is to "provide an interoperable wire format so that STOMP clients can communicate with any STOMP message broker to provide easy and widespread messaging interoperability among many languages, platforms and brokers".

The specification of the latest version of the STOMP protocol can be found at https://stomp.github.io

There is also a very short article about STOMP on Wikipedia


WebJars


Another quite cool thing I've learned is that one can use the so called webjars for specifying the client-side (javascript) dependencies. WebJars are client-side dependencies packaged as jar archive files.
The available javascript libraries can be found at WebJars home: http://www.webjars.org/.
For example of usage, see the following tutorial http://www.baeldung.com/maven-webjars.


Socksjs-client


https://github.com/sockjs/sockjs-client
A javascript library that provides "a coherent. cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server"

Thursday, September 03, 2015

Javacript, python si c# si operatia de impartire a numerelor intregi

În Python 2.7 găsesc supărător faptul ca împărțirea a doua numere întregi are ca rezultat tot un număr întreg chiar dacă împărțirea celor două numere nu este exactă. De la un limbaj dinamic m-aș fi așteptat să obțin un număr real, precum în Javascript:


C:\Users\>Daniel>node
> var a = 10;
undefined
> var b=3;
undefined
> a/b
3.3333333333333335

Javascript, intern, are doar singur tip de reprezentare a numerelor, 64-bit virgula mobila, acelasi cu double din c#.

Python în schimb suportă și tipul integer. Rezultatul împărțirii a doua numere întregi în Python 2.7 este întotdeauna un număr întreg:

In [1]: a = 10
In [2]: b = 3
In [3]: a/b
Out[3]: 3

Pentru a pentru a obține rezultatul corect trebuie sa convertim unul din operatori la tipul float.

In [5]: a/float(b)
Out[5]: 3.3333333333333335

Python 3 nu are această problemă, rezultatul împărțirii în astfel de cazuri fiind un număr real (true division).

Putem folosi și în Python2.7 operația de diviziune din Python 3  cu un import din __future__:

In [1]: from __future__ import division
In [2]: a = 10
In [3]: b = 3
In [4]: a/b
Out[4]: 3.33333333333333355


În C#, rezultatul împărțirii a doua numere întregi este tot un număr întreg, rezultat care nu ridică întrebări având în vedere faptul ca limbajul este static.


1
2
3
4
int a = 10;
int b = 3;

Console.WriteLine(a/b); // prints 3

Result: 3


REFERENCES:

- Python 2.7 Documentation, https://docs.python.org/2.7/library/stdtypes.html#numeric-types-int-float-long-complex
- Python 3.4 Documentation, https://docs.python.org/3.4/library/stdtypes.html#numeric-types-int-float-complex
- MDN, Numbers, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates
- How can I force division to be floating point in Python? http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python
- Learning Python de Mark Lutz. Capitolul 5, Numeric Types
- Javascript: the Good Parts de Douglas Crockford. Capitolul 2. Grammar
- C# 5.0 in a Nutshell de Joe Albahari, capitolul 2 Numeric Types