Is there a better way to check existence of object's property? - coffeescript

I have thisif condition.
if object.fruit and object.fruit isnt 'mango'
How can ? be utilised here? I tried using if object.fruit? isnt 'mango', but condition passes when object is empty object.

You want to check for three things:
Ensure the object exists: object?
Ensure the object has the key you're looking for: key of object
Test for the key value: object.key isnt 'mango'
All together that's:
if object? and "fruit" of object and object.fruit isnt 'mango'
You can play around with the values of obj in this demo: http://codepen.io/anon/pen/VLOMpm

Related

kotlin.reflect.KClass.isInstance(value: Any?) not working

I'm trying the follow
"simple string"::class.isInstance(kotlin.String)
But it return false!
I'm using
"org.jetbrains.kotlin:kotlin-reflect:1.3.21"
The another examples also fail
1::class.isInstance(kotlin.Int)
true::class.isInstance(Boolean)
Please, help me to understand it!
You use the API incorrectly, the isInstance function does the opposite check as shown in the documentation:
Returns true if [value] is an instance of this class on a given platform.
The kotlin.Int line does not refer to a type, you miss the ::class.
A possible solution is to flip the declaration:
String::class.isInstance("a string") /// true
Int::class.isInstance(42) /// true
You may also compare KClass objects, e.g. 432::class == Int::class or use KClass functions isSubclassOf and isSuperclassOf
kotlin.String, kotlin.Int, and Boolean are used as values, so they refer to companion objects of the corresponding classes. So the first line checks whether the String companion object is a String and correctly tells you it isn't.

Is conditional binding in swift pass by value or reference?

I've been having some issues with conditional binding returning an invalid (but non-nil) object from the watch accelerometer. I was thinking maybe making a copy of the object could help the problem, but I wasn't sure if that was already occurring. If I use code such as:
if let data = recorder.accelerometerData(from: startDate, to: endDate){...}
is this already creating a copy of the CMSensorDataList object or am I simply getting a reference to it?
It just depends upon whether the type wrapped by the optional was a value type or reference type. If reference type, it's obviously pass by reference. If value type, it's copied (unless CoW, copy-on-write, in which case it's copied if and when it's mutated).
In this case, CMSensorDataList is a class, so it's a reference to that instance, not a copy of it.

Check if a variable/property is a single value or is an object/array

I want to loop through a bunch of objects and check each object's properties to see if it is a single value (string or int) or if it is an object/array (doesn't matter if it contains anything just see if it is an object or array).
I was going to just do a check to see if each property is a string or and integer, but is there a more efficient way?
$object -is [Array]
The -is operator checks whether an object is a certain type.
Edit: maybe what you want is to see if it's a Value Type?
$object.GetType().IsValueType
Update: TamusJRoyce mentioned in a comment that they didn't see an .IsValueType property, and suggested this, which is more idiomatic and might work in more situations:
$value -is [System.ValueType]

Scala how to get object property value given name of property in string?

I wondering how to get object property value given name of property in string in Scala? I saw examples when you get all fields of object using Reflection and iterate over it. But is it possible to call it without iteration? Or may be there is a way to pass object.field to another function without evaluation and evaluate it there and return result?
Kolmar comment give me right direction to call by name function.

After raising IEnumerable.OrderBy(), the source list was not sorted,why?

1.First I defined an extension method for the IEnumerable.Add() like the code below
public static IEnumerable<T> Add<T, TKey>(this IEnumerable<T> enumerable, T value, Func<T, TKey> orderBy)
{
if (enumerable == null)
return null;
if (enumerable is IList<T>)
{
var list = enumerable as IList<T>;
if (!enumerable.Contains(value))
{
list.Add(value);
enumerable = enumerable.OrderBy(orderBy);
}
}
}
2.Then,I raised the extension method like this to sort the itemlist according to the "Date" property when a new item was added to the list:
itemList.Add(item, o => o.Date);
3.After all,it appears that the "itemList" was not sorted.
4.I followed the extension method and found that "enumerable" was a new instance after "enumerable = enumerable.OrderBy(orderBy)" and it was sorted,but the "list" was not.
5.Then I tried to cast the sorted enumerable to list like "list=enumerable.ToList()",both of them("enumerable" and "list") were sorted.
6.After that ,when the call stack went back to the "itemList.Add(item, o => o.Date);",the "itemList" was not sorted at all!!!
Anyone can give me some advices?Thanks a looooooooooooooooooooooooooooooooot!!
I believe your problem is that the reference to enumerable is being passed by value rather than by reference. See Jon Skeet's article about passing parameters by value or reference for more information about what that means. In short, C# passes a copy of the parameter's reference so assigning a new value to parameter does not change the reference of the object that was passed in. To pass a parameter by reference you specify the ref keyword, but I don't think that will work with an extension method. If you're dead set on making this work I would suggest inserting the items into your List in sorted order, probably requiring that T implement IComparable.
Update:
First off, see the Skeet's article it's really quite informative and I will probably only be half as clear as he is. Second, when you pass an object as a parameter to a method you are passing a copy of the reference. This means you can still access members of the object but, the same way that a value type is passed by copy, if you modify the reference (ie assign it a new value) you wont modify the original reference. Specifying ref means that you are passing a reference to the reference and changing the reference (assigning a value to it) will affect the original object.
Neither OrderBy or ToList will affect the source list. When you did this: list=enumerable.ToList() you changed your variable to point to a whole new list instance.
It appears to me that this method does too much. I would keep adding and sorting as separate operations. The fact that this extends IEnumerable but silently does nothing if the target is not an IList is a code smell.