Defined' and 'Unless' Not Working as Expected

unless defined? is not working in my code

defined? method will return:

nil => expression not recognizable

The problem in the above snippet is the scope of the local variable. Its end on the line where you using it. To learn more about local variable, please check this: local_variable

pry(main)> p "local_var is not initialized" unless defined? local_var
=> "loca_var is not initialized"

but if you do this:

pry(main)> local_var = "initialized" unless defined? local_var
=> nil

local_var is still nil because its scoped end after that line, so whatever assigned were wasted.

Solution: I will suggest if you want this behaviour then use this one:

local_var ||= "initialized"

Ant unless attribute not working?

For Ant 1.7 and prior, the if and unless attributes only check if a property is set. They don't actually check the value. You could in fact set it to anything, and that'll evaluate as true for if and false for unless. Likewise if you don't set it at all, you'll get false for if and true for unless.

In either case I'm not aware of the if and unless being available for <exclude>.

substitute does not work as expected

From the language definition:

Until that argument is accessed there is no value associated with the
promise. When the argument is accessed, the stored expression is
evaluated in the stored environment, and the result is returned. The
result is also saved by the promise. The substitute function will
extract the content of the expression slot. This allows the programmer
to access either the value or the expression associated with the
promise.

A promise stays a promise even if its expression has been evaluated. You can see what happens with the pryr package:

library(pryr)
f=function(x){
print(promise_info(x))
force(x)
promise_info(x)
}

a <- 10
f(a)

#$code
#a
#
#$env
#<environment: R_GlobalEnv>
#
#$evaled
#[1] FALSE
#
#$value
#NULL
############################
#$code
#a
#
#$env
#NULL
#
#$evaled
#[1] TRUE
#
#$value
#[1] 10

Obviously, assigning a new value to the symbol replaces the promise with that value. That's what happens in your last function.

Object property set inside decorator function doesn't persist as expected.'

I recommend to avoid work = spy(work); at first. Instead write

const spiedWork = spy(work);

Then compare what happens when calling work(1,2) and what happens when calling spiedWork(1,2). Also compare spiedWork.calls with work.calls. Once you have this figured out (and that spiedWork !== work), you'll understand what happened in the original code.

Override bean definition doesn't work as expected

You're quite near... First, you cannot have 2 beans with the same name in the same Spring context, unless you're specifically allowing it, which I don't recommend since it's error-prone.

Besides that, you should use the @Primary annotation, which can be applied both at method and type level. In your case, you should apply it at the method level in MyClass2Configuration:

@Configuration
public class MyClass2Configuration {

@Bean
@Primary
public MyInterface myClass2() {
return new MyClass2();
}
}

As you're autowiring by type (and not by name), it doesn't seem to be useful to specifically allow overriding of bean definitions. You could let both beans live within Spring context, and then, by means of the @Primary annotation, Spring will autowire your "primary" bean instance.

Why does AND, OR work as expected but an error is shown with XOR?

The question is who has time to read whole documents like that?

You don't have to read it all in one sitting, but that is where the information is, and Racket has a lot of great documentation.

eval can be a bit slippery in Racket; you need to finish setting up the namespace where the eval expression is evaluated. One way to do this is to use parameterize to create an empty namespace. This needs to happen inside the lambda expression, i.e., the environment in which eval is evaluated, and you need to be sure that both racket/base and racket/bool are required:

#lang racket

