How to access the first value from an object in C#3.0 - c#-3.0

How can I access the first value from
object t = (object) wsf.LinEst(y.ToArray(), x.ToArray(), false, true);
The output is
object[1..5, 1..2]}
[1, 1]: 0.17134831460674155
[1, 2]: 0.0
[2, 1]: 0.019612696690686725
[2, 2]: -2146826246
[3, 1]: 0.95020429009193053
[3, 2]: 0.82746057986828336
[4, 1]: 76.328205128205056
[4, 2]: 4.0
[5, 1]: 52.261235955056179
[5, 2]: 2.7387640449438226
I need to get only [1,1] 's value i.e. 0.17134831460674155
How to get that.
The linEst return an object only.
I am using C#3.0
Thanks
Thanks

Well, it looks like it's something you can enumerate which returns doubles - although it may not actually implement IEnumerable<double>. Try this (with a using System.Linq; directive at the top of your code):
IEnumerable t = (IEnumerable) wsf.LinEst(y.ToArray(), x.ToArray(), false, true);
double first = t.Cast<double>().First();
However, that won't work if actually it's returning something like an int[][]. Could you tell us more about what it's really returning? Yes, it's declared to just return object, but it's clearly returning something more than that. If you know it will always return an object[,] with 1-based indexes, you could cast to that:
object[,] t = (object[,]) wsf.LinEst(y.ToArray(), x.ToArray(), false, true);
double first = (double) t[1, 1];
If you don't know the lower bounds of the array, you can ask for them programmatically of course... but the LINQ way will probably be simpler.

You can force a cast to the appropriate type.
Instead of doing:
object t = (object) wsf.linEst(etc.)
try
int[,] t = (int[,]) wsf.linEst(etc.)
This is because the linEst function is not simply returning an instance of the Object class.
The output you've included in your question seems to imply that the debugger is able to determine the runtime type of your object t so try and use that information to do the proper cast.
Either that, or wsf.linEst isn't just returning an object and you are adding that (object) text which is forcing the return value to be cast to fit into the object t variable

Related

How to do multiple where query without effect data in TypeORM?

