# How to determine a character class

**URL:** <https://users.scala-lang.org/t/how-to-determine-a-character-class/4738>\
**Category:** Question\
**Created:** [June 25, 2019, 11:13am UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738 "2019-06-25T11:13:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [June 25, 2019, 11:13am UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/1 "2019-06-25T11:13:55Z")

</div>

Is there a set of character class predicates in the standard library? I’m looking for something like the following?

```auto
c.isDigit
c.isAlphaNumeric
c.isUpperCase
c.isPunctuation
c.isWhiteSpace
c.isEndOfLine
c.isPrintable

```

etc.

Without such predicates, I’m putting expressions like the following in my code, which of course works for my needs, but there’s probably a better way. AND I can detect end of line in UNIX with `c == '\n'`, but that may not work on other operating systems.

```auto
val digits = Set('0','1','2','3','4','5','6','7','8','9')

if ( digits.contains(c))
   ...
else
   ...

```

---

<div class="post-metadata">

**Author:** ![curoli](https://users.scala-lang.org/user_avatar/users.scala-lang.org/curoli/32/1753_2.png) [@curoli](https://users.scala-lang.org/u/curoli)\
**Post date:** [June 25, 2019, 12:02pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/2 "2019-06-25T12:02:30Z")

</div>

Yes, you can do:

**scala\> ‘1’.isDigit  
res0: Boolean = true**

The class that has these methods is, I think, scala.runtime.RichChar.

The problem with end-of-line is that on some platforms, including Windows, it is two characters.

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [June 25, 2019, 12:40pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/3 "2019-06-25T12:40:30Z")

</div>

so for EOL do I need to do something like this? `Set('\n','\r').contains(c)`

---

<div class="post-metadata">

**Author:** ![sangamon](https://users.scala-lang.org/user_avatar/users.scala-lang.org/sangamon/32/68_2.png) [@sangamon](https://users.scala-lang.org/u/sangamon)\
**Post date:** [June 25, 2019, 12:48pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/4 "2019-06-25T12:48:07Z")

</div>

> [@jimka](#):
>
> Is there a set of character class predicates in the standard library?

Parser libraries will have builtin support for this, of course, either via regexes or as dedicated predicates/lexers/whatever, or both.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [June 25, 2019, 3:01pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/5 "2019-06-25T15:01:28Z")

</div>

I suspect that for a tiny case like that, it’s actually most efficient (and clearest) to just use a plain old comparison:

```auto
(c == '\n' || c == '\r')

```

---

<div class="post-metadata">

**Author:** ![tyohDeveloper](https://users.scala-lang.org/user_avatar/users.scala-lang.org/tyohdeveloper/32/1094_2.png) [@tyohDeveloper](https://users.scala-lang.org/u/tyohDeveloper)\
**Post date:** [June 25, 2019, 4:14pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/6 "2019-06-25T16:14:37Z")

</div>

[https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html](https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html)

Side note. Always use eq for integers and characters.\* Other than 0.0, one should never compare two floating point numbers.0

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [June 25, 2019, 4:44pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/7 "2019-06-25T16:44:29Z")

</div>

are sets containing constants constructed at compile time or run time? `Set(1,2,4,8)` for example?

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://users.scala-lang.org/user_avatar/users.scala-lang.org/martijnhoekstra/32/424_2.png) [@martijnhoekstra](https://users.scala-lang.org/u/martijnhoekstra)\
**Post date:** [June 25, 2019, 5:11pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/8 "2019-06-25T17:11:41Z")

</div>

Those are runtime.

That’s not generally a problem.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [June 25, 2019, 6:36pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/9 "2019-06-25T18:36:45Z")

</div>

> [@tyohDeveloper](#):
>
> Always use eq for integers and characters.

Interesting – why?

---

<div class="post-metadata">

**Author:** ![tyohDeveloper](https://users.scala-lang.org/user_avatar/users.scala-lang.org/tyohdeveloper/32/1094_2.png) [@tyohDeveloper](https://users.scala-lang.org/u/tyohDeveloper)\
**Post date:** [June 25, 2019, 8:47pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/10 "2019-06-25T20:47:50Z")

</div>

@jducoeur  
Integers and characters are scalar values. When boxed, == translates to equals, which then will call eq. So it’s an extra method call.

I’m not sure, but an optimizer could avid boxing entirely. I’m not sure if the scala compiler does this or not. Commercial CLOS and old Smalltalk compilers avoided boxing if eq was used (and inlined the function). Equals requires a function/method. Generally, any identity checks are cheaper than equality checks. The first thing most languages do is an eq. If that passes, it doesn’t need to do anything more. Eq bypasses that extra, often dynamic method call.

With floating point numbers, few computations produce precisely a number. 0, 1, trunc, round, NaN, sNaN, the various infinities are exact numbers. In purely functional computations, a test harness usually can check for exact results. With the caveat that base 10 doesn’t translate to fp binary codes. Numerical codes seldom check for equality. Except to avoid divide by zero.

---

<div class="post-metadata">

**Author:** ![Russ](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Russ](https://users.scala-lang.org/u/Russ)\
**Post date:** [June 26, 2019, 6:03am UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/11 "2019-06-26T06:03:33Z")

</div>

If you can never “compare” two floating point numbers, then floating point numbers are almost completely useless. I think you meant “test for equality” rather than “compare,” but even then it is OK to test for equality in some cases. For example, if the numbers are set based on literals or read from text files and are not further processed, then testing for equality may be perfectly safe. As always, common sense overrules arbitrary draconian rules.

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [June 26, 2019, 6:54am UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/12 "2019-06-26T06:54:09Z")

</div>

Isn’t it better to use equality with equal when you know the results are exact? for example `if (x == 0.5)`. That’s an exact check. or `if (Set(2.0, 1.0, 0.5, 0.25, 0.125).contains(x))`.

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [June 26, 2019, 6:56am UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/13 "2019-06-26T06:56:44Z")

</div>

> [@martijnhoekstra](#):
>
> Those are runtime.
> 
> That’s not generally a problem.

My naive assumption was that a set of literals like `Set(1,2,3)` was allocated (or perhaps eliminated) at compile time or perhaps load time. So `Set(1,2,3).contains(x)` was fast and perhaps even complied to the same code as `(x == 1 || x == 2 || x == 3)`.

---

<div class="post-metadata">

**Author:** ![tyohDeveloper](https://users.scala-lang.org/user_avatar/users.scala-lang.org/tyohdeveloper/32/1094_2.png) [@tyohDeveloper](https://users.scala-lang.org/u/tyohDeveloper)\
**Post date:** [June 26, 2019, 7:11am UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/14 "2019-06-26T07:11:34Z")

</div>

@russ There is never (almost) need to check for equality in numerical codes. Ranges yes. Strict equality, no. One uses \<, \<=, \>=, \> almost exclusively. There are library code to heck if things are close enough to be considered equal.

Think about a convolution, differential equation, an integral. The physicist, engineer, economist, statisticians aren’t looking for an exact result. Computers can’t produce them. Humans can’t either. Pi, e, other kings aren’t representable.A ln or exp function by definition can’t be correct. However, we can calculate the volume of a sphere to enough accuracy and precision to build what we need or analyze things. From subatomic particles to galactic clusters.

== is bad \<= is good.

---

<div class="post-metadata">

**Author:** ![jimka](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jimka/32/646_2.png) [@jimka](https://users.scala-lang.org/u/jimka)\
**Post date:** [June 26, 2019, 11:47am UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/15 "2019-06-26T11:47:44Z")

</div>

> [@tyohDeveloper](#):
>
> Always use eq for integers and characters

If we should always use eq with integers and characters, shouldn’t the compiler do that for us?

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [June 26, 2019, 12:18pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/16 "2019-06-26T12:18:16Z")

</div>

> [@tyohDeveloper](#):
>
> Integers and characters are scalar values. When boxed, == translates to equals, which then will call eq. So it’s an extra method call.

I believe you’re making an incorrect assumption here – scalars aren’t always boxed in Scala. That’s kind of the point of `AnyVal`: it corresponds to value types (as opposed to reference types). This has nothing to do with optimization – it’s a central part of the language.

(They do box _sometimes_, but I don’t see any reason that would happen in a simple situation like this.)

I could be wrong, but AFAIK `==` between `AnyVal`s doesn’t involve boxing, so it should work just fine…

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [June 26, 2019, 12:28pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/17 "2019-06-26T12:28:13Z")

</div>

> [@jimka](#):
>
> My naive assumption was that a set of literals like `Set(1,2,3)` was allocated (or perhaps eliminated) at compile time or perhaps load time. So `Set(1,2,3).contains(x)` was fast and perhaps even complied to the same code as `(x == 1 || x == 2 || x == 3)` .

While that seems theoretically possible, I’m pretty sure that the compiler isn’t _that_ smart. That’s a pretty ornate transformation to infer automatically, and remember, `Set` is a library feature, not a language one.

In practice, I would expect the numbers themselves to be compile-time literals, and it’s a small `Set` so it ought to be efficient. (The standard library specializes very small `Map`s and `Set`s for efficiency: this is unobvious, but buried in the code they’re actually different classes for `Set`s of 1, 2, 3 and 4 members.) But it’s still going to allocate and populate the `Set` and call `contains` on it, so I think the explicit comparison is a good deal faster – although we’re probably talking microseconds here, so it may well be irrelevant…

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://users.scala-lang.org/user_avatar/users.scala-lang.org/martijnhoekstra/32/424_2.png) [@martijnhoekstra](https://users.scala-lang.org/u/martijnhoekstra)\
**Post date:** [June 26, 2019, 2:36pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/18 "2019-06-26T14:36:47Z")

</div>

The scala compiler doesn’t generally know about any libraries, including the standard library (though there are some exceptions).

To the compiler, `Set(1, 2, 3)` is just a method call, and it doesn’t know that it generates a set of values.

---

<div class="post-metadata">

**Author:** ![Russ](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Russ](https://users.scala-lang.org/u/Russ)\
**Post date:** [June 26, 2019, 6:57pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/19 "2019-06-26T18:57:39Z")

</div>

@tyohDeveloper I am well aware of that. In your earlier post, you said that “Other than 0.0, one should never compare two floating point numbers.” The inequality tests are comparisons and, as you later point out, they are perfectly acceptable.

I am well aware of the potential pitfalls of equality testing on processed floating point numbers. But my point was that equality tests are perfectly appropriate in some cases. In particular, if the numbers were based on literals and have not been processed in floating point operations, then testing for equality can be perfectly appropriate and reasonable.

Here’s a trivial example. Suppose I am keeping an inventory of some commodity that comes in packages of several different sizes. Let’s say I am selling coconut oil in containers of 4 different sizes or quantities. It would be perfectly safe to use an equality test to categorize a particular container based on the quantity of oil it contains.

---

<div class="post-metadata">

**Author:** ![martijnhoekstra](https://users.scala-lang.org/user_avatar/users.scala-lang.org/martijnhoekstra/32/424_2.png) [@martijnhoekstra](https://users.scala-lang.org/u/martijnhoekstra)\
**Post date:** [June 26, 2019, 7:14pm UTC](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738/20 "2019-06-26T19:14:16Z")

</div>

Maybe it’s time to split this thread. There are three almost entirely different discussions going on in this thread. Can an admin intervene?

[Next page](https://users.scala-lang.org/t/how-to-determine-a-character-class/4738.md?page=2)
