Last active
July 3, 2018 16:28
-
-
Save line-o/eae52c86b0317d9776af1417217bc78b to your computer and use it in GitHub Desktop.
Example for arrow operator and sequences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| xquery version "3.1"; | |
| declare function local:plusOne ($a as xs:integer) { $a + 1 }; | |
| declare | |
| function local:greaterThan ($int as xs:integer, $cmp as xs:integer) as xs:boolean { | |
| $int > $cmp | |
| }; | |
| declare function local:gt ($cmp as xs:integer) as function(*) { | |
| function ($int as xs:integer) as xs:boolean { | |
| $int > $cmp | |
| } | |
| }; | |
| declare function local:ascending ($a) { -$a }; | |
| declare function local:sortBy ($f as function(*)) { | |
| sort(?, (), $f) | |
| }; | |
| declare | |
| function local:take ($s, $n as xs:integer) { | |
| subsequence($s, 1, $n) | |
| }; | |
| (0 to 9) | |
| => for-each(local:plusOne(?)) | |
| => for-each(local:plusOne#1) (: same same, but different :) | |
| => filter(local:greaterThan(?, 5)) (: filter by partially applied function :) | |
| => for-each(math:pow(?, 2)) (: using a function from a module :) | |
| => filter(local:gt(10)) (: filter by using a returned function :) | |
| => local:sortBy(local:ascending#1) (: sort the sequence in ascending order :) | |
| (: => reverse() would have the same effect here :) | |
| => local:take(1) (: return the first item in the sequence :) | |
| => xs:integer() (: turn that single item into an integer :) | |
| (: should return 121 :) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment