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) let value = defaultArg (Map.tryFind "foo" dict) "default"
or match Map.tryFind "foo" dict with
| Some v -> v
| None -> "default"I sure as hell hope so, but that's what fleitz used.
value = dict.get('foo', 'default')
in IronPython.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.
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]
[0]: http://docs.python.org/library/collections.html#collections.... [1]: http://docs.python.org/library/stdtypes.html#dict
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.
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.
Regarding Python speed, I know it's pretty slow, especially the IronPython implementation.
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!
No, but that's not what the example he replied to does.
(setf value (gethash "foo" dict "default"))
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
Than 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.