digitalmars.D.learn - Max/Min values in an associative array
- TJB (11/11) Aug 06 2014 I am trying to find the max and min values in an associative
- Justin Whear (4/17) Aug 06 2014 Do you just need the min and max values or do you also need the keys of
- TJB (3/7) Aug 06 2014 Justin,
- Martijn Pot (3/14) Aug 06 2014 You can extract the values into a double[] using bid.values. Then
- H. S. Teoh via Digitalmars-d-learn (8/18) Aug 06 2014 [...]
- monarch_dodra (3/9) Aug 15 2014 Take a look at Justin Whear's dpaste. Dual pred reduce FTW.
- H. S. Teoh via Digitalmars-d-learn (5/17) Aug 15 2014 Yeah I saw that. Learned something new. :-)
- bearophile (10/19) Aug 14 2014 void main() {
I am trying to find the max and min values in an associative array. Say I have: double[char] bids; bid['A'] = 37.50; bid['B'] = 38.11; bid['C'] = 36.12; How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to. Thanks! TJB
Aug 06 2014
On Wed, 06 Aug 2014 17:57:54 +0000, TJB wrote:I am trying to find the max and min values in an associative array. Say I have: double[char] bids; bid['A'] = 37.50; bid['B'] = 38.11; bid['C'] = 36.12; How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to. Thanks! TJBDo you just need the min and max values or do you also need the keys of those values? If the former, here's a paste: http://dpaste.dzfl.pl/0bbf31278a25
Aug 06 2014
Justin, That's it! Perfect - thanks!! TJBDo you just need the min and max values or do you also need the keys of those values? If the former, here's a paste: http://dpaste.dzfl.pl/0bbf31278a25
Aug 06 2014
On Wednesday, 6 August 2014 at 17:57:55 UTC, TJB wrote:I am trying to find the max and min values in an associative array. Say I have: double[char] bids; bid['A'] = 37.50; bid['B'] = 38.11; bid['C'] = 36.12; How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to. Thanks! TJBYou can extract the values into a double[] using bid.values. Then you can simply use max and min from std.algorithm.
Aug 06 2014
On Wed, Aug 06, 2014 at 05:57:54PM +0000, TJB via Digitalmars-d-learn wrote:I am trying to find the max and min values in an associative array. Say I have: double[char] bids; bid['A'] = 37.50; bid['B'] = 38.11; bid['C'] = 36.12; How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to.[...] import std.algorithm : reduce, max, min; auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue()); auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue()); T -- Designer clothes: how to cover less by paying more.
Aug 06 2014
On Wednesday, 6 August 2014 at 18:07:08 UTC, H. S. Teoh via Digitalmars-d-learn wrote:import std.algorithm : reduce, max, min; auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue()); auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue()); TTake a look at Justin Whear's dpaste. Dual pred reduce FTW.
Aug 15 2014
On Fri, Aug 15, 2014 at 04:51:59PM +0000, monarch_dodra via Digitalmars-d-learn wrote:On Wednesday, 6 August 2014 at 18:07:08 UTC, H. S. Teoh via Digitalmars-d-learn wrote:Yeah I saw that. Learned something new. :-) T -- Bare foot: (n.) A device for locating thumb tacks on the floor.import std.algorithm : reduce, max, min; auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue()); auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue()); TTake a look at Justin Whear's dpaste. Dual pred reduce FTW.
Aug 15 2014
TJB:I am trying to find the max and min values in an associative array. Say I have: double[char] bids; bid['A'] = 37.50; bid['B'] = 38.11; bid['C'] = 36.12; How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to.void main() { import std.stdio, std.algorithm; double[char] bids = ['A': 37.50, 'B': 38.11, 'C': 36.12]; bids.byValue.reduce!(min, max).writeln; } Bye, bearophile
Aug 14 2014