What are the pros of loose typing? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
JavaScript is said to be a "loosely-typed" language. This is due to the fact that the runtime allows operations to be performed on operands of different types (via coercion):
var number = 6;
var bool = true;
var result = number + bool; //result is 7
Coming from a mostly statically-typed, strongly-typed background, I am having a hard time reasoning about the benefits of this type of approach. Sure, it can make for some pretty concise syntax, but it also seems like it could cause a nightmare when trying to track down bugs. So, besides conciseness, what are some of the benefits of loose typing and implicit type conversions?

Loosely typed languages have a number of differences which can be taken as advantages:
There is no need of interfaces. As long as an object has the method name that you need, call that method. Not using interfaces can simplify coding and reduce code size.
There is no need of generics, for very similar reasons.
"by type" function overloads are handled more simply If a function needs a string parameter, then just cast the incoming value to a string. If type checking is needed, it can be added there.
We don't have, or need, classes. The [almost] everything is an object makes passing values around much easier. No need to auto-box, no need to cast values coming out.
Objects are easily extended without breaking code. You can create an array then drop replace the indexOf method to use one uses the binary search. The end result is smaller, and IMHO, cleaner code.

Related

What is the correct way to hold dynamic strings in a constant file in React? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I am working on a fairly large project and I want to make some changes to the code base to support internationalisation in the future. I have stored most strings in a separate file. But is there a good way to store dynamic strings in React?
Using interpolated strings it is very hard to store in constant. I tried using functions and string interpolations but I feel this is not the correct way.
Some things that I tried:
const STRING = (context: string) => `This is a ${context}`
const STRING2 = `This is a ${context}`
But in second approach we need to have the context defined somewhere and maybe it needs to be passed in the translations file itself.
For context, I am using TypeScript and I am planning to use React i18n for internationalisation.
I tried splitting the dynamic strings in separate constants which work, but is very inefficient especially while translating.

Why does JS keep insertion order in Set? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Randomly experimenting with JavaScript (ES6) and reading its documentation I found out that Set maintains the insertion order of its elements.
I wonder what is the rationale behind this decision? I always thought of a set as an unordered collection. Requiring anything more would lead to a more costly implementation, whereas, in my opinion, this feature is mostly unused.
Ordered Sets are very useful, consider for example:
unique_elements_in_order = [...new Set(some_array)]
In an "unsorted set" language, like python, you'd need a separate OrderedSet implementation for this to work.
Yes, theoretically, sets are not ordered, but mathematical abstractions, like sets, functions, numbers etc, are only tangentially related to similarly named objects we use in programming. Set is just a special kind of data structure, and it's up to the language designers to define its specific properties, like "Sets are in the insertion order" or "Sets can only contain hashable objects" etc.
As to the committee's motivation, some googling brought this
Mark S. Miller:
Mon Feb 13 22:31:28 PST 2012
There are many benefits to determinism. E started with non-deterministic
iteration order, which opens a covert channel hazard. I initially changed
to deterministic order merely to plug this leak. Having done so, I found it
had many software engineering benefits. For example, it becomes much easier
to write regression tests and to reproduce bugs by re-execution. In my
implementation, it also had a minor additional space and time cost. Tyler's
Waterken tables show that even the minor runtime costs I was paying were
unnecessary.
Let's not introduce yet another source of non-determinism for the sake of
an unmeasured efficiency. Let's measure, and if the cost turns out to be
high after all, then let's reconsider determinism.

Loading lookup table from server - Efficient Format [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If I had a python script that created a lookup table which could be read by a webpage (javascript and maybe ajax), what is the most efficient (in speed and if possible size) format to use?
The lookup-table could have 2000 rows.
Here is a data example:
Apple: 3fd4
Orange: 1230
Banana: 942a
...
Even though this is primarily opinion based, I'd like to roughly explain to you what your options are.
If size is truly critical, consider a binary format. You could even write your own!
With the data size you are presenting, we are probably talking megabytes of data (depending on the field values and no. of columns), so the format is of importance. Now, a simple csv or plain text file - provided it can be read by the webpage - is very efficient in terms of the additional overhead: simply seperating the values by a comma and putting the table headers on line 1 is very, very concise.
JSON would work too, but does maintain a somewhat larger overhead than just a raw (text) data dump (like a csv would be). JavaScript object notation is often used for data transfers, but really, in the case of raw data it does not make much sense to coerce it into such a format.
Final thoughts: put it into a relational database and do not worry about it any more. That is the tried and tested approach to any relational data set, and I do not really see a reason you should deviate from that format.

Errors in javascript programs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is there a good classification of standard javascript errors? For example, in java like programs, there are errors like ArrayIndexOutOfRange, resource leaks, race conditions etc.
Also, in Javascript few errors are not reported as exceptions (e.g., divide by zero). Are there any other similar behaviors that are not reported as runtime exceptions in javascript?
MDN has a great article about this, they can put it better than I ever could:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types
Also, good classification here in the ECMA standard:
ECMAScript 5.1 (Current): http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.6
ECMAScript 6 (Coming soon, some features already here in certain browsers): http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.6
In terms of "not being reported as runtime errors", there are some evaluations of expressions that do not halt execution of code but return indicators like NaN, e.g.:
var a = "Hello";
var b = 3;
var c = a / b; // c is "NaN"
You can use the isNaN() function to check for this. Unfortunately I don't know of an offical definite list of these scenarios (if there are more) or even how you would classify them. I guess it comes down to experience and learning the features (or quirks depending on your perspective!) of the language.

Conversion from JavaScript to Python code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there a relatively quick program out there to accomplish at least the basics of this? Just a few regexes? I'm willing to do some manual conversion, but this is a pretty big set of scripts.
You can translate JavaScript to Python using Js2Py. It supports whole JavaScript and you can use it to translate large JavaScript modules like esprima.js (a JavaScript 6 parser).
Short demo:
>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3
This is how the translated function looks like internally (it's rather ugly):
>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'$'])
#Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
var.registers([u'a'])
return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'$'
var.put(u'$', PyJsHoistedNonPyName)
Updated
Now several (4) years later this (almost certainly) can be done; though certainly not with RegEx.
I suggest future readers look to #Piotr Dabkowski's answer..
Or some of the other answers. (I don't know having not tried them)
Original Answer
Hm this is a hard one.
The definition of a compiler is translates from a higher level language to a lower level language.
eg python to machine-code.
or java to javascript (google has a rather famous compiler for this somewhere - its' what makes google doc easier to make)
Python to javascript compilers abound.
technically javascript to python would be a decompiler. (afaik)
I found some speculation about a javascript-python converter here: follow the tread through. it mostly speaks of how it wouldn't be too hard to do.
I can't find anything , but that doesn't mean it's no out there.
Regex is not suitable, regex is suitable only for regular languages.
programming languages are not normally regular languages. see this
This answer might be about 2 years late but how about js -> CoffeeScript -> python? If you use something like http://js2.coffee/ to convert to cs, then things like indentation and lists are already done for you.
If what you are asking is converting a few regexs in javascript to Python equivalent, the basics of regular expressions are mostly pretty standard. check out the Python re module doc. See if what you are using is documented. Also of interest to you is this page.
If you are talking about converting javascript code to Python equivalent, the best converter is you. Learn about Python, and then convert them manually. there is nothing else better than the human being. Programming constructs like loops, variables, arrays are pretty common and standard you will recognize instantly how to use them straight away.

Categories