What is difference between firstWhere() and indexWhere() in Dart List - list

I come across firstWhere() and indexWhere() while using List in Dart but it seems that these two have the same work. Is there any difference between them. And what is the use case for them??

Documentation of both firstWhere and indexWhere both state it clearly.
firstWhere
Returns the first element that satisfies the given predicate test.
indexWhere
Returns the first index in the list that satisfies the provided test.
The difference is that firstWhere returns the element while indexWhere return the index of the matched test.

Related

Groovy: Iterating a list

I was trying to iterate a list and after googling, I found one solution like below:
value([0,1,2,3,4,5,6].sort{new Random()}?.take(1)[0])
I did not understand this part:
sort{new Random()}
Can someone explain this?
And which class the take method is belonging to?
sort receives a closure to determine the order that you wish. In this example, it makes a random sort.
take comes from Iterable and returns the first n elements.

Reverse the last two elements of a list in Prolog

I am trying to write the following predicate in Prolog while not making use of append/3:
lastTwoReversed(List, ListOf2)
which succeeds exactly when ListOf2 contains the last and the second-to-last elements of List in that order (i.e. reversed).
However, I don't know where to start. Any help is appreciated.
You can write a simple recursive predicate with a base case pattern matching on a list consisting of two elements like so:
last_two_reversed([X,Y],[Y,X]).
Since this is probably homework, I think it's best if you try to write the recursive clause yourself.
Simply use the built-in predicate reverse/2:
last_two_reversed([A,B|T],[Y,X]) :-
reverse([A,B|T],[Y,X|_]).
This will fail for lists with strictly less than two elements. A sensible thing to do would be to make it succeed using those two additional rules:
last_two_reversed([],[]).
last_two_reversed([H],[H]).
First of all, the predicate should fail or succeed with empty list or list with only one element??
In mathematical logic the predicate should return true with empty list and one-element list, because there are no last and second to-last elements to reverse.
So if you want to succeed with empty or one element list you should first start with :
lastTwoReversed([],[]).
lastTwoReversed([X],[X]).
(else don't write the above rules).
Next as base you should write:
lastTwoReversed([X,Y],[Y,X]).
and finally for list of length 3 or greater:
lastTwoReversed([X,Y,Z|T],[X|T1]):-lastTwoReversed([Y,Z|T],T1).
Keep in mind that we write [X,Y,Z|T] to specify for list with 3 or more elements so that doesn't match the previous rules.

OCAML - Is element in the list?

let's say we have a list of elements:
[(a,b); (c,d); (e,f)]
What function would check if element (lets say A, where A=(x,y)) is in the list or not?
Use List.mem to do the search for a match.
let a = (3,4)
List.mem a [(1,2); (3,4); (5,6)]
You can also use List.memq if you want to check if the two items both reference the same entity in memory.
Here's a hint on how to write this yourself. The natural way to to process a list is to initially look at the first element, then check the remainder of the list recursively until you have an empty list. For this problem, you could state the algorithm in English as follows:
If the list is empty then the item is not in the list,
else if the first list element equals the item then it is in,
else it is the answer to (Is the item in the remainder of the list?)
Now you just need to translate this into OCaml code (using a recursive function).
In general, if you can describe what you want to do in terms of smaller or simpler parts of the problem, then writing the recursive code is straightforward (although you have to be careful the base cases are correct). When using a list or tree-structured data the way to decompose the problem is usually obvious.

How does Scala's equals method work in the case of a List?

list1 == list2
To do the above check, will Scala iterate through both lists and call equals on each pair of elements ?
(I am sure, this question has been asked before, but I could not find a good answer with Google & Co)
You can find this out yourself for any method by looking at the Scaladoc and finding out where it's defined, and then looking at the source. If you start with the online docs, you can do this all just with clicking: go to the method, open it up by clicking on the arrow on the left, and you'll see a list of overriding classes. Go to the first one, and look at the source.
Anyway, in this case, GenSeqLike, a supertrait of List and many other collections, defines equals as a canEqual check followed by sameElements. In turn, sameElements checks whether both arguments are LinearSeqs, and if so, calls equals on each pair of elements by splitting the head and tail apart one by one. Otherwise it defaults to using iterators, calling hasNext on each and then comparing the elements with equals.
So, long story short: yes, it calls equals on each pair of elements (stopping as soon as it finds a mismatch).

tell if a given element is in a list of doubles

I've got a list of doubles in the form A = [[1,2],[2,3],[3,4],[5,5]]
What I'm trying to do is create a function that will return true if a number is the first number in any of the doubles. So somefunction(7,A) would return false and somefunction(5,A) would return true.
I've tried this somefunction(A,B) :- A == (B,_) but this doesn't do anything of any use.
member([N,_],As).
But there are two remarks here:
First, do you really want lists like [[1,2],[3,4]]? If you will always have exactly two elements, consider to use pairs. That is, use the functor (-)/2. It is often written Key-Value. You would thus write [1-2,3-4]. Lists of pairs are commonly used, there is keysort/2 which sorts only according to the Key.
Second, a frequent idiom for "name lookup" is the following:
..., P = (Name-Value), member(P, Dict), ...
What is remarkable here is that we do not need a "special" member predicate for lists of pairs or whatever other elements we have. We might search now "by name" or "by value" or both or none ...
Alternatively to doing the recursion yourself, you could use the "higher order" predicate maplist/3 combined with nth0/3 to select only the first elements of the lists and then issue a member/2 call on the result to achieve what you want:
somefunction(Element, List) :-
maplist(nth0(0), List, Firsts),
member(Element, Firsts).
You can do it recursively like this:
Check to see if the value is at the head of the list:
somefunction(A,[[A,_]|_]).
Then recursively check to see if the value is in the rest of the list:
somefunction(A,[BH|BT]) :- somefunction(A,BT).