Identity operators in Swift - swift

If a is identical to c, b is identical to c, why a is not identical to b?
var a = [1, 2, 3]
var b = a
var c = a[0...2]
a === c // true
b === c // true
a === b // false
If a, b, c are constants:
let a = [1, 2, 3]
let b = a
let c = a[0...2]
a === c // true
b === c // true
a === b // true

You can remove the import Cocoa or import UIKit if you are playing with PlayGround to make it correct. It seems there is some type map thing in the Cocoa framework to mess things up. It should be a bug, I think.

As #onevcat said, it's might be a bug of Playground. And if you change a to objects of reference type, all the identity tests will be true.
class K {}
var a = [K(), K(), K()]
var b = a
var c = a[0...2]
a === c // true
b === c // true
a === b // true
it means that a, b & c share the same storage and elements.

Interesting, my guess is that since c is a var in the first case, its mutable and thus it has to make a copy. That way if you add on to c it wouldn't modify a. In the second case, they are all immutable so they can point to the same memory space

Related

Explanation of class instance declarations

I am following a tutorial and found this code:
data A = B | C deriving(Eq)
class K a where
f :: a -> Bool
instance K A where
f x = x == C
f _ = False
call = f B
Why do I need f _ = False? I get the same result without it.
The answer is simply: you don't need f _ = False here. In fact, if you compile with -Wall then the compiler will warn you that this clause is redundant, because the f x = ... clause already catches everything.
If the tutorial told you to have that extra clause, well, it's wrong.
As pointed out, it's not necessary.
You might need (or want) that line, though, if you had a slightly different definition, one that does not require an Eq instance:
data A = B | C
class K a where
f :: a -> Bool
instance K A where
f C = True
f _ = False
Instead of comparing x to C, you can match the argument directly against C, then define f to return False for all other values. This makes more sense if there were more constructors that could produce False.
data A' = B | C | D
instance K A' where
f C = True
f _ = False -- in place of f B = False and f D = False

Assign same value to multiple variables in Scala

I have 3 variables that have already been initialized, and I want to assign a new value to all three of them. e.g.
var a = 1
var b = 2
var c = 3
and I want to reassign them to the same value e.g.
a = b = c = 4
But the above expression is invalid. Is there a right way to do this in Scala?
It is possible to slightly shorten the var definition code as follows
var (a, b, c) = (1, 2, 3)
This works because of extractor objects in scala. A tuple of 3 is extracted into 3 components it was created with.
But following does not work becase the extraction is applied on val or var definitions.
(a, b, c) = (4, 4, 4)
You can do this:
var Seq(a, b, c) = Seq.fill(3)(4)
As with Ivan's solution, this only works when declaring vars or vals, not when reassigning. Since the second parameter is computed for each element, it even works well for mutable objects:
import scala.collection.mutable.ListBuffer
var Seq(a, b, c) = Seq.fill(3)(ListBuffer[Int]())
a += 1 // Only modifies a, not b or c.
By contrast, something like a = b = c = [] will only create one list in most programming languages (e.g. Python, JavaScript, Java). If you don't want to create your object each time (perhaps because it is immutable and creation is expensive), declare it as val first to prevent this behavior:
val largeObject = ???
var Seq(a, b, c) = Seq.fill(3)(largeObject)

Accessing static properties in Coffeescript

