Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function that works like chr but fails if its argument is not a Unicode scalar value?

I was thinking that there should be a function that works like chr but fails in the case when its argument is not a Unicode scalar value.

I usually write my own function this way:

toUnicode :: Int -> Maybe Char
toUnicode x
  -- Ranges from "The Unicode Standard".
  -- See definition D76 in Section 3.9, Unicode Encoding Forms.
  | x >= 0      && x <= 0xD7FF   = Just (chr x)
  | x >= 0xE000 && x <= 0x10FFFF = Just (chr x)
  | otherwise                    = Nothing

But it would be nice if there's a better way to do that.

like image 466
Gaith Avatar asked Nov 07 '22 04:11

Gaith


1 Answers

is this is something you use to do, you can create an adds modules, like Data.Char.Adds and import whenever you want. It seems like it doesn't exist in the present. I cannot see another workaround.

like image 83
A Monad is a Monoid Avatar answered Nov 15 '22 06:11

A Monad is a Monoid