all empty Immutable.Lists are identical - immutable.js

Feature or bug: All empty Immutable.Lists test as identical.
For example:
var xxx = new Immutable.List();
var yyy = new Immutable.List();
xxx === yyy; // true
var zzz = yyy.push(1).pop();
zzz === yyy; // true
zzz = Immutable.fromJS([]);
xxx === zzz; // true
I can see why you might want to consider all empty lists as identical, but I also have use cases where just because 2 lists are empty doesn't imply that they are the same. As a counter-example, if I create two Immutable.Lists with the same contents, they do not test as identical.
Is there a way to tell 2 empty Lists apart?
Do you think this is a bug?

I am not an expert in Immutable.js nor a contributor, but I will try to write down some thoughts in immutable data structures.
Immutable.js is optimized for performance. Especially for performing equality checks. The idea behind immutable is, that if we have immutable data structures, we can determine if a structure changed simply by comparing the references. The first thing the implemention of Immutable.is does is checking if the references are equal:
export function is(valueA, valueB) {
if (valueA === valueB || (valueA !== valueA && valueB !== valueB)) {
return true;
}
// ...
}
If they are the same, we can always assume that the structure is still the same, since it is immutable. Making all empty lists equal increases performance here.
What we also need to understand is, that immutable data structures are widely used in functional programming languages (Immutable.js is actually inspired by Scala and Closure). Functional programming languages are much closer to mathematics than other languages. Mathematics is value based, this means in mathematics there are no instances. We would assume that a set containing the elements 1, 2 and 3 is equal a set of 1, 2 and 3. The empty set is always equal the empty set. The empty list equals the empty list.
Some interesting results from the Scala REPL:
scala> List(1, 2, 3) == List(1, 2, 3)
res0: Boolean = true
scala> Nil == Nil
res1: Boolean = true
scala> 1 :: 2 :: 3 :: Nil == List(1, 2, 3)
res2: Boolean = true

Related

The namespace or module 'XXXXX' is not defined, for a function parameter

When attempting to rework a merge sort program, I implemented a match with statement within a function.
let rec merge (array : int[]) (chunkA : int[]) (chunkB : int[]) a b i =
match a, b with
| chunkA.Length, _ -> chunkB
| _, chunkB.Length -> chunkA
| _ when chunkB.[b] < chunkA.[a]
-> array.[i] <- chunkB.[b]
merge array chunkA chunkB a (b+1) (i+1)
| _ -> array.[i] <- chunkA.[a]
merge array chunkA chunkB (a+1) b (i+1)
However, Visual Studio threw the error:
The namespace or module 'chunkA' is not defined.
This is confusing, since 'chunkA' had been stated within the function parameters.
In addition, I am rather new to F# and functional programming in general. If the structure or methodology in my code is not up to par, then please feel free to comment on this as well.
Also, if I'm being thick, please feel free to tell me that as well.
Many Thanks, Luke
As John mentioned, you cannot directly pattern match a numerical value against another variable. The language of patterns allows only constants, constructors and a few other things.
You can write the code using when but then you do not really benefit from the match constrct in any way, because you only have conditions in when clauses. In that case, I'd go for plain old if, because it makes it more obvious what you are doing:
let rec merge (array : int[]) (chunkA : int[]) (chunkB : int[]) a b i =
if a = chunkA.Length then chunkB
elif b = chunkB.Length then chunkA
elif chunkB.[b] < chunkA.[a] then
array.[i] <- chunkB.[b]
merge array chunkA chunkB a (b+1) (i+1)
else
array.[i] <- chunkA.[a]
merge array chunkA chunkB (a+1) b (i+1)
The match construct is very useful if you are pattern matching on more functional data structures - for example if you were writing merge on two lists (rather than arrays), then it would be a lot nicer with pattern matching.
When you use match, you need to use compile time constants.
Something like this is what you want
|aa,_ when aa=chunkA.Length -> ....

How to check if two lists are partially identical haskell

