Seq count number of elements - count

What is the easiest way to count the number of elements in a sequence in F#?

Use Seq.length
Returns the length of the sequence

open System.Linq
mySeq.Count()

FYI, if you search the library docs for Seq for "-> int" you'll find this rather quickly.

you can use Seq.length

Related

Is there a Rust function which counts frequencies in a Vec?

I want to count the frequencies of all elements in a given Vec, e.g. something like
count_frequencies(vec![1,1,1,4,1,2,3,5,4])
should return the following Vec:
[(1,4), (2,1), (3,1), (4,2), (5,1)]
(the order does not matter). While I know how I could implement such a function, it seems to me like there should already be an existing implementation in some crate. After some googling, I only found a crate named frequency, but didn't find any example in the documentation.
So, my question is: is there a crate that can achieve this task and, if so, how can I use it?
Edit: If you as well know a function which goes in the other direction, I would also be interested in that :)
Itertools offers counts, but you'd have to convert the result (a HashMap) into a Vec yourself.
Alternatively, sort the vector (costs O(n log n), but may - in practice - be faster than a HashMap-based approach) and use dedup_with_count on the sorted vector.
It isn't really needed because it is one-liner anyway:
let frequencies = v
.iter()
.copied()
.fold(HashMap::new(), |mut map, val|{
map.entry(val)
.and_modify(|frq|*frq+=1)
.or_insert(1);
map
});
There are different requirements for different tasks so there is no need to make some standard method for this.

Vector as an exponent in Julia

I have very basic question. I am new to Julia and used to code in R a lot. I need to take a scalar to the multiple powers, represented by a vector: 3^[2,3]. I got an error "Method error: no method matching ^...". I tried 3^Array([2,3]), but got the error again. At the same time, 3*[2,3] works as expected. Is there any way to do it in Julia without using for loop?
I think you are looking for the . or broadcast functions that allow you to apply any other functions elementwise!
3 .^ [2,3] or broadcast(^, 3, [2,3])
Small edit: you'll need a space after the number to be exponentiated, e.g. 3 .^[2,3].

How to implement symbol-length procedure in scheme

I am new to functional programming and trying to implement a procedure that returns the length of a symbol. Here is what i think: I give one parameter to it named "inSym" to return its length.
(define symbol-length (lambda (inSym) ( ...) )
But, i do not know how do i iterate over the inSym to find the number of characters in it. Can anyone give some help? Note that i do not want to use any built in functions or convert symbol into a string.
Thank you
You don't iterate over the symbol. Instead, convert the symbol to a string (symbol->string) and get its length (string-length).

ASP regular expression only allow numbers bigin with either 7 o 8

I need a regular expressin in asp.net , that will check my account number field that only allows numbers that begins with a 7 or a 8.
[Edit] This will work for your given case. I have tested it in RegexBuddy.
^(?:7|8)\d{6}-\d{7}
You can try liks this (partial example)
^(7|8)
you can use Regex.IsMatch Method
if (Regex.IsMatch(AccountNumber, #"^[7-8]+$"))
Also, it will be helpful for you to go through these articles
Regular Expression Basic Syntax Reference
Learn how to write a Regular Expression
Exact solution to your 14-digit format:
^[78]\d{6}-\d{7}$
^[78][0-9]*
Expression should start with 7 or eight, then have 0 or more numbers.
you can try this one; I'm padding my answer here to go over the 130 character minimum length imposed by stack overflow
^[7-8]+$

std::unique analogue in Qt?

I have browsed the documentation, but didn't find one.
Any ideas?
You should be able to just apply std::unique to the iterators of QList. std::unique just requires the iterators to be forward iterators (here and here), and it appears that QList's iterators meet that requirement.
Consider using a QSet instead (and use QSet::toList when you need it as a list).
This is how I created my unique list of integers:
list=QSet::fromList(list).toList();
No need to add STD, it might not be the best way using very large integers though
By now I have the following:
//filter out duplicates: stl algorithm 'unique' would be useful here
QList<int> uniqueIDs;
qStableSort(res);
foreach(int id, res)
if ( (uniqueIDs.empty())
|| (uniqueIDs.back() != id))
uniqueIDs.push_back(id);
swap(res, uniqueIDs);
res being the input for filtering, and not pleased with it.

Resources