With the following code in mind
a.coffee
B = require './b'
C = require './c'
console.log B.someStaticVar
C.checkB()
b.coffee
C = require './c'
class B
#someStaticVar: 1
module.exports = B;
c.coffee
B = require './b'
class C
#checkB: ->
console.log B.someStaticVar
module.exports = C
I am trying to understand why the static property of b is undefined when accessed by c but returning 1 when accessed by a
Output:
$ coffee a.coffee
1
undefined
It looks like you have a circular reference.
A loads B
B loads C
C loads B
But B isn't done loading, so here it's an empty object
C finishes loading
B finishes loading
A loads C - it's already loaded so it just gets a reference.
A finishes loading, your console.log lines at the end of the file are executed.
Here is a version of your 3 modules that illustrates this better:
a.coffee
B = require './b'
C = require './c'
console.log B.someStaticVar
C.checkB()
b.coffee
C = require './c'
console.log 'in b.coffee, we have loaded C: ', C
class B
#someStaticVar: 1
module.exports = B;
c.coffee
B = require './b'
console.log 'in c.coffee, we have loaded B: ', B
class C
#checkB: ->
console.log B.someStaticVar
module.exports = C
You have two options for fixing this cyclic dependency in commonjs:
1. Lazy loading
Don't require ./b in c.coffee until you execute your function. By the time you call C.checkB inside a.coffee B will have been fully loaded and the correct class will be returned from the require call
class C
#checkB: ->
B = require './b'
console.log B.someStaticVar
module.exports = C
2. Refactoring
B and C are tightly coupled. Consider rewriting them to be contained within a single file. You could just remove the require './c' from b.coffee. Though I'm guessing it's in this example as your code is more complex and does have a need for it.
a.coffee
{ B, C } = require './b'
console.log B.someStaticVar
C.checkB()
b.coffee
class B
#someStaticVar: 1
class C
#checkB: ->
console.log B.someStaticVar
module.exports =
C: C
B: B

Scala closure Lexical Scope

class Cell(var x: Int)
var c = new Cell(1)
val f1 = () => c.x /* Create a closure that uses c */
def foo(e: Cell) = () => e.x /* foo is a closure generator with its own scope */
// f2 wont do any reference/deep copy
val f2 = foo(c) /* Create another closure that uses c */
val d = c /* Alias c as d */
c = new Cell(10) /* Let c point to a new object */
d.x = d.x + 1 /* Increase d.x (i.e., the former c.x) */
// now c.x refers to 10
println(f1()) /* Prints 10 */
println(f2()) /* Prints 2 */
Here the f2() prints 2 , As scala wont do deep copy, why the value is still persisted as 1, it should be 10.. where i am going wrong
2) I had read smomehere, Closure in scala dont deep copy the objects, they just keep reference to the object. what do it exactly mean
Your example is somewhat tough to understand due to the way you copied it in (it looks like all the code is run when a Cell is created, but you'd get infinite recursion if that were true). The reason f1 and f2 return different results is that they are pointing at different Cells. You are right that when you write:
val d = c
both c and d contain the same reference. But when you write:
c = new Cell(10)
c is now a reference to a new cell, and d won't copy over that reference.
It's easier to see this with REPL, which can print hexadecimal reference locations.
scala> class Cell(var x: Int)
defined class Cell
scala> var a = new Cell(5)
a: Cell = Cell#368239c8
scala> val b = a
b: Cell = Cell#368239c8
We can see that a and b contain references to the same cell.
scala> a.x = 10
a.x: Int = 10
scala> b.x
res0: Int = 10
When we update the class referenced by a, it also updates for b.
scala> a = new Cell(7)
a: Cell = Cell#5b87ed94
scala> b
res1: Cell = Cell#368239c8
scala> a.x
res2: Int = 7
scala> b.x
res3: Int = 10
When we assign our variable a to a new cell, it has a different reference location (it is a different instance of Cell). b still has the same reference (why wouldn't it?).

An algorithm that determines if a function f from the finite set A to the finite set B is an onto function.

How can I write an algorithm that determines if a function f from the finite set A to the finite set B is an onto function.
This is what I have so far:
A: array ( members of set A )
B: array ( members of set B )
Mapped: associative array of Boolean variables.
for each b in B:
Mapped[b] = false
for each a in A:
Mapped[f(a)] = true
Onto = true;
for each b in B:
Onto = Onto AND Mapped[b]
return Onto
Is this correct?
Yeah, that'll work. A potentially easier approach would be
for each a in A:
remove f(a) from B
return (is B empty?)
And then of course you should sort B first, so you can remove more quickly.