Skip to main content

Home/ Haskell/ Group items tagged java

Rss Feed Group items tagged

mountain

Pipes: del.icio.us popular links for programming - 0 views

  •  
    del.icio.us popular links for programming, inculding java, c++, smalltalk, ruby, haskell, ml, schema, calm, ocaml, php, erlang, rails, perl, STM, metaprograming, and DSL
Javier Neira

jaspervdj - Maybe, null, and boilerplate code - 0 views

  • A first goal they serve is as "unassigned variables". I will not cover that here, instead I will focus on another use: abnormal return values.
  • So, null is some sort of "Not found" error here. But wait - they could have used an Exception here!
  • We see a similar behavior here: when the object is found, we get a Just a, and when it is not found, we get Nothing - comparable to a null pointer.
  • ...5 more annotations...
  • So, while we can get a null back from a function, we should never give it to a function.
  • Now, if you have written a lot of Java code, you know that checking for null is a vital part of the job, and fragments like the one above are pretty common.
  • In Haskell, however, Maybe is also a monad - and monads can be used to prevent common patterns in code - in other words, they assist you in the D.R.Y.-principle2. We can therefore write the above snippet again using do-notation3.
  • getPathMonth :: Map String String -> Maybe MonthgetPathMonth m = do p <- lookup "path" m d <- parseDate p return $ getMonth d
  • he whole "check-for-null-and-short-circuit" behavior is defined by the Maybe monad -
  •  
    getPathMonth :: Map String String -> Maybe Month getPathMonth m = case M.lookup "path" m of Nothing -> Nothing (Just p) -> case parseDate p of Nothing -> Nothing (Just d) -> Just (getMonth d) getPathMonth :: Map String String -> Maybe Month getPathMonth m = do p <- parseDate p return $ getMonth d
1 - 4 of 4
Showing 20 items per page