3 ms·
Reference manual for Maps: http://www.erlang.org/doc/reference_manual/maps.html http://www.erlang.org/doc/reference_manual/maps.html Manpage for Maps: http://w
by talklittle 13y ago
Reference manual for Maps: http://www.erlang.org/doc/reference_manual/maps.html http://www.erlang.org/doc/reference_manual/maps.html
Manpage for Maps: http://www.erlang.org/doc/man/maps.html http://www.erlang.org/doc/man/maps.html
Older documentation for Maps (EEP): http://www.erlang.org/eeps/eep-0043.html http://www.erlang.org/eeps/eep-0043.html
Important note found on the reference manual page:
Maps are considered experimental during OTP 17 and may be subject to change.
The documentation below describes it being possible to use arbitrary expressions or variables as keys, this is NOT implemented in the current version of Erlang/OTP.
Exceptions returns badarg instead of badmap, this will change in the future releases.
- bosky101 13y agoI especially like this property of maps that cannot be done in either records or JSON ( had it been supported as a primitive) M4 = #{{"w", 1} => f()}. % compound key associated with an evaluated expression And of course self-referencing is a welcome addition (especially on the terminal) Fac = fun(0) -> 1; (N) -> N*Fac(N-1) end ~B
- mononcqc 13y agoThe syntax would actually be: fun Fac(0) -> 1; Fac(N) -> N*Fac(N-1) end Note that the name `Fac` is not visible outside of the fun's scope, and to use the same variable name outside, you still need to bind it: F = fun Fac(0) -> 1; Fac(N) -> N*Fac(N-1) end
- eurleif 13y agoLooks like this is pretty much identical to JavaScript?
- mononcqc 13y agoPretty much. Javascript has the same scoping rules for functions used as expressions: var f = function fac(n) { if (n==0) { return 1; } else {return fac(n-1)*n; } }; console.log(f(3)); console.log(fac(3)); This will log '6' and error out on the last one (fac is undefined). This is the same scoping Erlang uses for anonymous function names as far as I can tell, with one difference: Erlang's function name is a variable that can be used as such internally. In Javascript, the function name doesn't refer to a variable per se: var f = function fac() { console.log(fac) }; console.log(f); f(); Which will log 'fac()' and 'undefined'. In Erlang: 1> F = fun Fac() -> Fac end. #Fun<erl_eval.44.71889879> 2> F(). #Fun<erl_eval.44.71889879> Both instances (F and Fac within the right scope) return the same anonymous function.
- eurleif 13y agoAre you sure you didn't make a mistake, and confuse the undefined that f() returned with an undefined output from the console.log(fac)? It looks to me like the function is a perfectly normal variable. var f = function foo() { console.log('Me:', foo); } f(); Me: function foo() { console.log('Me:', foo); } undefined
- mononcqc 13y agoYou're right, I've been fooled by Firebug merging in multiple identical output lines together and missed the (2) it had put on the right hand side of the screen. That makes a lot more sense.
- deleted 13y ago[deleted]