News for hackers. Stuff that matters.
Re:Why did Microsoft invest so much in F#? by fleitz on Monday November 08, 2010 @03:45PM (#1883925)
F# is the place where you can unleash the power of your core developers and make objects fully usable by an army of C# developers.

When I used it no one wanted to learn F#, but if I put a nice OO interface on top of whatever I wrote, packaged it as a DLL the other devs would be happy to use it.

F# isn't just cool, it will make you write better code, faster. The zen of it makes you follow a better path. Think about how many bugs go away when you make all sides of an 'if' 'else' block return the same data type.

Think about how much more sense returning an out parameter as a tuple makes. I'd much rather write

  let value = match dict.TryGetValue("foo") with
              | (true,val) -> val
              | (false,_) -> "default"
Than

  string value;
  if(!dict.TryGetValue("foo", out value)){
      value = "default";
  }
Also, if you're using a match statement and forget to handle the false case, the compiler will scream at you. In C# you're on your own. Also, in C# in this case you can't even use var because you're not initializing it.

The other thing about F# is that it's FAST, fast like beating C at the burrows-wheeler transform.

Re:Why did Microsoft invest so much in F#? by masklinn on Monday November 08, 2010 @05:42PM (#1884358)
> let value = match dict.TryGetValue("foo") with > | (true,val) -> val > | (false,_) -> "default"

groan

    let value = findWithDefault "default" "foo" dict
or

    case M.lookup key dict of
         Just v -> v
         Nothing -> "default"
(and for anybody who wants to whine about parameters order, please remember — or discover — that partial application is extremely frequent in Haskell and MLs, and try to read the parameters order in this light, you will be enlightened)
Re:Why did Microsoft invest so much in F#? by timrobinson on Monday November 08, 2010 @06:24PM (#1884494)
dict.TryGetValue is an odd example here -- idiomatic F# for a map lookup would be:

    let value = defaultArg (Map.tryFind "foo" dict) "default"
or

    match Map.tryFind "foo" dict with
    | Some v -> v
    | None -> "default"
Re:Why did Microsoft invest so much in F#? by masklinn on Tuesday November 09, 2010 @01:56AM (#1885648)
> dict.TryGetValue is an odd example here

I sure as hell hope so, but that's what fleitz used.

Re:Why did Microsoft invest so much in F#? by Gonsalu on Monday November 08, 2010 @04:48PM (#1884149)
FAST? It's still .NET. Regarding you example, I'd much rather write

  value = dict.get('foo', 'default')
in IronPython.
Re:Why did Microsoft invest so much in F#? by MichaelGG on Monday November 08, 2010 @05:04PM (#1884218)
I'm not familiar with python - does it have some magic global sentinel value to indicate "not found"? Or how do you handle that case with that 1 line of code?

Secondly, saying "it's still .NET" is similar to saying "it's still x64". The F# compiler does a lot of transforms on code that the C# compiler does not. In some cases, this can result in better IL and better JIT'd code. Also, having inlining support at the compiler level can be very handy sometimes.

Re:Why did Microsoft invest so much in F#? by masklinn on Monday November 08, 2010 @05:36PM (#1884337)
> I'm not familiar with python - does it have some magic global sentinel value to indicate "not found"?

No, it has a different method for failable search. The purpose of `get` is specifically to handle "provide a default value in case the key is not found".

> Or how do you handle that case with that 1 line of code?

dict[key]

Re:Why did Microsoft invest so much in F#? by xiongchiamiov on Monday November 08, 2010 @06:43PM (#1884551)
You can also use defaultdict[0], or define the __missing__() method[1] yourself on a dictionary you receive from someone else's code. In either of these cases the "not found" value will be whatever you decide it should be.

[0]: http://docs.python.org/library/collections.html#collections.... [1]: http://docs.python.org/library/stdtypes.html#dict

Re:Why did Microsoft invest so much in F#? by fleitz on Monday November 08, 2010 @05:33PM (#1884325)
Yes, good points MichaelGG, one of the really nice things about the F# compiler is that it can inline higher order functions, so if you pass a function to another it may create a custom piece of code with your passed function inlined. I wasn't sure if you were referring to C style inline support, which C# does have (in the .NET JIT). Like C the inlining in F# is just a suggestion to the compiler.

So you can write code that is optimal from a design point of view (abstractions, etc) and the compiler will turn it into optimal code from a performance point of view.

See http://flyingfrogblog.blogspot.com/2009/07/ocaml-vs-f-burrow... for a much better explanation.

Re:Why did Microsoft invest so much in F#? by MichaelGG on Monday November 08, 2010 @06:20PM (#1884481)
I don't think that F#'s inline is just a suggestion. Hat types (^a) aka statically resolved type variables, require inline methods to actually be inline. Same way you can't have inlined recursive functions.
Re:Why did Microsoft invest so much in F#? by jdh30 on Saturday November 13, 2010 @07:06AM (#1900741)
You can nest a recursive function inside a non-recursive one and inline that.
Re:Why did Microsoft invest so much in F#? by fleitz on Monday November 08, 2010 @05:21PM (#1884275)

  namespace Org.DictionaryExtensions =
    type Dictionary<'T> with
     member x.get(key,defaultValue) = 
       match x.TryGetValue(key) with
       | (true,val) -> val
       | (false,_) -> defaultValue
There fixed that for you, now you can do, dict.get("foo","defaultvalue") in whatever CLR language you like. :)

As for fast and knocking .NET you're not seriously suggesting python are you? Runtime method lookup is not exactly what I think when thinking fast.

Re:Why did Microsoft invest so much in F#? by Gonsalu on Tuesday November 09, 2010 @10:00AM (#1886601)
I wasn't knocking .NET; I had the idea that coding something in C# or F# would result in the same IL output, which, after reading MichaelGG's reply, I realized was completely wrong...

Regarding Python speed, I know it's pretty slow, especially the IronPython implementation.

Re:Why did Microsoft invest so much in F#? by rlmw on Monday November 08, 2010 @05:05PM (#1884222)
Upvoted because that's good code reuse in the scenario that you only want to store a default value, and I can see why you replied to the commenter with it. It doesn't necessarily work so well in the scenario where you want to perform some operation if there isn't a value. Or if default needs to be computed. I'm assuming that the idiomatic way of doing this in python would be:

try: value = dict['foo'] except KeyNotFound: value = do_something_interesting() dict['foo'] = value

If you could pass a function in as a default of course this point is moot!

Re:Why did Microsoft invest so much in F#? by popleft on Monday November 08, 2010 @05:18PM (#1884268)
See collections.defaultdict in the standard library -- http://docs.python.org/library/collections.html#collections....
Re:Why did Microsoft invest so much in F#? by masklinn on Monday November 08, 2010 @05:43PM (#1884362)
> It doesn't necessarily work so well in the scenario where you want to perform some operation if there isn't a value.

No, but that's not what the example he replied to does.

Re:Why did Microsoft invest so much in F#? by mahmud on Monday November 08, 2010 @06:38PM (#1884539)
CL

  (setf value (gethash "foo" dict "default"))
Re:Why did Microsoft invest so much in F#? by kenjackson on Monday November 08, 2010 @05:30PM (#1884309)
Hmmm... I'm slowly being convinced.