1

have a function that is returning:

>>> ["Just (Number 8000.0)","Just (Number 93.0)","Just (String \"test\")"]

What is the best way to get just the values?

>>> ["8000.0", "93.0", "test"]

The code is trying to parse JSON, using prisms from Aeson.

Code

jsonFile :: FilePath jsonFile = "test.json" getJSON :: IO BS.ByteString getJSON = BS.readFile jsonFile main :: IO () main = do input <- getJSON print $ f input f :: BS.ByteString -> [String] f x = [ show $ (x ^? key "a" . nth 0 . key "b") , show $ x ^? key "a" . nth 0 . key "c" , show $ x ^? key "a" . nth 0 . key "d" ] 
10
  • What is the type of the values under Maybe? Number, String, etc.? Commented Dec 8, 2015 at 3:20
  • 1
    It looks like you have a list of strings, not of Maybe. Perhaps the function you have called mapped show over your data? Commented Dec 8, 2015 at 3:21
  • 1
    Could you post the code for the function that produces the output at the top of your question? Commented Dec 8, 2015 at 3:22
  • @Koterpiller Aeson returns the data type when I use prism to obtain a particular value. so Number is an Int and String is a String Commented Dec 8, 2015 at 3:31
  • 1
    It seems like you somehow want to keep the Nothing values... Replace show $ ... with maybe "Nothing" show $ ... ? This still prints the string "Nothing" for any such values in the list, but prints the Just values without the "Just". Commented Dec 8, 2015 at 5:09

1 Answer 1

8

catMaybes from Data.Maybe will only leave Just values in the list, discarding any Nothings.

(Hint: you can use Hoogle search for [Maybe a] -> [a]).

Updated: if you want to replace Nothing with something else, use fromMaybe with your default value, i.e.

map (fromMaybe "Nothing") (f x)

It also looks like you have strings instead of Maybe inside the list; you'll have to remove show call from each element.

Updated again: let's convert everything to strings!

map (fromMaybe "nothing" . fmap show) 

The outer map applies the transformation to each element. fmap show converts the values inside Just to strings and leaves Nothing alone (note the number 1 converted to a string "1":

> map (fmap show) [Just 1, Nothing] [Just "1",Nothing] 

Then fromMaybe "nothing" unpacks Just values and replaces Nothing with a string of your choice.

> map (fromMaybe "nothing" . fmap show) [Just 1, Nothing] ["1","nothing"] 

I suggest you pay closer attention to types while using Haskell, converting everything to strings removes the benefits of using a well-typed language.

Sign up to request clarification or add additional context in comments.

5 Comments

I am not trying to discard the Nothings, I am trying to remove the string "Just" and "Number" or "String" from the output when printed.
Your probably ought to expand your example to demonstrate the behavior you want when the list contains a Nothing, then.
@ChrisMartin. Having "Nothing" in the list is fine for my purposes.
@matthias You just have to remove show and you won't have any "Just" and such. Just do map show . catMaybes $ f x.
@koterpiller thank you for this. I have learned a lot from your posts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.