Shooting arrows fast and accurately

62daacd9f1933897be51b797ad7aa13b?s=47 line-o
February 15, 2020

Shooting arrows fast and accurately

How to use XQuery arrow expressions for fun and profit.

62daacd9f1933897be51b797ad7aa13b?s=128

line-o

February 15, 2020
Tweet

Transcript

  1. Shooting Arrows fast and accurately

  2. Hi! I am Juri Leino GitHub: @line-o twitter: @line_o 2/55

    — XML Prague 2020
  3. arrow expressions 3/55 — XML Prague 2020

  4. arrow expressions evaluation order A !" f(B, C) evaluates to

    f(A, B, C) 4/55 — XML Prague 2020
  5. arrow expressions "hello Prague" !" upper-case() !" tokenize(" ") is

    the same as tokenize( upper-case( "hello Prague"), " ") 5/55 — XML Prague 2020
  6. learnings 6/55 — XML Prague 2020

  7. learnings 123 !" function ($a) { $a * $a }

    7/55 — XML Prague 2020
  8. learnings 123 !" (function ($a) { $a * $a })()

    8/55 — XML Prague 2020
  9. learnings (: 123 "# :) (function ($a) { $a *

    $a })(123) 9/55 — XML Prague 2020
  10. 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
  11. learnings (1, 2, 3) !" count() (: !$ 3 :)

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

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

    B (A !" B) ! C 13/55 — XML Prague 2020
  14. functional programming a quick recap 14/55 — Photo by Victor

    Xok on Unsplash -- XML Prague 2020
  15. partial application 15/55 — XML Prague 2020

  16. 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
  17. partial application (2, 1, 3, 8) ! $add48(.) !" codepoints-to-string()

    (: !% "2138" :) 17/55 — XML Prague 2020
  18. higher order functions functions parameterised by or returning a function

    18/55 — XML Prague 2020
  19. higher order functions • fn:function-arity#1 • fn:function-name#1 • fn:function-lookup#2 19/55

    — XML Prague 2020
  20. 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
  21. higher order functions (2, 1, 3, 8) !" for-each($add48) !"

    for-each($add16) !" codepoints-to-string() (: !% "BACH" :) 21/55 — XML Prague 2020
  22. Gluten-free programming™ no flour (FLWOR) 22/55 — XML Prague 2020

  23. Gluten-free programming™ replace for, let, order by, group by, where

    with for-each, sort, fold-left, filter 23/55 — XML Prague 2020
  24. xbow 24/55 — XML Prague 2020

  25. Like functx, but for arrow expressions ! Priscilla Walmsley 25/55

    — XML Prague 2020
  26. (numerical) Comparisons gt, ge, lt, le, eq 26/55 — XML

    Prague 2020
  27. 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
  28. helpers take, last, to-sequence, to-array, sortBy, ascending, descending, for-each-index, map-reverse,

    28/55 — XML Prague 2020
  29. helpers (2, 1, 3, 8) !" xbow:descending() !" xbow:take(2) !"

    xbow:to-array() (: !% [8, 3] :) 29/55 — XML Prague 2020
  30. 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
  31. XML helpers wrap-element, wrap-each, filter-attributes, attributes-to-map... 31/55 — XML Prague

    2020
  32. reduce Boil a sequence down to a boolean 32/55 —

    XML Prague 2020
  33. 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
  34. 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
  35. accessors A function retrieving a value from a (complex) item.

    35/55 — XML Prague 2020
  36. accessors $posts !" ( map { "views": 10 }, map

    { "views": 1 }, map { "views": 4922 }, map { "views": 16 }, map { "views": 345 } ) 36/55 — XML Prague 2020
  37. accessors function ($post) { $post?views }, map:get(?, 'views'), xbow:pluck('views') 37/55

    — XML Prague 2020
  38. accessors $posts !" for-each(xbow:pluck('views')) !" filter(xbow:gt(10)) (: returns :) (16,

    345, 4922) 38/55 — XML Prague 2020
  39. accessors $posts !" for-each(xbow:pluck('views')) !" filter(xbow:gt(10)) !" avg() (: returns

    :) 1761 39/55 — XML Prague 2020
  40. accessors $posts !" filter( xbow:gt(10, xbow:pluck('views')) (: returns :) (

    map { "views": 16 }, map { "views": 4923 }, map { "views": 345 } ) 40/55 — XML Prague 2020
  41. 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
  42. combine Merge functions into one 42/55 — XML Prague 2020

  43. 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
  44. categorize Clustering data by rules 44/55 — XML Prague 2020

  45. 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
  46. 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
  47. 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
  48. 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
  49. Readable Code 49/55 — XMLPrague 2020 – Photo by Markus

    Spiske on Unsplash
  50. Language 50/55 — XMLPrague 2020 – Photo by Mark Rasmuson

    on Unsplash
  51. evolution 51/55 — XMLPrague 2020 – Photo by erokism on

    Flickr (CC BY 2.0)
  52. Take part in it! • xquery/XSLT 3.1 (2017) • github.com/expath/xpath-ng

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

    decomposition • inline function syntax 53/55 — XML Prague 2020
  54. 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
  55. Thanks for having me; Go shoot some arrows! github.com/line-o/xbow 55/55

    — XML Prague 2020