
Hello, I have a data type like this data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132 deriving (Show) has you can see it is quite long. I want to write a method in order to parser a string using the show method. myparser :: Parser SpaceGroup myparser = do ... I do not want to write for each constructor something like do t <- takeText if t == "P21" then P21 else ... thanks for your help Frederic

Hello, you may derive an Enum instance for your SpaceGroup type and then generate all enumerations: data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132 deriving (Show, Enum) matches t = t `elem` map show [P1 .. I4132] But of course this should not be as efficient as writing every constructor case by hand. Tobias On 6/21/19 10:05 AM, PICCA Frederic-Emmanuel wrote:
Hello,
I have a data type like this
data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132 deriving (Show)
has you can see it is quite long.
I want to write a method in order to parser a string using the show method.
myparser :: Parser SpaceGroup myparser = do ...
I do not want to write for each constructor something like
do t <- takeText if t == "P21" then P21 else ...
thanks for your help
Frederic _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Hello Frederic, On Fri, Jun 21, 2019 at 08:05:21AM +0000, PICCA Frederic-Emmanuel wrote:
Hello,
I have a data type like this
data SpaceGroup = P1 | P2 | P21 | C2 | P222 | P2221 | P21212 | P212121 | C222 | C2221 | F222 | I222 | I212121 | P4 | P41 | P42 | P43 | P422 | P4212 | P4122 | P41212 | P4222 | P42212 | P4322 | P43212 | I4 | I41 | I422 | I4122 | P3 | P31 | P32 | P312 | P321 | P3112 | P3121 | P3212 | P3221 | P6 | P61 | P62 | P63 | P64 | P65 | P622 | P6122 | P6522 | P6222 | P6422 | P6322 | R3 | R32 | P23 | P213 | P432 | P4232 | P4332 | P4132 | F23 | F432 | F4132 | I23 | I213 | I432 | I4132 deriving (Show)
has you can see it is quite long.
I want to write a method in order to parser a string using the show method.
If you can use `read` data SpaceGroup = P1 | P2 | P21 | C2 | P222 deriving (Show, Read) p = read <$> takeText and if you cannot import Text.Parsec import Text.Parsec.String import Text.Parsec.Char data SpaceGroup = P1 | P2 | P21 | C2 | P222 deriving (Enum, Show) pa :: Parser SpaceGroup pa = let ps = map (\s -> s <$ string (show s)) [P1 ..] in choice ps -- this need to be tweaked Is this what you were looking for? -F

Re-Hello, is there a way to apply a function to all constructors of this this type ? Instead of doing this ? do sg P1 sg P2 sg P21 sg C2 sg P222 sg P2221 sg P21212 sg P212121 sg C222 sg C2221 sg F222 sg I222 sg I212121 sg P4 sg P41 sg P42 sg P43 sg P422 sg P4212 sg P4122 sg P41212 sg P4222 sg P42212 sg P4322 sg P43212 sg I4 sg I41 sg I422 sg I4122 sg P3 sg P31 sg P32 sg P312 sg P321 sg P3112 sg P3121 sg P3212 sg P3221 sg P6 sg P61 sg P62 sg P63 sg P64 sg P65 sg P622 sg P6122 sg P6522 sg P6222 sg P6422 sg P6322 sg R3 sg R32 sg P23 sg P213 sg P432 sg P4232 sg P4332 sg P4132 sg F23 sg F432 sg F4132 sg I23 sg I213 sg I432 sg I4132

On Fri, Jun 21, 2019 at 01:02:26PM +0000, PICCA Frederic-Emmanuel wrote:
Re-Hello,
is there a way to apply a function to all constructors of this this type ?
Instead of doing this ?
do sg P1 sg P2 sg P21 [...]
Use the same `Enum` trick Tobias Brandt described. λ> :t sequence_ $ map print [FirstConstructor ..]

On Fri, Jun 21, 2019 at 12:37:37PM +0000, PICCA Frederic-Emmanuel wrote:
Do you know if a version existe of readMaybe with Text instead of String
Voilà https://hackage.haskell.org/package/readable-0.3.1/docs/Data-Readable.html Maybe is an instance of MonadPlus, so it should fit your bill
participants (3)
-
Francesco Ariis
-
PICCA Frederic-Emmanuel
-
Tobias Brandt