1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
* Stuff
** DONE group_by default function
** DONE group_by_count
** DONE group_by_array
** DONE Remove special subs
**** DONE docs
** DONE Fix throw, remove lib
** DONE Release
** DONE Deprecate Array::Transform, point to new
** DONE map_by, grep_by, group_by hash key lookup
*** if first item is ref HASH
** DONE grep_by - value
** [#A] group (group_count)
*** group_array
** DONE sort
** find(sub), like grep
** find_by($method, $args, $value), like grep_by but first
** contains value
** compact - without undefined
** compactTrue - without false
** DONE reject - grep -v value
*** DONE reject
*** DONE reject_by
*** DONE reject_each
*** DONE reject_each_defined
*** release
** DONE grep --> filter
*** DONE so I can write "grep" instead of autobox::Core
*** DONE deprecate grep, but keep the alias
*** get going with the grep --> filter, so we can use nothing (true), string eq, regex or subref.
*** DONE Leave the old implementation behind, but deprecate it and only briefly mention it in the docs.
** DONE grep, grep_by third arg more dwim
*** DONE third arg sub (defaults to true)
**** if string, do eq
**** if regex, do =~
** DONE enforce scalar / list context, to ref / array, hash
** DONE Also, coerce to scalar context / reference.
*** to_ref / as_ref
*** to_list, as_list
** DONE as_ref
** DONE new format
While doing the rename to filter, can we introduce the ["accessor",
@args] format too? Just special case it, so if the first is an
arrayref, use that format.
** DONE use autobox::Core inside Transform
*** use autobox::Core, so we can use e.g. split
*** Provide this as a parameter with core as the default?
** DONE Fix method with args syntax
*** DONE methods
**** DONE map_by
**** DONE grep_by
**** DONE uniq_by
**** DONE group_by
**** DONE group_by_count
**** DONE group_by_array
*** DONE tasks
**** DONE fix
**** DONE document new style
**** DONE changes
**** DONE release
**** DONE change docs to new style
***** DONE document old style
***** DONE deprecate old style
****** DONE document plan for 2.0.0
**** DONE release
** DONE filter dwim
*** DONE filter
*** DONE filter_by
*** DONE filter_each
** DONE Document filter_each, etc
** DONE array->as_hash, hash->as_array
** TODO map_each_by
** TODO map_each_value_by - replace the hashref/object value with the result of map_by
** TODO group
** TODO group_count
** TODO group_array
** TODO sub uplevel or whatnot
*** so warnings and dies are reported from the caller.
*** SIG __WARN__
**** warn from caller, or use Carp::longmess
** TODO Sorting using order and order_by (h2)
*** TODO tasks
**** DONE order
**** DONE simple value
**** DONE num/str
**** DONE arg validation
***** unknown
**** DONE asc/desc
**** DONE multiple options
**** DONE arg validation - only one in each group, no repeating
**** DONE subref
**** DONE regex
**** DONE multiple comparisons
**** DONE order_by
**** docs: order_by must return a single value that can be compared as a scalar
**** bonus: rewrite map_by to take a subref transformer, so it can be used here
***** and do the map_by and extractor in the same go, with just the map_by return values
**** cache sorts?
**** undef warnings?
*** Sorting with sort
- provide a sub that returns the comparison outcome of two values: $a and $b
- in case of a tie, provide another comparison of $a and $b
# If the name is the same, compare age (oldest first)
sort {
uc( $a->{name} ) cmp uc( $b->{name} )
||
int( $b->{age} / 10 ) <=> int( $a->{age} / 10 )
} @users
(note the opposite order of $a and $b for the age comparison,
something that's often difficult to discern at a glance)
*** Sorting with order, order_by
- provide order options for how one value should be compared with another value
- the value can be transformed using an optional subref, e.g. by uc($_)
- how to compare (cmp or <=>)
- which direction to sort (ascending or descending)
- in case of a tie, provide another comparison
# If the name is the same, compare age (oldest first)
@users->order(
sub { $_->{name} }, # first comparison
[ "num", sub { int( $_->{age} / 10 ) }, "desc" ], # second comparison
)
@users->order_by(
name => "str", # first comparison
age => [ num => desc => sub { int( $_ / 10 ) } ], # second comparison
)
*** Order Options for a comparison
If there's only one option for a comparison, provide a single option
(string/regex/subref) value. If there are many options, provide them
in an arrayref in any order.
*** Comparison operator
- "str" (cmp) - default
- "num" (<=>)
- "nat" (natural sort)
*** Sort order
- "asc" (ascending) - default
- "desc" (descending)
*** The value to compare
- A subref - default is: sub { $_ }
- The return value is used in the comparison
- A regex
- The value of join("", @captured_groups) are used in the comparison (i.e. $1, $2, $3 etc.)
*** Examples
## Simple
# order: first item is the comparison options (one or an arrayref
# with many)
->order() # Defaults: str, asc, $_, just like sort
->order(sub { uc($_) })
->order( qr/first_name: (\w+), last_name: (\w+)/ )
->order([ num => qr/id: (\d+)/ ])
->order([ sub { int($_) }, "num" ])
# order_by: first item is the accessor, second item is the
# comparison options (one or an arrayref with many)
->order_by("id")
->order_by("id", "num")
->order_by("name", sub { uc($_) })
->order_by(log_line => qr/first_name: (\w+), last_name: (\w+)/ )
->order_by("log_line", [ num => qr/id: (\d+)/ ])
->order_by(age => [ sub { int($_) }, "num" ])
->order_by([ age_by_interval => 10 ] => [ sub { int($_) }, "num" ])
->order_by([ name_with_title => $title ], sub { uc($_) })
## Multiple comparisons
# order: subsequent comparison options are added as needed (one or
# an arrayref with many, per comparison)
->order(
[ sub { uc($_) }, "desc" ],
"str",
)
->order(
[ sub { $_->{price} }, "num" ], # First a numeric comparison of price
[ sub { $_->{name} }, "desc" ], # or if same, a reverse comparison of the name
)
->order(
qr/type: (\w+)/,
[ num => desc => qr/duration: (\d+)/ ]
[ num => sub { /id: (\d+)/ } ],
"str",
)
# order_by:
...
*** Order methods
See L</order> and L</order_by>
*** TODO Fresh
**** order
***** description
****** for each key there's an option or arrayref of options
****** If there are multiple keys to compare
******* there are multiple option(s)
***** examples
**** order_by
***** description
****** for each key, there's pair of accessor-option(s)
******* the first item is always the accessor
******* the second item is always an option or an arrayref of options
****** if there are multiple keys to compare
******* there are multiple pairs of accessor-option(s)
***** param check
****** when all items are consumed,
******* if there's an expected option
******** the default is an empty option list
***** examples
->order_by("name")
->order_by(name => sub { int($_) })
->order_by(name => [ "desc", sub { int($_) } ])
->order_by([ price => $discount ] => sub { int($_) })
->order_by([ price => $discount ] => [ sub { int($_) }, "num" ])
->order_by(
[ price => $discount ] => [ sub { int($_) }, "num" ],
name => [ sub { uc($_) }, "desc" ],
time => [ ],
[ duration => $now ] => "natural",
)
*** CPAN
**** https://v1.metacpan.org/pod/Sort::Maker
**** https://v1.metacpan.org/pod/Sort::Key
**** https://metacpan.org/pod/Sort::Key::Multi
**** https://metacpan.org/pod/Sort::Key::Maker
**** https://metacpan.org/pod/Sort::Key::Natural
*** Old attempts
**** multi key using hashrefs
***** description
***** examples
->order("num")
->order(sub { int($_) }, "num")
->order(
{ num => 1, value => sub { uc($_) } },
{ str => 1, value => sub { int($_) } },
)
->order({ num => sub { $_->[1] } })
->order_by(
{ num => [ "price", 0.22 ] },
{ str => "symbol", value => sub { uc($_) } },
{ num => "id" },
)
->order_by("symbol")
->order_by([ price => 0.22 ])
->order_by({ num => "id" })
->order_by({ num => [ "price" => 0.22 ] })
**** multi key using arrayrefs
***** description
****** for _by, first item is always the accessor
****** the rest are named options
******* e.g. option strings, or a subref to transform it
***** examples
->order("num")
->order(sub { int($_) }, "num")
->order(
[ num => sub { uc($_) } ],
[ "str", sub { int($_) } ],
)
->order(num => sub { $_->[1] })
->order_by(
# PROBLEM: how is this distinguished from an arrayref accessor?
[ [ "price", 0.22 ], "num" ],
[ "symbol", str => sub { uc($_) } ],
[ id => "num" ],
)
->order_by("symbol")
->order_by([ price => 0.22 ])
->order_by(id => "num", "reverse")
->order_by([ "price" => 0.22 ], "num")
** set_each
set_each([$accessor, @args])
This uses the same format for getting at a accessor and args as
everything else, i.e. an arrayref.
For hashrefs, set the key $accessor to $args[0] (because a hashref
value can't be a list)
For objects, call $_->$accessor(@args) to set the value.
** array includes, includes_by / contains, contains_by
*** http://emberjs.com/api/classes/Ember.Enumerable.html
*** with predicate, same as always: string eq, regex, hashref keys, (array in (later))
*** hash contains_value_by
** hash contains_key, contains_value -- checks the predicate for keys/values
** Ember Array, Enumerable
*** http://emberjs.com/api/classes/Ember.Array.html
** Fix croak, use throw
* Hash
** DONE key_value($key, $new_key_name=$key) : ($new_key_name => $value) | { $new_key_name => $value }
*** exists
*** exists, undef
*** doesn't exist, undef
** DONE key_value_if_exists($key, $new_key_name=$key) : ($new_key_name => $value) | { $new_key_name => $value }
** DONE key_value_if_true($key, $new_key_name=$key) : ($new_key_name => $value) | { $new_key_name => $value }
** DONE key_value_if_defined($key, $new_key_name=$key) : ($new_key_name => $value) | { $new_key_name => $value }
** keys_value* to support multiple pairs
*** if there are more than one arg, assume they are pairs
** map family
*** map hash -> array
**** [#A] map_each_to_array($subref)
***** ($key, $value), $_ is value
****** return value (or die) to be list item
***** return array with return values
*** map hash -> hash
**** map_each($subref)
***** ($key, $value), $_ is value
****** return new key, new value (or die)
***** return hash with return values
**** map_each_value($subref, $new_key = $key)
***** ($key, $value), $_ is value
****** return new value
***** return hash with same key, return value
**** map_each_value_by($accessor, $new_key = $accessor) maps values from one thing to another
***** (key, $value, $accessor_value), $_ is accessor_value
****** return new value
***** return hash with same keys + $new_key = return value
**** map_by ?
** grep family
*** filter -> hash
**** [#A] filter_each($subref = true)
***** ($key, $value), $_ is value
**** [#B] filter_each_defined
***** value is defined
**** filter_each_by($accessor, $args?, $subref = true)
***** ($key, $value, $accessor_value), $_ is accessor value
**** filter_each_by_defined
***** accessor value is defined
* DOCS
** Operate on an array of scalars
*** TODO grep_true
*** TODO grep_defined
*** flat
*** TODO group
*** TODO group_count
*** TODO group_array
** Operate on a hash
*** map_each
*** map_each_value
*** map_each_to_array
*** grep_each
*** TODO grep_each_true
*** grep_each_defined
*** key_value
*** key_value_if_true
*** key_value_if_defined
*** key_value_if_exists
** Operate on an array of hashrefs/objects
*** map_by
*** grep_by
*** TODO grep_by_true
*** TODO grep_by_defined
*** uniq_by
*** group_by
*** group_by_array
*** group_by_count
** Operate on a hash with hashref/object values
*** TODO map_each_value_by
*** TODO grep_each_value_by
*** TODO grep_each_value_by_true
*** TODO grep_each_value_by_defined
* Cookbook
** Document the gems from core
*** elements
**** avoid ugly deref punctuation
*** length
*** strip
**** better chomp
** map_by("strip")
** map_each_value_by("group_count")
** map_by("flat")
** map_by([ split => "/" ])
** DBIC
->all doesn't work so well, since it returns a list
Base ResultSet
sub all_ref { [ shift->all ] }
** etc
|