News for hackers. Stuff that matters.
Re:Scala's Option monad versus null-conditional operator in C# by louthy on Saturday February 20, 2016 @04:26PM (#11142030)
C# has native support for monads (LINQ), so it's trivial to implement an Option monad.

Or you can go the whole hog like I have my language-ext project [1][2].

In C# it would look like this:

    var city = from author  in article.author
               from address in author.address
               from city    in address.city
               select city.ToLower();
Which is arguably even more elegant than the Scala 'for' notation.

As an aside, there was a discussion [3] on one of the language-ext issues about my Try<T> implementation, and I was asked to compare it to Scala's Try monad. The crux of it was that it's perfectly possible to capture the majority of the use-cases, but may be of interest in the context of this discussion.

[1] https://github.com/louthy/language-ext

[2] https://github.com/louthy/language-ext/blob/master/LanguageE...

[3] https://github.com/louthy/language-ext/issues/33#issuecommen...

Re:Scala's Option monad versus null-conditional operator in C# by airless_bar on Saturday February 20, 2016 @05:06PM (#11142180)
Implementing a monad is only 30% of the deal.

Being able to abstract over monads (and other type constructors) ... that's the part that really matters.

C# is not expressive enough for that.

Re:Scala's Option monad versus null-conditional operator in C# by louthy on Saturday February 20, 2016 @05:11PM (#11142201)
It doesn't do higher-kinded polymorphism, no. If you're referring to something else, please elaborate on what it can't express. But for the example given in the blog piece it's exactly as expressive and concise - so it's not giving C# a fair hearing.

It is actually possible to get most of the way to HKT using extension methods. But it involves a hell of a lot of typing. In my project I used a template generator to do it. It's still pretty limited though, and there isn't a single type-class called Monad.

Re:Scala's Option monad versus null-conditional operator in C# by airless_bar on Saturday February 20, 2016 @05:29PM (#11142275)
> It doesn't do higher-kinded polymorphism, no.

That's what I referred to.

> ...

The point is that you can't leverage all the existing monad/functor/applicative/free/yoneda... libraries out there–they don't exist, because they can't be written in a way that would allow re-use.

Re:Scala's Option monad versus null-conditional operator in C# by louthy on Saturday February 20, 2016 @05:40PM (#11142314)
> all the existing monad/functor/applicative/free/yoneda... libraries out there–they don't exist

Agreed. I can only hope one day that the CLR team decide it's a reasonable thing to support so that C# (and F#) can both support it. It seems to be the blockage to the language teams (they won't move without it, even if they could implement it in a weaker/slower way on top of the existing type system). I've jumped through many hoops to get as much of a functional 'base class library' as possible in C# (I needed it for a very large C# project I maintain and want to bring more stability to), but language support would be preferred.

Re:Scala's Option monad versus null-conditional operator in C# by daxfohl on Saturday February 20, 2016 @06:40PM (#11142519)
Just out of curiosity what kind of reusable monad libraries are out there for Scala/Haskell? Given the scope of everything between Maybe and IO, I've never understood what one could make that would be useful to "monads" in general (beyond the sugar for do-notation).
Re:Scala's Option monad versus null-conditional operator in C# by runT1ME on Saturday February 20, 2016 @06:46PM (#11142536)
https://github.com/scalaz/scalaz

Monad Transformers for one. Traverse/Sequence are another super common (amazing) pattern that requires abstracting over types.

http://eed3si9n.com/learning-scalaz-day12

is a nice tutorial series on it.

Re:Scala's Option monad versus null-conditional operator in C# by daxfohl on Saturday February 20, 2016 @08:34PM (#11142870)
Ah, light bulb moment (perhaps).

So in F# I've been using specialized asyncSeq and asyncMaybe libraries. With monad transformers (and thus HKT's), would those come largely for free?

And you could traverse a list<maybe<list<_>> combo like [Some [Some [1; 2]; None; Some [3]]; None; Some [None; Some [4]]] with no more difficulty than a regular list-of-lists?

Re:Scala's Option monad versus null-conditional operator in C# by runT1ME on Saturday February 20, 2016 @08:46PM (#11142902)
Exactly!

You can also write a generic "sequence" over a list, for instance, so that any 'monad' (task/future, maybe/option, state, either/disjunction etc.) can 'swap places'.

So your List<Future<A>> can become Future<List<A>>. But you only need to write it once. Then it doesn't matter what the inner type of the List is, as long as it's a monad (applicative, really, but all monads are applicatives).

Re:Scala's Option monad versus null-conditional operator in C# by daxfohl on Sunday February 21, 2016 @09:20AM (#11144561)
Crazy, I'd always considered monad transformers a sore thumb on, well, all that stuff. The fact that monads weren't enough but then you had to go reproduce all that stuff in monad transformers as well. The whole thing just reeked of code duplication and bad smells so I didn't really look into it further.

