Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Sequence Generation?

Tags:

c#

linq

sequence

I'm looking for an ultra-easy way to generate a list of numbers, 1-200. (it can be a List, Array, Enumerable... I don't really care about the specific type)

Apparently .Net 4.0 has a Sequence.Range(min,max) method. But I'm currently on .Net 3.5.

Here is a sample usage, of what I'm after, shown with Sequence.Range.

public void ShowOutput(Sequence.Range(1,200));

For the moment, I need consequitive numbers 1-200. In future iterations, I may need arbitrary lists of numbers, so I'm trying to keep the design flexible.

Perhaps there is a good LINQ solution? Any other ideas?

like image 544
abelenky Avatar asked Jul 28 '09 21:07

abelenky


People also ask

What are the two types of sequence models?

Types of sequence modelsSequence-to-one: In sequence-to-one sequence model, the input data is sequence and output data is non sequence. Sequence-to-sequence: In sequence-to-sequence sequence model, the input data is sequence and output data is sequence.

What is sequence 2 sequence model?

A Seq2Seq model is a model that takes a sequence of items (words, letters, time series, etc) and outputs another sequence of items. Seq2Seq Model. In the case of Neural Machine Translation, the input is a series of words, and the output is the translated series of words.

What is sequence generation in machine learning?

SEquence generation tasks aim at generating a target. sequence conditioning on a source input. Standard se- quence generation tasks include neural machine transla- tion [1], [2], image captioning [3], and automatic speech.

What is sequence sequence architecture?

Sequence to Sequence (often abbreviated to seq2seq) models is a special class of Recurrent Neural Network architectures that we typically use (but not restricted) to solve complex Language problems like Machine Translation, Question Answering, creating Chatbots, Text Summarization, etc.


3 Answers

.NET 3,5 has Range too. It's actually Enumerable.Range and returns IEnumerable<int>.

The page you linked to is very much out of date - it's talking about 3 as a "future version" and the Enumerable static class was called Sequence at one point prior to release.

If you wanted to implement it yourself in C# 2 or later, it's easy - here's one:

IEnumerable<int> Range(int count)
{
    for (int n = 0; n < count; n++)
        yield return n;
}

You can easily write other methods that further filter lists:

IEnumerable<int> Double(IEnumerable<int> source)
{
    foreach (int n in source)
        yield return n * 2;
}

But as you have 3.5, you can use the extension methods in System.Linq.Enumerable to do this:

var evens = Enumerable.Range(0, someLimit).Select(n => n * 2);
like image 151
Daniel Earwicker Avatar answered Sep 27 '22 23:09

Daniel Earwicker


var r = Enumerable.Range( 1, 200 );
like image 22
JP Alioto Avatar answered Sep 28 '22 00:09

JP Alioto


Check out System.Linq.Enumerable.Range.

Regarding the second part of your question, what do you mean by "arbitrary lists"? If you can define a function from an int to the new values, you can use the result of Range with other LINQ methods:

var squares = from i in Enumerable.Range(1, 200)
              select i * i;
like image 21
dahlbyk Avatar answered Sep 28 '22 01:09

dahlbyk