(define-syntax e.g.
(syntax-rules (===>)
((e.g. proposition)
(unless proposition
(error "invalid proposition: " 'proposition)))
((e.g. proposition ===> value)
(let ((result proposition))
(unless (equal? proposition value)
(error "invalid proposition: " 'proposition
", expected " value ", got " result))))))

(define my-eval
(lambda (expr)
(parameterize ([current-namespace (make-base-empty-namespace)])
(namespace-require 'racket/base)
(namespace-require 'racket/bool)
(eval expr))))

(e.g. (my-eval '(and #t #t)) ===> #t)
(e.g. (my-eval '(and #t #f)) ===> #f)
(e.g. (my-eval '(or #t #f)) ===> #t)
(e.g. (my-eval '(or #f #f)) ===> #f)
(e.g. (my-eval '(xor #t #t)) ===> #f)
(e.g. (my-eval '(xor #t #f)) ===> #t)

global variables in php not working as expected

Global DOES NOT make the variable global. I know it's tricky :-)

Global says that a local variable will be used as if it was a variable with a higher scope.

E.G :

<?php

$var = "test"; // this is accessible in all the rest of the code, even an included one

function foo2()
{
global $var;
echo $var; // this print "test"
$var = 'test2';
}

global $var; // this is totally useless, unless this file is included inside a class or function

function foo()
{
echo $var; // this print nothing, you are using a local var
$var = 'test3';
}

foo();
foo2();
echo $var; // this will print 'test2'
?>

Note that global vars are rarely a good idea. You can code 99.99999% of the time without them and your code is much easier to maintain if you don't have fuzzy scopes. Avoid global if you can.

DataFrame user-defined function not applied unless I change column name

Let me explain, what is happening on your conversion function with bellow example.
First Create data frame:

val jsonString: String =
"""{
| "employee": {
| "id": 12345,
| "name": "krishnan"
| },
| "_id": 1
|}""".stripMargin

val jsonRDD: RDD[String] = sc.parallelize(Seq(jsonString, jsonString))

val df: DataFrame = sparkSession.read.json(jsonRDD)
df.printSchema()

Output structure:

root
|-- _id: long (nullable = true)
|-- employee: struct (nullable = true)
| |-- id: long (nullable = true)
| |-- name: string (nullable = true)

Conversion function similar to your's:

def myConversion(myDf: DataFrame, colName: String): DataFrame = {
myDf.withColumn(colName + "_tmp", udf((x: Long) => (x+1).toString).apply(col(colName)))
.drop(colName)
.withColumnRenamed(colName + "_tmp", colName)
}

Scenario 1#
Do the conversion for root level field.

myConversion(df, "_id").show()
myConversion(df, "_id").select("_id").show()

Result:

+----------------+---+
| employee|_id|
+----------------+---+
|[12345,krishnan]| 2|
|[12345,krishnan]| 2|
+----------------+---+
+---+
|_id|
+---+
| 2|
| 2|
+---+

Scenario 2# do the conversion for employee.id. Here, when we use employee.id means, data frame got added with new field id at root level. This is the correct behavior.

myConversion(df, "employee.id").show()
myConversion(df, "employee.id").select("employee.id").show()

Result:

+---+----------------+-----------+
|_id| employee|employee.id|
+---+----------------+-----------+
| 1|[12345,krishnan]| 12346|
| 1|[12345,krishnan]| 12346|
+---+----------------+-----------+
+-----+
| id|
+-----+
|12345|
|12345|
+-----+

Scenario 3# Select the inner field to root level and then perform conversion.

myConversion(df.select("employee.id"), "id").show()

Result:

+-----+
| id|
+-----+
|12346|
|12346|
+-----+

My new conversion function, takes struct type field and perform conversion and store it into struct type field itself. Here, pass employee field and convert the id field alone, but changes are done field employee at root level.

case class Employee(id: String, name: String)

def myNewConversion(myDf: DataFrame, colName: String): DataFrame = {
myDf.withColumn(colName + "_tmp", udf((row: Row) => Employee((row.getLong(0)+1).toString, row.getString(1))).apply(col(colName)))
.drop(colName)
.withColumnRenamed(colName + "_tmp", colName)
}

Your scenario number 3# using my conversion function.

myNewConversion(df, "employee").show()
myNewConversion(df, "employee").select("employee.id").show()

Result#

+---+----------------+
|_id| employee|
+---+----------------+
| 1|[12346,krishnan]|
| 1|[12346,krishnan]|
+---+----------------+
+-----+
| id|
+-----+
|12346|
|12346|
+-----+


Related Topics



Leave a reply



Submit