I want to do multiple where query without effect data. I want to get data that include at least 1 data per array. Pseudo code
data =[1,3]
array1 = [1,2]
array2 = [3,4]
if(data.IsIntersect(array1) and data.IsIntersect(array2))
IsIntersect checks are there a intersection beetween arrays
I did so far
queryBuilder.andWhere(
'properties.id IN (:...sizeIds) AND properties.id IN (:...colorIds)',
{ sizeIds: [1, 2], colorIds: [3, 4] },
);
It returns empty because firstly checks properties for 'sizeIds' then it checks for 'colorIds'. For example
properties includes 1,3
check for sizeIds, returns 1
check for colorIds, return empty
How can I do that with typeORM?
How can properties.id be 1 and 3? And if it is, how could 1 or 3 be in both? You're asking for the impossible.
I assume you mean to ask for when properties.id is 1 or 3, because if it is [1,3] then you should use the postgres array syntax {1,3} & the ANY keyword (some variation on this: Check if value exists in Postgres array).
tldr, I think all you need is brackets and OR instead of AND:
queryBuilder.andWhere(
'(properties.id IN (:...sizeIds) OR properties.id IN (:...colorIds))',
{ sizeIds: [1, 2], colorIds: [3, 4] },
);
If properties.id is in fact an array, then please add the entity definition to your question. If you want to merge the rows where properties.id is in the list you will need a GROUP BY (https://orkhan.gitbook.io/typeorm/docs/select-query-builder).

Can I retrieve the second to last value of a stream?

I'm building a function that requires both the previous and the current value of a stream.
I managed to work that around, but I was wondering if that is some way to retrieve the second to last value of it.
You can use rxdart pairwise:
RangeStream(1, 4)
.pairwise()
.listen(print); // prints [1, 2], [2, 3], [3, 4]
You will always get a List containing the current emitted value and the last one as well. Just be aware this will only emit after there are 2 items to be emitted, so if you need the first value ASAP, this might not be the best solution for you.
A simple way to solve this is to just save the emitted value to an external variable, this usually isn't much recommended as Streams are supposed to be encapsulated from external code, but for many cases this would be simpler.
If you really need the first value you can duplicate your stream and consume the first value only once, then let pairwise() do it's magic, here's one solution using the async and rxdart packages:
Stream<int> stream = Stream.fromIterable([0, 1, 2, 3, 4, 5]);
List<Stream<int>> splitted = StreamSplitter.splitFrom(stream);
splitted[0].take(1).listen(print); // prints 0 immediately
splitted[1].pairwise().listen(print); // prints [0, 1], [1, 2], [2, 3], [3, 4], [4, 5]
Of course you can also merge them and get all of it in one stream.

Swift - XCode 7 - userDefaults messing up array on save

I have been carrying values on my app through a global variable declared on the first View Controller. It's value is updated whenever the app is reopened.
When the user is on the same session, the array appends propperly, but when I save it through userDefaults, the elements position on the array get messed up. Can you find out why? I cannot seem to find an answer.
Array on same session: [0, 123456, 789101, 456789]
Array after loading from userDefaults: [(0, 123456, 789101, 456789), 222345] It wraps up everything that was done on parenthesis.
The same happens when I try to save a vector of integers. Is there any way I can save variables on userDefaults to get the following:
Vector before the save: [1, 2, 3]
Vector after the save: [1, 2, 3]
Sorry for not posting the full code, I am currently coding on a VM so the copying is hard. I thought explaining the issue would be better. That's how I've been saving and loading it:
var ACC = ["123456", "987654", "908761"]
NSUserDefaults.standardUserDefaults().setObject(ACC, forKey: "Key")
NSUserDefaults.standardUserDefaults().objectForKey("Key")
For anyone having the same problem, all I had to do was force the object type to String, and equal it at index 0 in order for it to append correctly.
var ACC = ["123456", "987654", "908761"]
NSUserDefaults.standardUserDefaults().setObject(ACC, forKey: "Key")
NSUserDefaults.standardUserDefaults().objectForKey("Key")![0] as? [String]

Why does Data.append(Mutable​Range​Replaceable​Random​Access​Slice<Data>) append slice.count bytes from the beginning of the base collection?

Using Data.append(Mutable​Range​Replaceable​Random​Access​Slice), I expected the bytes within the start/end indexes of the provided slice to be appended onto the Data instance. Instead, it appears Slice.count bytes from the beginning of the Slice.base underlying collection are appended. In contrast, instantiating Data with a slice results in the bytes between the slice's start and end indexes populating the instance.
// Swift Playground, Xcode Version 8.3 (8E162)
import Foundation
var fooData = Data()
let barData = Data([0, 1, 2, 3, 4, 5])
let slice = barData.suffix(from: 3)
fooData.append(slice) // [0, 1, 2]
Data(slice) // [3, 4, 5]
Is this the expected behavior and, if so, what might help me better understand the behavior of Data.append in this context, and its differences from Data.init?
Additionally, given that the docs for Mutable​Range​Replaceable​Random​Access​Slice encourage using slices "only for transient computation", do Data.init and Data.append reference the Slice.base collection or create their own copy of the bytes?
I've filed a JIRA issue, which is likely the best place to continue tracking a possible answer:
https://bugs.swift.org/browse/SR-4473

How exactly does the "let" keyword work in Swift?

I've read this simple explanation in the guide:
The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.
But I want a little more detail than this. If the constant references an object, can I still modify its properties? If it references a collection, can I add or remove elements from it? I come from a C# background; is it similar to how readonly works (apart from being able to use it in method bodies), and if it's not, how is it different?
let is a little bit like a const pointer in C. If you reference an object with a let, you can change the object's properties or call methods on it, but you cannot assign a different object to that identifier.
let also has implications for collections and non-object types. If you reference a struct with a let, you cannot change its properties or call any of its mutating func methods.
Using let/var with collections works much like mutable/immutable Foundation collections: If you assign an array to a let, you can't change its contents. If you reference a dictionary with let, you can't add/remove key/value pairs or assign a new value for a key — it's truly immutable. If you want to assign to subscripts in, append to, or otherwise mutate an array or dictionary, you must declare it with var.
(Prior to Xcode 6 beta 3, Swift arrays had a weird mix of value and reference semantics, and were partially mutable when assigned to a let -- that's gone now.)
It's best to think of let in terms of Static Single Assignment (SSA) -- every SSA variable is assigned to exactly once. In functional languages like lisp you don't (normally) use an assignment operator -- names are bound to a value exactly once. For example, the names y and z below are bound to a value exactly once (per invocation):
func pow(x: Float, n : Int) -> Float {
if n == 0 {return 1}
if n == 1 {return x}
let y = pow(x, n/2)
let z = y*y
if n & 1 == 0 {
return z
}
return z*x
}
This lends itself to more correct code since it enforces invariance and is side-effect free.
Here is how an imperative-style programmer might compute the first 6 powers of 5:
var powersOfFive = Int[]()
for n in [1, 2, 3, 4, 5, 6] {
var n2 = n*n
powersOfFive += n2*n2*n
}
Obviously n2 is is a loop invariant so we could use let instead:
var powersOfFive = Int[]()
for n in [1, 2, 3, 4, 5, 6] {
let n2 = n*n
powersOfFive += n2*n2*n
}
But a truly functional programmer would avoid all the side-effects and mutations:
let powersOfFive = [1, 2, 3, 4, 5, 6].map(
{(num: Int) -> Int in
let num2 = num*num
return num2*num2*num})
Let
Swift uses two basic techniques to store values for a programmer to access by using a name: let and var. Use let if you're never going to change the value associated with that name. Use var if you expect for that name to refer to a changing set of values.
let a = 5 // This is now a constant. "a" can never be changed.
var b = 2 // This is now a variable. Change "b" when you like.
The value that a constant refers to can never be changed, however the thing that a constant refers to can change if it is an instance of a class.
let a = 5
let b = someClass()
a = 6 // Nope.
b = someOtherClass() // Nope.
b.setCookies( newNumberOfCookies: 5 ) // Ok, sure.
Let and Collections
When you assign an array to a constant, elements can no longer be added or removed from that array. However, the value of any of that array's elements may still be changed.
let a = [1, 2, 3]
a.append(4) // This is NOT OK. You may not add a new value.
a[0] = 0 // This is OK. You can change an existing value.
A dictionary assigned to a constant can not be changed in any way.
let a = [1: "Awesome", 2: "Not Awesome"]
a[3] = "Bogus" // This is NOT OK. You may not add new key:value pairs.
a[1] = "Totally Awesome" // This is NOT OK. You may not change a value.
That is my understanding of this topic. Please correct me where needed. Excuse me if the question is already answered, I am doing this in part to help myself learn.
First of all, "The let keyword defines a constant" is confusing for beginners who are coming from C# background (like me). After reading many Stack Overflow answers, I came to the conclusion that
Actually, in swift there is no concept of constant
A constant is an expression that is resolved at compilation time. For both C# and Java, constants must be assigned during declaration:
public const double pi = 3.1416; // C#
public static final double pi = 3.1416 // Java
Apple doc ( defining constant using "let" ):
The value of a constant doesn’t need to be known at compile time, but you must assign the value exactly once.
In C# terms, you can think of "let" as "readonly" variable
Swift "let" == C# "readonly"
F# users will feel right at home with Swift's let keyword. :)
In C# terms, you can think of "let" as "readonly var", if that construct was allowed, i.e.: an identifier that can only be bound at the point of declaration.
Swift properties:
Swift Properties official documentation
In its simplest form, a stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the varkeyword) or constant stored properties (introduced by the let keyword).
Example:
The example below defines a structure called FixedLengthRange, which describes a range of integers whose range length cannot be changed once it is created:
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
Instances of FixedLengthRange have a variable stored property called firstValue and a constant stored property called length. In the example above, length is initialized when the new range is created and cannot be changed thereafter, because it is a constant property.