Now looking at the Scalaz impl, the transformers are actually the core thing, and monads themselves are just transformers over Identity. So it all fits and isn't redundant. Certainly changes a lot of perspective.

Re:Scala's Option monad versus null-conditional operator in C# by daxfohl on Thursday March 03, 2016 @01:41PM (#11218889)
That said, I'd still put LINQ as 99% of "the deal" per the original comment. The extra abstractions allowed by HKT's, while "necessary" mathematically, still only save me about 1% of the work required to implement asyncSeq and asyncMaybe, and in 15 years of pro experience those are the only real uses of HKT's I've ever had the need for.
Re:Scala's Option monad versus null-conditional operator in C# by louthy on Saturday February 20, 2016 @07:09PM (#11142616)
Monad transformers in C# [1] (but yes, only works with the monads in the library)

[1] https://github.com/louthy/language-ext/blob/master/LanguageE...

Re:Scala's Option monad versus null-conditional operator in C# by chongli on Saturday February 20, 2016 @11:44PM (#11143340)
For one example, in Haskell, all Monads are also Functors. This means you can `fmap` a function over a Maybe Foo just as you would over a [Foo] (read list of Foo) or Vector Foo or indeed an IO Foo.
Re:Scala's Option monad versus null-conditional operator in C# by tome on Sunday February 21, 2016 @06:59AM (#11144173)
lens, for example (which is not really a "reusable monad library" but very much of that flavour).
Re:Scala's Option monad versus null-conditional operator in C# by dk8996 on Saturday February 20, 2016 @06:48PM (#11142547)
Trivial to implement but not compatible if we both implemented. That's the main benefit of Scala Optional, its baked-in -- I don't have to worry about writing adapter to use your library.
Re:Scala's Option monad versus null-conditional operator in C# by louthy on Saturday February 20, 2016 @06:57PM (#11142582)
Agreed, there is no functional base with C# aside from LINQ. So if you want to do functional in C# you would have to chose a library, and that's why I went so far building the rest of the functional infrastructure around it. It'd be insane to use multiple Option implementations. My first attempt at bringing common monad support in C# [1] suffered from the problems you state.

But that still doesn't change the crux of the blog post, it's disingenuous comparing the Option monad to a null coalescing operator.

[1] https://github.com/louthy/csharp-monad

Re:Scala's Option monad versus null-conditional operator in C# by miloszpp on Monday February 22, 2016 @06:36AM (#11150104)
[Author] I wasn't trying to say that Option monad is better/worse than null conditional. My point was to compare two different approaches to the same problem.
Re:Scala's Option monad versus null-conditional operator in C# by wantreprenr007 on Saturday February 20, 2016 @09:59PM (#11143087)
This article is timely for us as we're using Scala for backend work.

Ruby 2.3 &. operator and Swift can do something like this pseudocode:

    if let city = article?.author?.address?.city?.ToLower() {
       // city present
    }
Too long?:

    if let city = article?
      .author?
      .address?
      .city?
      .ToLower() {
        // city present
    }
All are improvements over pyramids of doom.

(Be aware of subtle gotcha in Scala of var and val, really prefer Rust/Go/Swift keywords.)

Re:Scala's Option monad versus null-conditional operator in C# by jb55 on Saturday February 20, 2016 @09:31PM (#11143015)
I have a standalone implementation here as well: https://github.com/jb55/Data.Maybe.cs
Re:Scala's Option monad versus null-conditional operator in C# by hibikir on Saturday February 20, 2016 @05:56PM (#11142369)
As usual when someone tries to get into explaining monads, mistakes happen.

First, on the scala standard library and monads: Scala very carefully avoided creating a Monad trait, on purpose. So while List and Options are monads, we can't just go and say that everything that has a flatmap is a monad.

Also, the reason we can combine options and lists in a for comprehension is not really that they both have a flatmap: Try doing that with Future instead, and it will not work quite so easily! The reason those two have such easy interoperability is that in Scala's Predef there is a conversion that turns an Option into a Traversable, which is being called under the covers. For comprehensions are very powerful, but switching from one monadic context to another without help isn't one of them. In the case of Future and List, for instance, we'd have to use some of the utility methods in the Future object that can help transform a future into a List[Future], and a List[Future] into Future[List]

Don't get me wrong, it's great to see people trying to explain this kind of stuff, but it is so very hard to cover the topic in a way that is both approachable and that doesn't leave people with intuitions that will fail them outside of the given example.

Re:Scala's Option monad versus null-conditional operator in C# by miloszpp on Monday February 22, 2016 @06:43AM (#11150137)
[Author] Thanks for your remarks.

