Live data from Hacker News

.NET Fiddle adds F#

dotnetfiddle.net

41–50 of 72 posts

Re: .NET Fiddle adds F#

#41

Cool to "see" F# in action. Like the union types, but not so much the list operations; seems more natural to: [1;2;3;4] filter isEven sum vs. List.filter isEven [1;2;3;4] |> List.sum in Scala it's: List(1,2,3,4) filter isEven sum Of course I'm not familiar with F# so don't know all of the WIN within (Type Providers, for example, are very impressive, would love to see that on the Scala side of the fence one day).

I've got little experience with Scala, but judging from your sample it looks like what's happening is that Scala follows more Java-y idioms for how code is structured. So in List(1,2,3,4) filter isEven sum it looks like filter and sum are instance methods on the List class, and I'm guessing isEven is a predicate that's being passed as an argument to the filter method. F# leans closer to its functional roots in this r…

> So the List class mostly sticks to static methods in its public interface because that approach fits better with traditional functional idioms.

And also fits perfectly with the C# and VB.NET extension method syntactic sugar, which was used extensively for LINQ. They're really just static methods in the .NET VM and bytecode.

Re: .NET Fiddle adds F#

#42
post #3

Can anyone comment how F# compares to other functional languages? Just curious.

F# is very un-functional in many ways. Immutable variables are the default but mutable variables are made with just the "mutable" keyword. There are also lots of imperative control structures, like while, and for. Those two combined means that imperative algorithims typically have two distinct translations into F#. First you can translate it literally, where F# looks like a slightly more verbose version of Python. Se…

I'm not sure I'd say un-functional, just multiparadigm. But having at least some support for imperative programming is the norm in functional languages - ML does it, most LISP dialects do it, etc. Even Scheme, originally notable for its lack of loops, still has set! and idioms for combining it with closures and tail recursion to produce constructs that behave like loops.

And impure, of course. It's definitely an impure language. Personally, though, I'm not at all fond of conflating the terms "pure" and "functional". It's a terrible retcon that has more to do with the Haskell proselytism than with the development and history of functional programming or how its community has self-identified over the years. Note that the seminal book's title is "Purely Functional Data Structures", not something that's only three words long.

Re: .NET Fiddle adds F#

#43

Earlier quoted context omitted.

I've got little experience with Scala, but judging from your sample it looks like what's happening is that Scala follows more Java-y idioms for how code is structured. So in List(1,2,3,4) filter isEven sum it looks like filter and sum are instance methods on the List class, and I'm guessing isEven is a predicate that's being passed as an argument to the filter method. F# leans closer to its functional roots in this r…

> So the List class mostly sticks to static methods in its public interface because that approach fits better with traditional functional idioms. And also fits perfectly with the C# and VB.NET extension method syntactic sugar, which was used extensively for LINQ. They're really just static methods in the .NET VM and bytecode.

> that approach fits better with traditional functional idioms

Just interested, what's the real value here? "Familiarity" alone seems to be a pretty poor reason.

> They're really just static methods in the .NET VM and bytecode.

Doesn't this mean that either your methods won't be dynamically dispatched or that you would need to rewrite your code if you ever change from e. g. List to Array?

Looks like a bad compromise to me.

Re: .NET Fiddle adds F#

#44

Cool to "see" F# in action. Like the union types, but not so much the list operations; seems more natural to: [1;2;3;4] filter isEven sum vs. List.filter isEven [1;2;3;4] |> List.sum in Scala it's: List(1,2,3,4) filter isEven sum Of course I'm not familiar with F# so don't know all of the WIN within (Type Providers, for example, are very impressive, would love to see that on the Scala side of the fence one day).

I've got little experience with Scala, but judging from your sample it looks like what's happening is that Scala follows more Java-y idioms for how code is structured. So in List(1,2,3,4) filter isEven sum it looks like filter and sum are instance methods on the List class, and I'm guessing isEven is a predicate that's being passed as an argument to the filter method. F# leans closer to its functional roots in this r…

> F# leans closer to its functional roots in this respect, so it's more idiomatic to keep object-oriented constructs at arm's reach in most your code. The language has full support for OOP, it's just that you're not expected to trot it out except when you're writing public interfaces that are meant to be consumed by code that might be written in C# or VB.NET.

Wouldn't it have been better to keep such things out of the language instead of having a considerable amount of complexity in the language for stuff people are not even supposed to be doing in the first place?

Re: .NET Fiddle adds F#

#45

Earlier quoted context omitted.

I've got little experience with Scala, but judging from your sample it looks like what's happening is that Scala follows more Java-y idioms for how code is structured. So in List(1,2,3,4) filter isEven sum it looks like filter and sum are instance methods on the List class, and I'm guessing isEven is a predicate that's being passed as an argument to the filter method. F# leans closer to its functional roots in this r…

> F# leans closer to its functional roots in this respect, so it's more idiomatic to keep object-oriented constructs at arm's reach in most your code. The language has full support for OOP, it's just that you're not expected to trot it out except when you're writing public interfaces that are meant to be consumed by code that might be written in C# or VB.NET. Wouldn't it have been better to keep such things out of th…

No, The .NET Base Class Library is object-oriented at the core, and one of the value propositions of F# is that you can fully leverage that library.

Re: .NET Fiddle adds F#

#46

Earlier quoted context omitted.

> F# leans closer to its functional roots in this respect, so it's more idiomatic to keep object-oriented constructs at arm's reach in most your code. The language has full support for OOP, it's just that you're not expected to trot it out except when you're writing public interfaces that are meant to be consumed by code that might be written in C# or VB.NET. Wouldn't it have been better to keep such things out of th…

