3 ms·
> Clojure has a function that does basically the same thing. The get function will return nil if a key in a dictionary isn’t present: Isn't that already what h
by masklinn 8y ago
> Clojure has a function that does basically the same thing. The get function will return nil if a key in a dictionary isn’t present:
Isn't that already what happens when you use the map as a function directly? Even the sub-feature of being able to provide a default value if the key is not found is supported.
- hellofunk 8y agoBut what if the map itself is nil? That’s why using maps as functions is generally not done in the wild.
- masklinn 8y agoThat's nil punning, which the article specifically does not care for if not outright decries in its conclusion: > Over at Lispcast, Eric Normand argues for the “nil-punning” approach, which is fine. But I think this approach requires a confused notion of what nil/Nothing actually means. […] It is much simpler to understand nil as Nothing, i.e. the absence of a value (which is a type). Assuming that, the behaviour of `get` is specifically one you do not want. Furthermore, calling the map actually behaves as "Haskell and other ML-ish languages". `Data.Map.lookup` takes a Map, not a Maybe Map.
- hellofunk 8y ago‘get’ is a function that will always exist, but using a map in function position is very fragile and everything breaks if the map itself is nil. If you advocating for this, it is contrary to general consensus in the community; you will almost never see this in real code. Responding to your edit: it’s safe to do it in Haskell because you know it isn’t nil at compile time.
- masklinn 8y ago> ‘get’ is a function that will always exist, but using a map in function position is very fragile and everything breaks if the map itself is nil. It's no more fragile than any other function which doesn't do nil punning, and again the essay we're supposedly discussing advocates not using nil punning.
- hellofunk 8y agoBut you are advocating to use the map in function position? That’s much worse than get, most of the time.
- masklinn 8y agoAll I'm pointing out is that for the purpose of the article there seems to be no difference between using the map as a function and using get, and in fact that using the map directly is more suitable to the essay's espoused philosophy of avoiding nil punning and treating nil as a hole/error rather than a normal value.
- hellofunk 8y agoThe problem is that ‘get’ possibly creates one nil result. Using the map as a function, even if nil is understood to be an error, now gives you two possible and very different errors from one expression. It’s a bad idea and generates added ambiguity and complexity, which further strays from the article’s philosophy.
- masklinn 8y agoThere is no ambiguity to using map as a function. It raises an error if the map is nil, and returns a nil if the key is not found. Using get is ambiguous, getting a nil result may mean that the key was not found or that there was no map in the first place.
- fiddlerwoaroof 8y agoYeah, Common Lisp’s gethash is better than get in most other languages because of Common Lisp’s support for multiple return values. (gethash key hash-table) Returns two values: the first one is the value found in the hash table, or nil if no value was found. The second one is nil (i.e. false) if no value was found and non-nil (maybe t? I forget what the standard says) if a value was found.
- deleted 8y ago[deleted]