Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Shooting arrows fast and accurately

line-o
February 15, 2020

Shooting arrows fast and accurately

How to use XQuery arrow expressions for fun and profit.

line-o

February 15, 2020
Tweet

Other Decks in Programming

Transcript

  1. arrow expressions "hello Prague" !" upper-case() !" tokenize(" ") is

    the same as tokenize( upper-case( "hello Prague"), " ") 5/55 — XML Prague 2020
  2. learnings (: 123 "# :) (function ($a) { $a *

    $a })(123) 9/55 — XML Prague 2020
  3. learnings • (1 to 9) !" sum#1 • error •

    (1 to 9) !" sum(.) • error again • 'world' !" concat('hello ', ?) • anonymous function with arity 1 10/55 — XML Prague 2020
  4. learnings (1, 2, 3) !" count() (: !$ 3 :)

    (1, 2, 3) ! count(.) (: !$ (1, 1, 1) :) 11/55 — XML Prague 2020
  5. !"! A ! B A ! B ! C A

    !" B A !" B !" C 12/55 — XML Prague 2020
  6. !"! A ! (.) !" B A ! f(.) !"

    B (A !" B) ! C 13/55 — XML Prague 2020
  7. partial application declare function local:add ($a, $b) { $a +

    $b }; let $add16 !" local:add(?, 16) let $add48 !" local:add(48, ?) 16/55 — XML Prague 2020
  8. higher order functions • fn:apply#2 • fn:for-each#2 • fn:filter#2 •

    fn:sort#3 • fn:fold-left#3 • fn:fold-right#3 20/55 — XML Prague 2020
  9. higher order functions (2, 1, 3, 8) !" for-each($add48) !"

    for-each($add16) !" codepoints-to-string() (: !% "BACH" :) 21/55 — XML Prague 2020
  10. Gluten-free programming™ replace for, let, order by, group by, where

    with for-each, sort, fold-left, filter 23/55 — XML Prague 2020
  11. comparisons xbow:gt(5) (: "# function(xs:numeric) :) 1 "$ xbow:gt(5)() (:

    "# false() :) (1 to 9) "$ filter(xbow:gt(5)) (: "# (6, 7, 8, 9) :) 27/55 — XML Prague 2020
  12. helpers (2, 1, 3, 8) !" xbow:descending() !" xbow:take(2) !"

    xbow:to-array() (: !% [8, 3] :) 29/55 — XML Prague 2020
  13. helpers (6 to 9) !" xbow:for-each-index( function ($item, $pos) {

    [$pos, $item] }) (: !% ([1, 6], [2, 7], [3, 8], [4, 9]) :) 30/55 — XML Prague 2020
  14. reduce (1, 2, 4) !" xbow:some(xbow:gt(1)) (: !$ true() :)

    (1, 2, 4) !" xbow:all(xbow:le(4)) (: !$ true() :) (1, 2, 4) !" xbow:none(xbow:gt(10)) (: !$ true() :) 33/55 — XML Prague 2020
  15. reduce (1 to 10) !" xbow:group-by(xbow:even-odd(?)) (: returns :) map

    { "even": (2, 4, 6, 8, 10), "odd": (1, 3, 5, 7, 9), } 34/55 — XML Prague 2020
  16. accessors $posts !" ( map { "views": 10 }, map

    { "views": 1 }, map { "views": 4922 }, map { "views": 16 }, map { "views": 345 } ) 36/55 — XML Prague 2020
  17. accessors $posts !" filter( xbow:gt(10, xbow:pluck('views')) (: returns :) (

    map { "views": 16 }, map { "views": 4923 }, map { "views": 345 } ) 40/55 — XML Prague 2020
  18. accessors [ <year n="2001" !", <year n="2020" !", <year n="1999"

    !" ] !# array:filter( xbow:gt(2000, function ($y) { xs:integer($y/@n) }) 41/55 — XML Prague 2020
  19. xbow:combine $large-sequence "# for-each( $a ) "# for-each( $b )

    "# for-each( $c ) $large-sequence "# for-each( xbow:combine(($a, $b, $c))) 43/55 — XML Prague 2020
  20. categorize (: A set of rules :) let $post-view-rules #$

    [ xbow:eq(0), (: no one :) xbow:le(10), (: just a few :) xbow:le(100), (: almost successful :) xbow:gt(100) (: successful posts :) ] 45/55 — XML Prague 2020
  21. categorize (: And a data set :) $posts "# xbow:categorize(

    $post-view-rules, xbow:pluck('views')) (: returns :) [ (), (map { "views":"1" }), (map { "views":"10" }, map { "views":"16" }), (map { "views":"4923" }, map { "views":"345" }) ] 46/55 — XML Prague 2020
  22. categorize (: we can now label those for human consumption

    :) $posts "# xbow:categorize( $post-view-rules, xbow:pluck('views')) "# xbow:label([ 'no one', 'just a few', 'almost successful', 'successful posts' ]) 47/55 — XML Prague 2020
  23. categorize (: generate ten complex rules :) let $rules "#

    xbow:array-fill(10, function ($item, $ignore) { math:pow($item, 4) "% xbow:lt() }) return $posts "% xbow:categorize($rules, xbow:pluck('views')) 48/55 — XML Prague 2020
  24. Take part in it! • xquery/XSLT 3.1 (2017) • github.com/expath/xpath-ng

    (2017) • xquery/XSLT 4 (?) 52/55 — XML Prague 2020
  25. Cool things already proposed • if without else • structure

    decomposition • inline function syntax 53/55 — XML Prague 2020
  26. What I would like to see • the discussion to

    continue • a process to extend and evolve xquery • select function by arity and type • async, await (over promises) • generators • tooling (!) 54/55 — XML Prague 2020