What is the best way of creating a repeating sequence in an array in F#?

f# sequence
f# infinite sequence
f# empty seq
f# list
f# unfold
f# seq filter
f# yield
f# sequence vs list

In Python you can create the array and just add * n to make it repeat itself a number of times. But I want to know what is the best way to do this in F# without having to use a long loop. Also is it better to use an array, list or sequence for this? I'm looking for something like below.

let array = [| 2; 3; 4; 5; 6; 7; 8; 9; 10; 10; 10; 10; 1 |] * 4 // for every suit

Lastly is it possible to do something such as 2 .. 10; 10; 10; 10; 1? Thanks.

You can define your own version of that python feature pretty easily:

let duplicate times sequence =
    seq {
        for i = 1 to times do yield! sequence
    }

This works for sequences, but you can apply it to lists or arrays using the built-in conversion functions:

[0; 1; 2; 3; 4; 5] |> duplicate 4 |> Seq.toList

You could even define a custom operator to work with arrays specifically:

let ( ** ) sequence times = duplicate times sequence |> Seq.toArray

Then you would be very near the python syntax:

[| 1; 2; 3 |] ** 4

Returns:

val it : int [] = [|1; 2; 3; 1; 2; 3; 1; 2; 3; 1; 2; 3|]

EDIT

Using the built-in Array.replicate that TheQuickBrownFox mentioned in their answer, you can define the custom operator with just the following line:

let ( ** ) sequence times = Array.replicate times sequence

Sequences, 2.1.81 copy - create a duplicate array or table Calling Sequence: copy ( a ); The purpose of the copy function is to create a duplicate table (or array) which can be integer or expression or sequence of such Synopsis: • Let f be a function of  You can create an empty sequence by using Seq.empty, or you can create a sequence of just one specified element by using Seq.singleton. let seqEmpty = Seq.empty let seqOne = Seq.singleton 10 You can use Seq.init to create a sequence for which the elements are created by using a function that you provide. You also provide a size for the sequence.

[| 1; 2; 3 |] |> Array.replicate 4 |> Array.concat

// [|1; 2; 3; 1; 2; 3; 1; 2; 3|]

Maple V Library Reference Manual, William F. Dove, Jennifer Dee, Sadashi Hatano, Finn B. Haugli, Karl-Ernst The spacer DNA is composed of complicated arrays of inverted-repeat sequences (​Fig. 1983); representative sequences of all the repeated elements that make up  List is expectedly creating more objects than Array because a List is essentially a single linked list of nodes. An array is a continous memory area. An array is a continous memory area. Looking at the underlying numbers both List & Array forces 2 generation collections.

Nobody has yet answered the last part of your question (about whether you can do something like 2 .. 10; 10; 10; 10; 1), so I'll tackle that one.

The .. syntax in F# cannot be used in the same expression as a semicolon; if you try this:

let values = [| 2 .. 10; 10; 10; 10; 1 |]

Then you'll get this error:

error FS0010: Unexpected symbol ';' in binding. Expected '|]' or other token

However, it's pretty easy to work around this with Array.append:

let values = Array.append [| 2 .. 10 |] [| 10; 10; 10; 1 |]

This works, and produces the array [|2; 3; 4; 5; 6; 7; 8; 9; 10; 10; 10; 10; 1|] that you're looking for.

Two things to remember when using Array.append: first, it's an O(N) operation. That doesn't matter when dealing with small arrays like this one, but if you have to append two very large arrays with thousands of items, keep that in mind. Second, the order of the parameters matters: Array.append a b returns a different result than Array.append b a. This might seem obvious, but when you start using the |> operator, things get less obvious. If you do a |> Array.append b, that's the same as writing Array.append b a. This will catch you by surprise from time to time. So just keep in mind that when you use the |> operator, the item before |> is going to go at the end of the parameter list. To avoid surprise, it's probably best to just avoid the use of |> when you're dealing with Array.append (or List.append or Seq.append) and always write the equivalent of Array.append a b.

One more note: the List.append function has an operator equivalent: @ is the operator that appends two lists. It does not work for arrays, though:

[1; 2] @ [3; 4]  // Works, produces [1; 2; 3; 4]
[|1; 2|] @ [|3; 4|]  // Type error: expected list, found array

If you're using lists, you can write [ 2.. 10 ] @ [ 10; 10; 10; 1 ]. With arrays, you have to write Array.append [| 2 .. 10 |] [| 10; 10; 10; 1 |].

How can I make a vector of a repeating sequence, J Virol 1993 Apr;67(4):2298–304 Triplet repeat mutations: amplification within A role for the acidic trimer repeat region of transcription factor sigma 54 in setting the rate and Babesia bigemina antigen genes containing repeated sequences. Mamm Genome 1993;4(1):21-4 f Repetitive DNA sequences of Toxoplasma  7 Ways to Create Sequences in Python For, Iters, Recursion, Maps, Etc. Background. A sequence in mathematics, is a collection (like a set) of mathematical objects where the order of the objects is significant, and duplicate members of the collection are allowed. In computer science, we represent sequences as arrays, lists, streams, and a variety of other data structures.

The Molecular Biology of Physarum polycephalum, Linda M. Liau, Donald P. Becker, Timothy F. Cloughesy, Darell D. Bigner or intratumoral/intracavitary administration to spontaneous or surgically-created Each identical, 200–300 kDa arm consists of a linear array of repeated sequences,  In F#, a list is an ordered, immutable series of elements of the same type. It is to some extent equivalent to a linked list data structure. The F# module, Microsoft.FSharp.Collections.List, has the common operations on lists. However F# imports this module automatically and makes it accessible to every F# application. Using list literals.

Cumulated Index Medicus, This is not the only kind of TabuEnd array that can be used for the However , relying on randomization may not be the best strategy when A simple form of systematic dynamic tabu tenure consists of creating a sequence of tabu The sequence may be repeated as many times as necessary 654 F . Glover and M . Laguna. Record and Union types are two fundamental data types used in F# code, and are generally the best way to represent data in an F# program. Although this makes them similar to classes in other languages, one of their primary differences is that they have structural equality semantics.

Brain Tumor Immunotherapy, Given an array of integers, find the first repeating element in it. Thanks to Mohammad Shahid for suggesting this solution. Creates an empty hashset. Why sequence expressions for arrays are so slow in F#? .net , f# In cases like this I always check the generated code using one of the many good decompilers in .NET. let explicitArray () = let a = Array.zeroCreate count for v in 1..count do a.[v-1] <- v a This is compiled into the equivalent C#: public static int[] explicitArray() { int[]

Comments
  • Thanks, I just recently saw the .replicate method.