# Seq view with only one item?

Often I find myself passing in `Seq`

only one element for methods like this:

``````def myMethod(myList: Seq[Int]) = { ... }
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

I usually do it like this:

``````myMethod(List(42))
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

But it seems to me that this might not be the most elegant way, and if there is one thing about Scala that I love, it is its ability to choke my mind by shortening the symbols used when I thought it was impossible.

So, is there a shorter or more elegant representation of a single element `Seq`

than `List(42)`

?

I can imagine a couple of worse options!

``````42 to 42
42 :: List()
```

(adsbygoogle = window.adsbygoogle || []).push({});

```
+3

source to share

2 answers

Probably the shortest built-in way is to use the `Seq`

applicable companion object and write

``````myMethod(Seq(42))
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

This applicable function returns the standard implementation `Seq`

, i.e. A, `List`

and is therefore equivalent to use `List(42)`

.

I'm not sure if there is an implementation `Seq`

with a shorter name, but you can confidently implement your own, or just import `Seq`

(or `List`

) when overlaying the name:

``````scala> import scala.collection.{Seq => S}

scala> S(42)
res0: Seq[Int] = List(3)
```

(adsbygoogle = window.adsbygoogle || []).push({});

```
+3

source

Shorter usage syntax can be obtained with this implicit,

``````implicit def SingletonSeq(i: Int) = Seq(i)
```

(adsbygoogle = window.adsbygoogle || []).push({});

```

Hence,

``````def myMethod(myList: Seq[Int]) = myList.size

myMethod(42)
res: 1
```

(adsbygoogle = window.adsbygoogle || []).push({});

```
+1

source

All Articles
{"ft_lang_label":"__label__en","ft_lang_prob":0.8584006,"math_prob":0.93219215,"size":1077,"snap":"2021-21-2021-25","text_gpt3_token_len":263,"char_repetition_ratio":0.101584345,"word_repetition_ratio":0.0,"special_character_ratio":0.2544104,"punctuation_ratio":0.11659193,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95994246,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-14T00:17:06Z\",\"WARC-Record-ID\":\"<urn:uuid:8a7cfa0d-1aa4-4612-97d9-4cd9f3d81e75>\",\"Content-Length\":\"31481\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:719ca1e8-4734-4451-abd3-0cb93e95c306>\",\"WARC-Concurrent-To\":\"<urn:uuid:9c63c611-1cee-4444-a838-ad2ab260fc4c>\",\"WARC-IP-Address\":\"68.183.23.220\",\"WARC-Target-URI\":\"https://daily-blog.netlify.app/questions/2167686/index.html\",\"WARC-Payload-Digest\":\"sha1:HG4QFLLJMV6ED4XQPBQK3M2LFG4WZRTQ\",\"WARC-Block-Digest\":\"sha1:UQJSDF6WBIHWLFN6RIJYA2PDFTMHYVGC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487611089.19_warc_CC-MAIN-20210613222907-20210614012907-00475.warc.gz\"}"}