1 /** This file is part of Shapes.
3 ** Shapes is free software: you can redistribute it and/or modify
4 ** it under the terms of the GNU General Public License as published by
5 ** the Free Software Foundation, either version 3 of the License, or
8 ** Shapes is distributed in the hope that it will be useful,
9 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ** GNU General Public License for more details.
13 ** You should have received a copy of the GNU General Public License
14 ** along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 ** Copyright 2014, 2015 Henrik Tidefelt
19 ##needs ..Shapes..Data / seq-support
22 ##lookin ..Shapes..Data
24 /** Text input form of an array.
26 vectorInputForm: \ v → (String..newString << `[vector ´ << [seq_string [separate ` ´ v.list]] << ` ]´)
28 /** Data to be sorted.
42 /** Sort keys, and show.
44 keysSorted: [lexiographicSort keys]
45 IO..•stdout << `Sorted keys:´ << "{n}
46 IO..•stdout << [seq_string [separate "{n} [fmap vectorInputForm keysSorted]]] << "{n}
48 /** Values to associate with keys.
49 ** Typically, the keys are computed based on the values, but this will be shown further down.
51 values: [list `one´ `two´ `three´ `four´ `five´ `six´ `seven´ `eight´ `nine´]
53 /** Sort values associated with keys, and show.
55 valuesSorted: [lexiographicSort keys values]
56 IO..•stdout << `Sorted values associated with keys:´ << "{n}
57 IO..•stdout << [seq_string [separate "{n} valuesSorted]] << "{n}
59 /** Now the typical situation where the keys are derived from the values.
60 ** Sort the string values in alphabetical order by deriving keys from the values.
61 ** Of course, a dedicated string sorting function would be mch more efficient, but
62 ** this example is just about showing how keys can naturally be derived from the
63 ** values to be sorted.
65 stringKey: \ str → vector [] <>[unlist [fmap ( \ i → [str.UTF8select i].code ) [range '0 count:str.UTF8count]]]
66 valueKeys: [fmap stringKey values]
67 IO..•stdout << `Keys derived from strings:´ << "{n}
68 IO..•stdout << [seq_string [separate "{n} [fmap vectorInputForm valueKeys]]] << "{n}
70 valuesAlphabetical: [lexiographicSort valueKeys values]
71 IO..•stdout << `Strings sorted alphabetically:´ << "{n}
72 IO..•stdout << [seq_string [separate "{n} valuesAlphabetical]] << "{n}