This is my code im trying to check if a list can be paritially identical into another. It is a game of dominoes a Domino=(Int,Int) and a Board = [Domino] and an end either left or right. I'm to check if any domino goes into a board say for example can domino (2,3) go into board [(3,4)(5,6)] is should be able to go to the left end because (2,3) and (3,4) have have a similar element. Here is my code
goesP :: Domino -> Board -> End -> Bool
goesP (h,t) [(h1,t1)] LeftEnd
| h==h1 || t==h1 =True
| otherwise False
goesP (h,t) [(h1,t1)] RightEnd
| h==t1 || t==t1 = True
| otherwise False
The pattern-matching you're using for the board is incomplete. The [(h1,t1)] pattern will only match Boards with one element (a pair (h1,t1)).
This is the same as using the pattern (h1,t1):[], ie. a list (:) containing the element (h1,t1) followed by an empty list [].
If we try to run your code with the examples you gave, (2,3) and [(3,4), (5,6)] (NOTE: you need a comma between the list elements!) we will get the following:
goesP (2,3) [(3,4), (5,6)] LeftEnd
Haskell will try to match these arguments against the patterns in your definition, from top to bottom.
It will check the following pattern first:
goesP (h,t) [(h1,t1)] LeftEnd
The first and third arguments will match, by 'unifying' h with 2, t with 3 and LeftEnd with LeftEnd, but the second will fail to match. The argument [(3,4), (5,6)] is 'syntactic sugar' for the list (3,4):(5,6):[], whilst the pattern [(h1,t1)] is syntactic sugar for the list (h1,t1):[]. We could unify h1 with 3 and t1 with 4, but there's nothing to unify (5,6) with.
Haskell will move on to the next possibility:
goesP (h,t) [(h1,t1)] RightEnd
The first argument will match (with h as 2 and t as 3), but the second argument will fail for the same reason as the previous clause. The third argument will also fail to match, since LeftEnd and RightEnd are different values (but that's the point ;) ).
Haskell will then see that there are no more possibilities, so the program will crash.
To fix this, you need to change the patterns for the second arguments so that Boards with more than one Domino are handled properly.
The case for LeftEnd is quite easy, just change the list of one element (h1,t1):[] to a list of at least one element (h1,t1):_ (I also added the extra = after otherwise):
goesP (h,t) ((h1,t1):_) LeftEnd
| h==h1 || t==h1 = True
| otherwise = False
The case for RightEnd is harder, since we want to compare with the last element of the list, but we only have access to the first. In this case, we can keep your definition which checks single-element lists, but also add another definition which uses recursion: if the list has more than one element, remove the first element and check it again. That way, any non-empty list will eventually be broken down until it only has one element, which your existing pattern can work with (again, I've added a missing =):
goesP (h,t) [(h1,t1)] RightEnd
| h==h1 || t==h1 = True
| otherwise = False
goesP (h, t) (_:xs) RightEnd = goesP (h, t) xs RightEnd
Now Haskell will match [(3,4), (5,6)] (which is sugar for (3,4):(5,6):[]) against (h1,t1):[]. This will fail, since the lists have different lengths. It will then match [(3,4), (5,6)] against _:xs, which will succeed, unifying xs with (5,6):[]. We then run the function again, using xs. This time the (5:6):[] will unify with the (h1,t1):[], so we can check whether the numbers are equal or not.
Also, an observation: goesP is actually overly complicated. You're using "pattern guards" to choose between the value True and the value False; however, pattern guards also require a Bool to work with. In other words, code like this:
| h==h1 || t==h1 = True
| otherwise = False
Can be read as saying "Create the Bool value h==h1 || t==h1; if it is True, then return True. If it is False then return False."
Clearly this is redundant: we can just return the value h==h1 || t==h1:
goesP (h,t) ((h1,t1):_) LeftEnd = h==h1 || t==h1
goesP (h,t) [(h1,t1)] RightEnd = h==h1 || t==h1
goesP (h, t) (_:xs) RightEnd = goesP (h, t) xs RightEnd
UPDATE: Fixed my RightEnd code

Create 1 function from 2 other functions in scala?

This question relates to the scala course from coursera so I want to please ask you to not give me the plain solution that I can copy-paste as this would break the coursera honor code.
This relates to the second assignment.
def Set = Int => Boolean
As it can be seen, Set is a function which returns weather or not the given int is or not part of the set. This is plain and simple so far. However the task asks me to create a union
def union(f: Set, s: Set): Set = ???
This union should return a set that satisfies the condition of both sets.
How could I do something like this:
I thought that such a thing could be done by adding the functions together however the following code:
f + s
Will not compile properly as expected
My question to is:
How would I be able to create a function from 2 other functions?
x => if x == 0 true else false //first
x => if x == 1 true else false //second
And what should equal:
x => if x==0 || x == 1 true else false
I'm not asking for a solution but rather how would I go around building something like this?
As I think you already understand, these Sets are functions that test whether a value meets the criteria for each Set.
The union of such a Set must also be a function that returns a Boolean (as shown by the type signature)
def union(f: Set, s: Set): Set
which (because Set is a type alias) is equivalent to:
def union(f: Int => Boolean, s: Int => Boolean): Int => Boolean
In plain English, union of two sets A and B means: "is the item in A or B".
Your task is to write a function that carries out that plain English specification.
You cannot "add" two functions together (at least, not in a way that is applicable to this question), but you can combine their results.
The Set has form of Set = Int => Boolean. Given the Int function will return true if the value is in a Set.
Well if we want to create a singleton set, we will return new function, which will compare any value passed to it, with the one passed to the function that created it.
The union of two sets, is one set plus the other. It means the element you're looking for must be either in one or the other set. But how do we get the new set, well we return a new function that does just that - checks if an element is either in one set or another.
Remember that in Scala functions can return functions, which may be evaluated later. I think that's the key.
The Set is defined as a function from Int to Boolean, "summing" two Sets won't return a Set object, the union means that one element should be either in one or in the other set but always expressed as a function.
I hope this is not too much, but given an element it should satisfy either f or s.
First of all, it's type Set =. Not def. Set is a type alias not a function definition.
Now, your question. You need a function which, when given two Int =>Boolean combines them with OR and returns a Int => Boolean.
First, how would you do this for two Boolean arguments?
def or(a: Boolean, b: Boolean) = a || b
So now we're half way there. What we have:
A pair of Int => Boolean functions.
A function that takes two Booleans and return a Boolean.
So all we need to do is apply each Set to an Int to get a Boolean and OR the result. The confusion is probably here.
The easiest way to curry a function is to do it explicitly
def union(f: Set, s: Set): Set = {
def doUnion(x: Int) = //apply x to f and s, return OR
doUnion
}
But we can, in Scala, so this inline by declaring an anonymous function
def union(f: Set, s: Set): Set = x => //apply x to f and s, return OR

Functional Programming: Does a list only contain unique items?

I'm having an unsorted list and want to know, whether all items in it are unique.
My naive approach would be val l = List(1,2,3,4,3)
def isUniqueList(l: List[Int]) = (new HashSet()++l).size == l.size
Basically, I'm checking whether a Set containing all elements of the list has the same size (since an item appearing twice in the original list will only appear once in the set), but I'm not sure whether this is the ideal solution for this problem.
Edit:
I benchmarked the 3 most popular solutions, l==l.distinct, l.size==l.distinct.size and Alexey's HashSet-based solution.
Each function was run 1000 times with a unique list of 10 items, a unique list of 10000 items and the same lists with one item appearing in the third quarter copied to the middle of the list. Before each run, each function got called 1000 times to warm up the JIT, the whole benchmark was run 5 times before the times were taken with System.currentTimeMillis.
The machine was a C2D P8400 (2.26 GHz) with 3GB RAM, the java version was the OpenJDK 64bit server VM (1.6.0.20). The java args were -Xmx1536M -Xms512M
The results:
l.size==l.distinct.size (3, 5471, 2, 6492)
l==l.distinct (3, 5601, 2, 6054)
Alexey's HashSet (2, 1590, 3, 781)
The results with larger objects (Strings from 1KB to 5KB):
l.size==l.distinct.size MutableList(4, 5566, 7, 6506)
l==l.distinct MutableList(4, 5926, 3, 6075)
Alexey's HashSet MutableList(2, 2341, 3, 784)
The solution using HashSets is definitely the fastest, and as he already pointed out using .size doesn't make a major difference.
Here is the fastest purely functional solution I can think of:
def isUniqueList(l: List[T]) = isUniqueList1(l, new HashSet[T])
#tailrec
def isUniqueList1(l: List[T], s: Set[T]) = l match {
case Nil => true
case (h :: t) => if (s(h)) false else isUniqueList1(t, s + h)
}
This should be faster, but uses mutable data structure (based on the distinct implementation given by Vasil Remeniuk):
def isUniqueList(l: List[T]): Boolean = {
val seen = mutable.HashSet[A]()
for (x <- this) {
if (seen(x)) {
return false
}
else {
seen += x
}
}
true
}
And here is the simplest (equivalent to yours):
def isUniqueList(l: List[T]) = l.toSet.size == l.size
I would simply use distinct method:
scala> val l = List(1,2,3,4,3)
l: List[Int] = List(1, 2, 3, 4, 3)
scala> l.distinct.size == l.size
res2: Boolean = false
ADD: Standard distinct implementation (from scala.collection.SeqLike) uses mutable HashSet, to find duplicate elements:
def distinct: Repr = {
val b = newBuilder
val seen = mutable.HashSet[A]()
for (x <- this) {
if (!seen(x)) {
b += x
seen += x
}
}
b.result
}
A more efficient method would be to attempt to find a dupe; this would return more quickly if one were found:
var dupes : Set[A] = Set.empty
def isDupe(a : A) = if (dupes(a)) true else { dupes += a; false }
//then
l exists isDupe

Is it considered bad form to execute a function within a conditional statement?

Consider a situation in which you need to call successive routines and stop as soon as one returns a value that could be evaluated as positive (true, object, 1, str(1)).
It's very tempting to do this:
if (fruit = getOrange())
elseif (fruit = getApple())
elseif (fruit = getMango())
else fruit = new Banana();
return fruit;
I like it, but this isn't a very recurrent style in what can be considered professional production code. One is likely to rather see more elaborate code like:
fruit = getOrange();
if(!fruit){
fruit = getApple();
if(!fruit){
fruit = getMango();
if(!fruit){
fruit = new Banana();
}
}
}
return fruit;
According to the dogma on basic structures, is the previous form acceptable? Would you recommend it?
Edit:
I apologize to those who assumed that these functions were meant to be factories or constructors. They're not, they're just placeholders. The question is more about syntax than "factorying". These functions could as well be lambda.
If you want a succinct syntax, several languages allow using the "logical or" for this purpose (C# explicitly provides a coalescing operator, because nulls are not falsy).
Python:
fruit = ( getOrange() or
getApple() or
getMango() or
Banana() )
C#:
fruit = getOrange() ??
getApple() ??
getMango() ??
new Banana();
I can think of two alternatives.
The first is only allowable in languages like yours (PHP?), where single = in a conditional is ok.
if ( (fruit = getOrange()) != null)
elseif ( (fruit = getApple()) != null)
elseif ( (fruit = getMango()) != null)
else fruit = new Banana();
Makes it clear that you are doing a comparison and that the single = are not a mistake.
fruit = getOrange();
if(!fruit) fruit = getApple();
if(!fruit) fruit = getMango();
if(!fruit) fruit = new Banana();
Just like your second example, but gets rid of the ugly extra nesting.
In a strongly-typed language that doesn't equate 0/null to false and non-0/non-null to true, I would say that it's probably safe, but marginally less readable in the general case, where your method names and number of parameters may be larger. I would personally avoid it, except for certain standard idioms, in cases where 0/null equate to false and non-0/non-null to true simply because of the potential danger of confusing assignment with equality checking in reading the code. Some idioms in weakly-typed languages, like C, are so pervasive that it doesn't make sense to avoid them, .e.g,
while ((line = getline()) != null) {
...
}
The problem, as I see it, is not the structure, but the driving rules. Why does getOrange come before getApple, etc?
You are probably more likely to see something more data-driven:
enum FruitEnum
{
Orange, Apple, Mango, Banana
}
and separately,
List<FruitEnum> orderedFruit = getOrderedFruit();
int i = 0;
FruitObj selectedFruit;
while(selectedFruit == null && i <= orderedFruit.Count)
{
fruit = FruitFactory.Get(orderedFruit[i++]);
}
if(fruit == null)
{
throw new FruitNotFoundException();
}
That said, to simplify your code, you can use a coalesce operator:
fruit = getOrange() ?? getApple() ?? getMango() ?? new Banana();
In C or C++, you could write:
return (fruit = getOrange()) ? fruit :
(fruit = getApple()) ? fruit :
(fruit = getMango()) ? fruit :
new Banana();
The reason to avoid both this and your first version isn't "dogma on basic structures", it's that assignment on its own in a condition is confusing. Not all languages support it, for one thing. For another it's easily misread as ==, or the reader might be uncertain whether you really meant it, or perhaps intended ==. Adding != 0 to each condition gets quite dense and wordy.
GCC has an extension to allow:
return getOrange() ? : getApple() ? : getMango() ? : new Banana();
The same thing can often be achieved with || or or (but not in C or C++).
Another possibility is:
do {
fruit = getOrange();
if (fruit) break;
fruit = getApple();
if (fruit) break;
fruit = getMango();
if (fruit) break;
fruit = new Banana();
} while (false);
This goes even better in a language where you can break out of a basic block, which you can with last in Perl, since you can dispense with the do / while(false). But probably only assembly programmers will actually like it.
To answer your question directly: it is usually bad form to have side-effects in conditional statement.
As a work around, you can store your fruit constructors in an array and find the first constructor which returns non-null (pseudocode):
let constructors = [getOrange; getApple; getMango; fun () -> new Banana()]
foreach constructor in constructors
let fruit = constructor()
if fruit != null
return fruit
Its like a null-coalesce operator, but more generalized. In C#, you'd probably use linq as follows:
var fruit = constructors
.Select(constructor => constructor())
.Filter(x => x != null)
.First();
At least this way you can pass your constructors around like a factory class, instead of hard-coding them with the null-coalesce operator.
I don't think anybody's mentioned yet that the first version can be kind of a pain to step through in a debugger. The difference in readability is debatable, but In general, I avoid assignments and function calls in conditionals to make it easier to trace through the execution, when necessary (even if I never need to debug it, someone else may need to modify the code later).