Sarathkumar's Profile




  • Asked on January 27, 2019 in Javascript.

    What is the difference between undefined and not defined in JavaScript?

    In JavaScript, if you try to use a variable that doesn’t exist and has not been declared, then JavaScript will throw an error var name is not defined and script will stop executing. However, if you use typeof undeclared_variable, then it will return undefined.

    Before getting further into this, let’s first understand the difference between declaration and definition.

    Let’s say var x is a declaration because you have not defined what value it holds yet, but you have declared its existence and the need for memory allocation.

    > var x; // declaring x
    > console.log(x); //output: undefined
    Here var x = 1 is both a declaration and definition (also we can say we are doing an initialisation). In the example above, the declaration and assignment of value happen inline for variable x. In JavaScript, every variable or function declaration you bring to the top of its current scope is called hoisting.

    The assignment happens in order, so when we try to access a variable that is declared but not defined yet, we will get the result undefined.

    • 3 answers
    • 0 votes
  • Asked on January 27, 2019 in Javascript.

    while keeping the same array (“mutable”):

    Array.prototype.clear = function() {
    while (this.length) {
    FYI Map defines clear() so it would seem logical to have clear() for Array too.

    Or as an Underscore.js mixin:

    clearArray: function(array) {
    while (array.length) {
    Or a simple function:

    function clearArray(array) {
    while (array.length) {
    TypeScript version:

    function clearArray(array: T[]) {
    while (array.length) {
    FYI it cannot be simplified to while (array.pop()): the tests will fail.

    And the tests that goes with it:

    describe(‘Array’, () => {
    it(‘should clear the array’, () => {
    let array = [1, 2, 3, 4, 5];

    // Even with undefined or null inside
    array = [1, undefined, 3, null, 5];

    • 8 answers
    • 0 votes
  • Asked on January 27, 2019 in Javascript.

    I would make a function to test the type of object you are dealing with…

    function whatAmI(me){ return\W/)[2]; }

    // tests
    whatAmI(function(ing){ return ing+” to the global window” }),
    whatAmI(“going to do with you?”)

    // output: Array Object Function String
    then you can write a simple if statement…

    if(whatAmI(myVar) === “Array”){
    // do array stuff
    } else { // could also check `if(whatAmI(myVar) === “String”)` here to be sure
    // do string stuff

    • 7 answers
    • 0 votes
  • Asked on January 27, 2019 in Javascript.

    That seems to be Chrome’s new way of displaying uninitialized indexes in arrays (and array-like objects):

    > Array(100)
    [undefined × 100]
    Which is certainly better than printing [undefined, undefined, undefined,…] or however it was before.

    Although, if there is only one undefined value, they could drop the x 1.
    Google Chrome seems to choose to display sparse array using this undefined x n notation. It will show [undefined, undefined] if it is not a sparse array:

    var arr = new Array(2);

    var arr2 = [];
    arr2[3] = 123;

    var arr3 = [,,,];

    var arr4 = [,];

    var arr5 = [undefined, undefined]

    var arr6 = [undefined]
    arr1 to arr4 are all sparse arrays, while arr5 and arr6 are not. Chrome will show them as:

    [undefined × 2]
    [undefined × 3, 123]
    [undefined × 3]
    [undefined × 1]
    [undefined, undefined]
    note the [undefined x 1] for the sparse array.

    Since you deleted an element, it follows that: If you delete an element from an array, the array becomes sparse.

    • 4 answers
    • 0 votes
  • Asked on January 27, 2019 in Javascript.

    What is a Function Declaration?

    A Function Declaration defines a named function variable without requiring variable assignment. Function Declarations occur as standalone constructs and cannot be nested within non-function blocks. It’s helpful to think of them as siblings of Variable Declarations. Just as Variable Declarations must start with “var”, Function Declarations must begin with “function”.


    function bar() {
    return 3;

    • 7 answers
    • 0 votes
  • Asked on January 27, 2019 in Javascript.

    The variable declaration, var message whose scope is the function hoist(), is hoisted to the top of the function.

    To avoid this pitfall, we would make sure to declare and initialise the variable before we use it:

    function hoist() {
    var message=’Hoisting is all the rage!’
    return (message);

    hoist(); // Ouput: Hoisting is all the rage!

    • 8 answers
    • 0 votes
  • Asked on January 27, 2019 in Javascript.

    Suppose you have the following,

    var myObject = {}; // denotes an Object is being created = “Chennai”;
    myObject.state = “Tamilnadu”; = “Indian Peninsula”;
    console.log(myObject.length); // results in undefined
    But, there is a way to calculate the length in modern browsers.(Chrome, FF4+, IE9)

    // –> [“city”, “state”, “country”]
    // –> 3One can also deal with the issue of associative arrays length matters by keeping track of the length of the array with a associative element called length.


    As you add or remove elements simply add or subtract from the myArrary[“length”] element.

    Then you can do things such as:


    • 5 answers
    • 0 votes
  • Asked on January 27, 2019 in Apache-spark.

    good programmer!”, your brain is evaluating the “she” in the sentence. If “she” was defined earlier, it will evaluate to a pointer to a person. 🙂
    While most languages do this immediately, it’s not very efficient. Not all expressions are worth evaluating. For instance, function parameters might go unused, so we don’t need to evaluate them.
    def someFunction(x, y) =
    return x + 1
    someFunction(3, 3 + 2)
    In the example above there is no point in evaluating 3 + 2, because it’s not used. The style of only evaluating what’s needed is called lazy evaluation, while the opposite (evaluating immediately) is called strict evaluation.
    It’s called “lazy” because the compiler will procrastinate. It will not be a good student and study in advance. Instead, it will postpone the evaluation until it’s absolutely needed, and only then will it say, “Sigh. All right, I’ll evaluate your darn expression.”

    • 10 answers
    • 0 votes
  • def return_string(a, b, c):
    if a == ‘s’ and b == ‘S’ and c == ‘s’:
    return ‘S’
    if a == ‘s’ and b == ‘NS’ and c == ‘s’:
    return ‘S’
    if a == ‘s’ and b == ‘S’ and c == ‘NS’:
    return ‘NS’

    df = sc.parallelize([(‘s’, ‘S’, ‘NS’), (‘?’, ‘?’, ‘?’)]).toDF([‘a’, ‘b’, ‘c’])

    df.withColumn(‘result’, return_string(‘a’, ‘b’, ‘c’)).show()
    ## +—+—+—+——+
    ## | a| b| c|result|
    ## +—+—+—+——+
    ## | s| S| NS| NS|
    ## | ?| ?| ?| null|
    ## +—+—+—+——+
    All arguments should be listed (unless you pass data as struct).
    You should use and not & (you evaluate logical expressions not SQL expressions).
    Conditions should be expressions not lists (non-empty list are always truthy).
    Personally I’d skip all the ifs and use simple dict:

    • 8 answers
    • 0 votes
  • Asked on January 27, 2019 in Apache-spark.

    I have a list of Tuples of type : (user id, name, count).

    For example,

    val x = sc.parallelize(List(
    (“a”, “b”, 1),
    (“a”, “b”, 1),
    (“c”, “b”, 1),
    (“a”, “d”, 1))
    I’m attempting to reduce this collection to a type where each element name is counted.

    So in above val x is converted to :

    (a,ArrayBuffer((d,1), (b,2)))
    Here is the code I am currently using :

    val byKey ={case (id,uri,count) => (id,uri)->count})

    val grouped = byKey.groupByKey
    val count ={case ((id,uri),count) => ((id),(uri,count.sum))}
    val grouped2: org.apache.spark.rdd.RDD[(String, Seq[(String, Int)])] = count.groupByKey

    I’m attempting to use reduceByKey as it performs faster than groupByKey.

    How can reduceByKey be implemented instead of above code to provide the same mapping ?
    Following your code:

    val byKey ={case (id,uri,count) => (id,uri)->count})
    You could do:

    val reducedByKey = byKey.reduceByKey(_ + _)

    scala> reducedByKey.collect.foreach(println)
    PairRDDFunctions[K,V].reduceByKey takes an associative reduce function that can be applied to the to type V of the RDD[(K,V)]. In other words, you need a function f[V](e1:V, e2:V) : V . In this particular case with sum on Ints: (x:Int, y:Int) => x+y or _ + _ in short underscore notation.

    For the record: reduceByKey performs better than groupByKey because it attemps to apply the reduce function locally before the shuffle/reduce phase. groupByKey will force a shuffle of all elements before grouping.Your origin data structure is: RDD[(String, String, Int)], and reduceByKey can only be used if data structure is RDD[(K, V)].

    val kv = => e._1 -> e._2 -> e._3) // kv is RDD[((String, String), Int)]
    val reduced = kv.reduceByKey(_ + _) // reduced is RDD[((String, String), Int)]
    val kv2 = => e._1._1 -> (e._1._2 -> e._2)) // kv2 is RDD[(String, (String, Int))]
    val grouped = kv2.groupByKey() // grouped is RDD[(String, Iterable[(String, Int)])]

    • 9 answers
    • 0 votes