tag:blogger.com,1999:blog-806077456733837244.post7835534215273377612..comments2023-04-27T21:57:37.457+10:00Comments on Lachlan's Rambling: Scala's Missing MonadXhttp://www.blogger.com/profile/13336649267304857235noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-806077456733837244.post-58814814894006701172012-07-08T10:46:45.316+10:002012-07-08T10:46:45.316+10:00For some reason your code snippets aren't rend...For some reason your code snippets aren't rendering…jedhttps://www.blogger.com/profile/03788339539345176707noreply@blogger.comtag:blogger.com,1999:blog-806077456733837244.post-57849612960822445262012-07-08T04:05:05.248+10:002012-07-08T04:05:05.248+10:00@Ken, @Alex: Your solutions only make a shorter Li...@Ken, @Alex: Your solutions only make a shorter List[T] from the original List[Option[T]], while the OP function needed List[Option[T]] => Option[List[T]], where if Some(x), x is of the same length of the original.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-806077456733837244.post-78684118541046216162012-07-08T01:40:33.025+10:002012-07-08T01:40:33.025+10:00Change map to flatMap!
for (x <- xs) yield op...Change map to flatMap! <br /><br />for (x <- xs) yield optionalValue(x) <br /><br />Becomes... <br /><br />for {<br /> x <- xs<br /> value <- optionalValue(x)<br />yield value<br /><br />Or:<br /><br />xs.flatMap(optionalValue(x))Alex Cruisehttps://www.blogger.com/profile/17293235415939008462noreply@blogger.comtag:blogger.com,1999:blog-806077456733837244.post-79820320233207312822010-12-23T13:42:25.525+11:002010-12-23T13:42:25.525+11:00You can also use the collect method, which is like...You can also use the collect method, which is like a map and a filter in one:<br /><br />List(1,2,3) map foo collect {<br /> case Some(s) => s<br />}<br />// Result is a List[String]Ken Scamblerhttps://www.blogger.com/profile/10516060971350809942noreply@blogger.com