I agree on the first point. As to the second, I didn't mean that having flatmap is the reason why we can combine Options and Lists. I meant that Options are composable with other Options and Lists are composable with other Lists.

I understand the problem of working with different monads. I looked into monad transformers at some point and found them difficult to work with so my approach now is to avoid mixing monad types (for example instead of having Future[Option[T]], just use a failed Future instead of None).

Re:Scala's Option monad versus null-conditional operator in C# by VoiceOfWisdom on Saturday February 20, 2016 @04:25PM (#11142027)
The same can be accomplished in F# for those who want this power in the .net ecosystem

    type Address = { street : string; city : string option }
    type Author = { name : string; email : string; address : Address option }
    type Article = { title : string; content : string; author : Author option }

    let printCity (article : Article) =
        article.author
        |> Option.bind(fun author -> author.address)
        |> Option.bind(fun address -> address.city)
        |> Option.iter(fun city -> printfn "%s" city)
Re:Scala's Option monad versus null-conditional operator in C# by daxfohl on Saturday February 20, 2016 @10:01PM (#11143096)
Better to do with the maybe workflow from Fsharpx:

    maybe {
      let! address = author.address
      let! city = address.city
      printfn %s city
    }
Re:Scala's Option monad versus null-conditional operator in C# by krcz on Saturday February 20, 2016 @05:25PM (#11142261)
Scala version can be written as one-liner as well; losing readability, but it might be ok for fast hacking / prototyping:

article.author.flatMap(_.address).flatMap(_.city).map(_.toLowerCase).foreach(println)

And in for-comprehension version there is no need to use yield, as we don't need result of println.

Re:Scala's Option monad versus null-conditional operator in C# by dk8996 on Saturday February 20, 2016 @06:35PM (#11142494)
We work with Scala at our company and this is the style we use. I think that it's something that even a new person to Scala can easy understand.
Re:Scala's Option monad versus null-conditional operator in C# by Filligree on Saturday February 20, 2016 @07:51PM (#11142759)
Hell, I've never used Scala at all, and I can read that.
Re:Scala's Option monad versus null-conditional operator in C# by closetnerd on Sunday February 21, 2016 @12:37PM (#11145440)
You can tell that "_.author" is a parameterless lambda?
Re:Scala's Option monad versus null-conditional operator in C# by Filligree on Sunday February 21, 2016 @02:19PM (#11146006)
Um, no? It takes one parameter.

Wouldn't fit into the map type signature otherwise.

Re:Scala's Option monad versus null-conditional operator in C# by psuter on Saturday February 20, 2016 @08:28PM (#11142856)
After 10 years of programming in Scala, I must admit I still don't know which style I prefer.
Re:Scala's Option monad versus null-conditional operator in C# by jameslk on Saturday February 20, 2016 @04:51PM (#11142111)
Why not just use less dots to begin with? Or, in other words, apply the Law of Demeter[0]. To me, diving so many levels deep into dependencies is a code smell because it tightly couples code to sub-dependencies and makes refactoring more difficult.

0. https://en.wikipedia.org/wiki/Law_of_Demeter

Re:Scala's Option monad versus null-conditional operator in C# by daxfohl on Saturday February 20, 2016 @06:32PM (#11142487)
This is important for typical OO chaining

i.e. you should avoid:

serviceX.getServiceY().getServiceZ().getSomeValue();

But for just structured data it's not constructive to write accessor wrappers at each level. And for things like recursive data structures, it's not even possible.

Re:Scala's Option monad versus null-conditional operator in C# by oofabz on Saturday February 20, 2016 @09:56PM (#11143082)
More about the Law of Demeter at Ward's Wiki:

http://c2.com/cgi/wiki?LawOfDemeter

Re:Scala's Option monad versus null-conditional operator in C# by lmm on Sunday February 21, 2016 @03:44AM (#11143823)
Monads are very useful for what OO people sometimes call "cross-cutting concerns" - something that's relevant at many levels but not the primary purpose of any one function. E.g. error handling, async, audit logs, database transaction boundaries.
Re:Scala's Option monad versus null-conditional operator in C# by paulddraper on Sunday February 21, 2016 @03:21AM (#11143782)
The differences go far beyond syntax.

Option/Maybe can represent arbitrary optionality, while null is limited to a sentinel value.

