This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 29428 - map:fold and map:filter
Summary: map:fold and map:filter
Status: NEW
Alias: None
Product: XPath / XQuery / XSLT
Classification: Unclassified
Component: XQuery 3.2 Use Cases and Requirements (show other bugs)
Version: Candidate Recommendation
Hardware: PC Linux
: P2 normal
Target Milestone: ---
Assignee: Jonathan Robie
QA Contact: Mailing list for public feedback on specs from XSL and XML Query WGs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-07 13:34 UTC by Benito van der Zander
Modified: 2016-05-22 06:40 UTC (History)
1 user (show)

See Also:


Attachments

Description Benito van der Zander 2016-02-07 13:34:14 UTC
There is a surprising lack of higher order functions on maps. 


     

map:filter

Summary

    Returns an map containing those entries of the $map for which $function returns true.
    
Signature
    map:filter(	$map	 as map(*),  $filter as function($key as xs:anyAtomicType, $value as item()*) as xs:boolean ) as map(*)
    
Rules

    The effect of the function is equivalent to the following definition:

    map:merge($map, map:keys() [$filter(., $map(.))] ! map:entry(., $map(.)) )

Error Conditions

    As a consequence of the function signature and the function calling rules, a type error occurs if the supplied function $function returns anything other than a single xs:boolean item; there is no conversion to an effective boolean value.

Examples

    The expression map:filter( map { "A": 1, "B": 2, "C": "Hello world" }, function($x,$y) {$y instance of xs:integer}) returns map { "A": 1, "B": 2 }.


17.3.13 map:fold

Summary

    Evaluates the supplied function cumulatively on successive values of the supplied map.
    
Signature
    map:fold(	$map	 as map(*),
    $zero	 as item()*,
    $function	 as function(item()*, xs:anyAtomicType, item()*) as item()*) as item()*
    
Rules

    The effect of the function is equivalent to the following definition:

    fold-left(map:keys($map), $zero, function($aggregate as item()*, $key as xs:anyAtomicType) as item()* { 
      $function($aggregate, $key, $map($key)) 
    })
             

Notes

    If the supplied map is empty, the function returns $zero.

    If the supplied map contains a single entry [$key,$value], the function returns $zero => $function($key, $value).

    If the supplied map contains two entries [$key, $value] and [$key2, $value2], the function returns $zero => $function($key,$value) => $function($key2,$value2); and similarly for an input map with more than two entries.
    
Examples

    The expression map:fold(map {"a": true(), "b": true(), "c": false()}, true(), function($x, $key, $y){$x and $y}) returns false(). (Returns true if every value of the input map has an effective boolean value of true().).

    The expression map:fold(map {"a": true(), "b": true(), "c": false()}, true(), function($x, $key, $y){$x or $y}) returns true(). (Returns true if at least one value of the input map has an effective boolean value of true().).
Comment 1 Michael Kay 2016-02-07 13:55:08 UTC
Classifying this against future requirements. We're not going to be adding anything to 3.1 just because it seems a good idea; the bar for considering changes to a candidate recommendation has to be set very high.
Comment 2 Vladimir Nesterovsky 2016-05-22 06:40:15 UTC
> 17.3.13 map:fold
> 
>     Evaluates the supplied function cumulatively on successive values of the
> supplied map.
>     
> Signature
>     map:fold(	$map	 as map(*),
>     $zero	 as item()*,
>     $function	 as function(item()*, xs:anyAtomicType, item()*) as item()*)
> as item()*

Please note that map:fold() would be of limited use, as with current definitions it relies on map:keys() functions, which is non-deterministic with respect to ordering. This means that two calls with the same argument are not guaranteed to call $function in the same order.