No, The .NET Base Class Library is object-oriented at the core, and one of the value propositions of F# is that you can fully leverage that library.

Isn't there a large difference between supporting interop with code written in other languages and importing all the stuff into the language itself?

Re: .NET Fiddle adds F#

#47

Earlier quoted context omitted.

No, The .NET Base Class Library is object-oriented at the core, and one of the value propositions of F# is that you can fully leverage that library.

Isn't there a large difference between supporting interop with code written in other languages and importing all the stuff into the language itself?

Well, if the language was only meant to be an API consumer then eliding OO stuff would make more sense, but they designed it for bi-directional interop. And besides, F# was based on OCaml (as I'm sure you know), which itself was an existence proof that you could successfully bolt OO onto a functional language.

Re: .NET Fiddle adds F#

#48
post #30

Earlier quoted context omitted.

I've done very little but it seems really concise. I like that. One place some predict it will emerge is in the Web API arena, and I could totally see that happening.

Why? What makes it better than C# in web Web API area? (honest question)

In my own tests, F# will require about 1/20th of the type annotations of C#. It offers a near superset of functionality, so you don't lose anything (except a bit on the tooling side of things).

The lightweight syntax, easy handling of functional features, nesting of code - it ends up leaving you with far less code that has less bugs by default. MS describes F# as excelling at "programming in the small" - the line-by-line, char-by-char code is simply far superior to using C#. Just type inference alone is huge - C# is embarrassingly bad at type inference, and it's partially due to the complexity in their codebase in implementing it properly. At the moment, MS seems to have sort of backed off on enhancing C#.

There's so many tiny things in F# that just make the code easier and less bug prone. For instance, being able to nest code. Suppose I want to read an integer out of a querystring, and on failure provide a default. Idiomatically, in C#:

  int age;
  if (!int.TryParse(qs["age"], out age)) age = -1;
In F#:

  let age = try int qs.["age"] with _ -> -1
Or lets say I have some intermediate variables. In C#:

  int age;
  var parts = String.Split(bla, ",");
  if (parts.Length == 0) age = -1;
  else age = int.Parse(parts[0]);
I've introduced a useless local var, parts, for no reason other than needing it in computing age. In F# (and avoiding pattern matching just to demonstrate):

  let age = 
    let parts = String.Split(bla, ",")
    if parts.Length = 0 then -1 else int parts.[0]
The intermediate binding, is not available outside it's scope. That's far cleaner, and quite commonly useful.

Top level functions are handy. Being able to make useful lambdas without specifying the types is super useful. Many of my functions have a nested helper function which makes things cleaner. Places where I don't want to add a full utility function, but I don't want to repeat code. You can do it in C#, but it's ugly. In F# it's natural.

Array/list comprehensions. In C#:

  var res = new List();
  using(var reader = cmd.ExecuteReader()) {
    while(reader.Read()){
      res.Add(reader.GetInt32(0));
    }
  }
In F#:

  use reader = cmd.ExecuteReader()
  let res = [ while reader.Read() do yield reader.GetInt32(0) ]
Pattern matching is well covered, and F# Active Patterns are just awesome, because you can customize your own destructuring for arbitrary objects. The list just goes on and on, lots of things in F# that are just so right, so easy, so trivial. Custom operators, tuple handling.

I'm not even touching on the larger things, like Type Providers, records, sum types, or the fact that F# has monad syntax that's flexible, instead of C# building specific monads into the language. Heck, the F# compiler is far more advanced and produces faster code in many cases. And the fact that you can manually make it inline code is also a nice perf win you simply cannot get with C#.

I recently started working on a project that uses C#, and it's just such a pain. Death by a thousand cuts. There's no reason to use C# over F#. Even if you only use F# as a light-syntax-C#, you're better off.

The only real objection is hiring, in an enterprise scenario. For any serious project where the code is the product (versus the code being an artifact of just solving a business problem), I don't believe you can hire any good programmer that cannot deal with F#. (I think that hold for most languages, in general.) Unfortunately, people don't seem to believe that, and somehow think they're going to hire really smart people, but these people aren't going to be able to learn a different language.

Re: .NET Fiddle adds F#

#49

Earlier quoted context omitted.

> So the List class mostly sticks to static methods in its public interface because that approach fits better with traditional functional idioms. And also fits perfectly with the C# and VB.NET extension method syntactic sugar, which was used extensively for LINQ. They're really just static methods in the .NET VM and bytecode.

> that approach fits better with traditional functional idioms Just interested, what's the real value here? "Familiarity" alone seems to be a pretty poor reason. > They're really just static methods in the .NET VM and bytecode. Doesn't this mean that either your methods won't be dynamically dispatched or that you would need to rewrite your code if you ever change from e. g. List to Array? Looks like a bad compromise…

One of the big values is currying. Instance methods don't always interact well with partial application and design patterns that rely on it, because with instance methods one of the function's (logical) operands is a special snowflake that isn't curried.

Re: .NET Fiddle adds F#

#50

Earlier quoted context omitted.

No, The .NET Base Class Library is object-oriented at the core, and one of the value propositions of F# is that you can fully leverage that library.

Isn't there a large difference between supporting interop with code written in other languages and importing all the stuff into the language itself?

There's an enormous difference. And it's a good thing F#'s designers chose the latter option; it's a huge win on the interoperability front. Libraries have a tendency to be write-once-use-often, so exporting object-oriented APIs from F# code is way better than consuming functional APIs from C# code.
Post reply on HN