Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to get a default value from an assoc in Factor programming language
+1
−0
To get a value from an assoc and provide a default value if the value does not exist, I am using the following definition:
:: at-def ( key assoc default -- value ) key assoc ?at [ drop default ] when ;
Is there a more specialized word to do this? (or a different class to use?)
1 answer
+2
−0
: at-default ( default key assoc -- value/default )
at* [ nip ] [ drop ] if ; inline
100 1 H{ { 1 2 } } at-default .
2
100 10 H{ { 1 2 } } at-default .
100
You can also just do this:
H{ { 1 2 } } 1 of [ 100 ] unless* .
2
H{ { 1 2 } } 10 of [ 100 ] unless* .
100
H{ { 1 2 } } 1 of 100 or .
2
H{ { 1 2 } } 10 of 100 or .
100
0 comment threads