I wrote a blog post about null (https://www.lucidchart.com/techblog/2015/08/31/the-worst-mis...) and this one of the examples:

Suppose you have a generic key-value store. If a value is not present, the lookup returns null. You use it for a cache that maps user IDs to phone numbers. Some people don't have phone numbers, i.e. their phone number is null. But now you can't represent that in your cache. (JS "fixes" this -- if you want to call it that -- by using a second type of null: undefined.)

Option is generic and can applied arbitrarily deep; null/Nullable is a sentinel that can be used once.

Option is arbitrarily composable, and the superior choice.

Re:Scala's Option monad versus null-conditional operator in C# by CuriousSkeptic on Saturday February 20, 2016 @04:30PM (#11142054)
The null-conditional operator is a language feature. The Option monad is a class in the standard library.

You can have Option in C# and use it with linq to get more or less the same feature and syntax. There are a few implementations on nuget if you don't want to write your own.

Re:Scala's Option monad versus null-conditional operator in C# by qwtel on Saturday February 20, 2016 @08:19PM (#11142838)
In case anybody is interested, this is how you would do this in clojure:

    (println (some-> article :author :address :city upper-case))
Re:Scala's Option monad versus null-conditional operator in C# by sandisk5 on Monday February 22, 2016 @12:32PM (#11152562)
(some-> article :author :address :city upper-case println)

So that you don't print nil if some-> returns nil.

Re:Scala's Option monad versus null-conditional operator in C# by finishingmove on Sunday February 21, 2016 @04:36AM (#11143909)
Thanks, I'm learning Clojure and things like this make me all warm and fuzzy inside :)
Re:Scala's Option monad versus null-conditional operator in C# by ubercow on Sunday February 21, 2016 @04:36AM (#11143910)
I didn't know about some->, that would have been handy recently.

Cheers.

Re:Scala's Option monad versus null-conditional operator in C# by gmantom on Saturday February 20, 2016 @05:37PM (#11142301)
Why not just use F#, it's better than Scala and C# at these sorts of things and functional first and immutable first.
Re:Scala's Option monad versus null-conditional operator in C# by runT1ME on Saturday February 20, 2016 @05:39PM (#11142310)
It's really not. I worked for a .NET shop that migrated some new projects to Scala. We evaluated F#, but decided the power of Scala was worth the platform switch. F# lacks higher kinds and implicit parameters, meaning you can't really use the type class pattern. This hurt our productivity.
Re:Scala's Option monad versus null-conditional operator in C# by louthy on Saturday February 20, 2016 @05:49PM (#11142349)
> lacks higher kinds

It can be done in F# [1], but it's not pretty, and absolutely nails the compiler. The fact that the compiler can be cheated into doing it makes it even more frustrating that there isn't some official support.

[1] https://github.com/mausch/FsControl

Re:Scala's Option monad versus null-conditional operator in C# by azth on Sunday February 21, 2016 @01:37AM (#11143585)
> F# lacks higher kinds and implicit parameters, meaning you can't really use the type class pattern. This hurt our productivity.

Genuinely curious, but what are you doing that the lack of type classes hurts productivity so much?

Re:Scala's Option monad versus null-conditional operator in C# by runT1ME on Sunday February 21, 2016 @02:29AM (#11143677)
It's just annoying to have to re-implement sequence/traverse and every combination of monad stacks you want to for each type. It's doable, but bug prone and frustrating.

I now work for one of the largest scala teams in the US. We make heavy use of the Scalaz project and regularly use monad transormers or free monads and both require higher kinded types.

Re:Scala's Option monad versus null-conditional operator in C# by draw_down on Saturday February 20, 2016 @06:54PM (#11142575)
I think this is the first time I've ever really understood what the hell flatMap is for. So, thanks!
Re:Scala's Option monad versus null-conditional operator in C# by miloszpp on Monday February 22, 2016 @06:44AM (#11150148)
[Author] That's awesome to hear that, thanks :)
Re:Scala's Option monad versus null-conditional operator in C# by merb on Sunday February 21, 2016 @06:25AM (#11144101)
Actually in scala for just getting the city and lowercase it I would write that: article.author.flatMap(_.address.flatMap(_.city.map(_.toLowerCase)))

I know that's by far not as good looking as the c# version, however at the end all comes down to syntactic sugar.

Re:Scala's Option monad versus null-conditional operator in C# by joaoqalves on Saturday February 20, 2016 @04:20PM (#11142013)
tl; dr: Scala Option monad is more elegant :)
Re:Scala's Option monad versus null-conditional operator in C# by jeroen on Saturday February 20, 2016 @06:41PM (#11142520)

  Console.WriteLine(article?.Author?.Address?.City?.ToUpper());
That is actually a bit different than the first piece of code, since it calls Console.WriteLine.
Re:Scala's Option monad versus null-conditional operator in C# by wantreprenr007 on Saturday February 20, 2016 @09:53PM (#11143072)
Much better than the usual terrible patterns of pyramids of doom.

Another reason to set tab stops to 4+ characters: make PoDs impractical and better code legible.