Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Python's range()

See also std.range.

(1) iota()
def main():
    print(list(range(5)))  # [0, 1, 2, 3, 4]
    print(list(range(5, 10)))  # [5, 6, 7, 8, 9]
    print(list(range(5, 15, 2)))  # [5, 7, 9, 11, 13]

    print(list(range(10, 0, -2)))  # [10, 8, 6, 4, 2]
import std.stdio;
import std.range;  // iota()

void main()
{
    writeln(iota(5)); // [0, 1, 2, 3, 4]
    writeln(iota(5, 10)); // [5, 6, 7, 8, 9]
    writeln(iota(5, 15, 2)); // [5, 7, 9, 11, 13]

    writeln(iota(10, 0, -2)); // [10, 8, 6, 4, 2]
}
  • We convert the range to a list to make it printable.
  • writeln() can print a range, thus converting a range to an array (just for printing) is not necessary.

walkLength

See https://dlang.org/phobos/std_range_primitives.html#walkLength

To figure out the number of elements in a range, use walkLength(). If length() doesn't work, try this :)

(2) walkLength()
import std.algorithm;
import std.range;
import std.stdio;

void main()
{
    auto r = iota(10).filter!(x => x % 2 == 0); // it's a range

    writeln(r); // [0, 2, 4, 6, 8]

    // writeln(r.length);  // Error

    writeln(r.walkLength); // 5
}
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 June 29, 10:56