Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for an empty seq in Nim?

Let's say I have the following sequences:

var s1: seq[int] = @[]
var s2: seq[int]
var s3: seq[int] = nil
var s4: seq[int] = newSeq[int](4)

Which of these are typically considered "empty"? And what is the most idiomatic way to test if they are empty?

Right now I am just checking if len is 0:

proc doSomething(s: seq[int]) =
  if s.len() == 0:
    echo("Your sequence is empty.")
  else:
    # do something
like image 414
Imran Avatar asked Feb 07 '18 06:02

Imran


1 Answers

The strutils module provides an isNullOrEmpty proc for strings: https://nim-lang.org/docs/strutils.html#isNilOrEmpty,string

As you can see in its implementation it just checks for len(s) == 0.

like image 159
def- Avatar answered Oct 15 '22 22:10

def-