.. _stdlib_functional: ============================== 函数式编程库 ============================== .. include:: detail/functional.rst 功能模块实现一组高阶函数和模式,以向 Daslang 公开函数编程模式。 所有函数和符号都在 “functional” 模块中,使用 require 来访问它。:: require daslib/functional +++++++++++ Map, reduce +++++++++++ * :ref:`filter (src:iterator\ -const;blk:lambda\<(what:TT const -&):bool\> const) : auto ` * :ref:`filter (src:iterator\ -const;blk:function\<(what:TT const -&):bool\> const) : auto ` * :ref:`map (src:iterator\ -const;blk:lambda\<(what:TT const -&):auto(QQ)\> const) : auto ` * :ref:`map (src:iterator\ -const;blk:function\<(what:TT const -&):auto(QQ)\> const) : auto ` * :ref:`reduce (it:iterator\ const;blk:lambda\<(left:TT const -&;right:TT const -&):TT const -&\> const) : auto ` * :ref:`reduce (it:iterator\ const;blk:function\<(left:TT const -&;right:TT const -&):TT const -&\> const) : auto ` * :ref:`reduce (it:iterator\ const;blk:block\<(left:TT const -&;right:TT const -&):TT const -&\> const) : auto ` * :ref:`sum (it:iterator\ const) : auto ` * :ref:`any (it:auto const) : auto ` * :ref:`all (it:auto const) : auto ` * :ref:`cycle (src:iterator\ -const) : auto ` * :ref:`islice (src:iterator\ -const;start:int const;stop:int const) : auto ` * :ref:`repeat_ref (value:auto(TT) const;total:int -const) : auto ` * :ref:`repeat (value:auto(TT) const;count:int -const) : auto ` * :ref:`not (x:auto const) : auto ` * :ref:`echo (x:auto -const;extra:string const) : auto ` * :ref:`flatten (it:iterator\ -const) : auto ` .. _function-_at_functional_c__c_filter_1_ls_Y_ls_TT_gr_._gr_G_CN_ls_what_gr_0_ls_CY_ls_TT_gr_L_gr_1_ls_b_gr__at_: .. das:function:: filter(src: iterator; blk: lambda<(what:TT const):bool> const) filter returns auto +--------+----------------------------------+ +argument+argument type + +========+==================================+ +src +iterator + +--------+----------------------------------+ +blk +lambda<(what:TT const):bool> const+ +--------+----------------------------------+ 迭代 `src` 并仅生成 `blk` 返回 true 的那些元素 filter with function .. _function-_at_functional_c__c_filter_1_ls_Y_ls_TT_gr_._gr_G_CN_ls_what_gr_0_ls_CY_ls_TT_gr_L_gr_1_ls_b_gr__at__at_: .. das:function:: filter(src: iterator; blk: function<(what:TT const):bool> const) filter returns auto +--------+------------------------------------+ +argument+argument type + +========+====================================+ +src +iterator + +--------+------------------------------------+ +blk +function<(what:TT const):bool> const+ +--------+------------------------------------+ 迭代 `src` 并仅生成 `blk` 返回 true 的那些元素 filter with function .. _function-_at_functional_c__c_map_1_ls_Y_ls_TT_gr_._gr_G_CN_ls_what_gr_0_ls_CY_ls_TT_gr_L_gr_1_ls_Y_ls_QQ_gr_._gr__at_: .. das:function:: map(src: iterator; blk: lambda<(what:TT const):auto(QQ)> const) map returns auto +--------+--------------------------------------+ +argument+argument type + +========+======================================+ +src +iterator + +--------+--------------------------------------+ +blk +lambda<(what:TT const):auto(QQ)> const+ +--------+--------------------------------------+ 减少 值,任何可调用 .. _function-_at_functional_c__c_map_1_ls_Y_ls_TT_gr_._gr_G_CN_ls_what_gr_0_ls_CY_ls_TT_gr_L_gr_1_ls_Y_ls_QQ_gr_._gr__at__at_: .. das:function:: map(src: iterator; blk: function<(what:TT const):auto(QQ)> const) map returns auto +--------+----------------------------------------+ +argument+argument type + +========+========================================+ +src +iterator + +--------+----------------------------------------+ +blk +function<(what:TT const):auto(QQ)> const+ +--------+----------------------------------------+ 减少 值,任何可调用 .. _function-_at_functional_c__c_reduce_C1_ls_Y_ls_TT_gr_._gr_G_CN_ls_left;right_gr_0_ls_CY_ls_TT_gr_L;CY_ls_TT_gr_L_gr_1_ls_CY_ls_TT_gr_L_gr__at_: .. das:function:: reduce(it: iterator const; blk: lambda<(left:TT const;right:TT const):TT const> const) reduce returns auto +--------+-----------------------------------------------------+ +argument+argument type + +========+=====================================================+ +it +iterator const + +--------+-----------------------------------------------------+ +blk +lambda<(left:TT const;right:TT const):TT const> const+ +--------+-----------------------------------------------------+ 所有元素的总和 .. _function-_at_functional_c__c_reduce_C1_ls_Y_ls_TT_gr_._gr_G_CN_ls_left;right_gr_0_ls_CY_ls_TT_gr_L;CY_ls_TT_gr_L_gr_1_ls_CY_ls_TT_gr_L_gr__at__at_: .. das:function:: reduce(it: iterator const; blk: function<(left:TT const;right:TT const):TT const> const) reduce returns auto +--------+-------------------------------------------------------+ +argument+argument type + +========+=======================================================+ +it +iterator const + +--------+-------------------------------------------------------+ +blk +function<(left:TT const;right:TT const):TT const> const+ +--------+-------------------------------------------------------+ 所有元素的总和 .. _function-_at_functional_c__c_reduce_C1_ls_Y_ls_TT_gr_._gr_G_CN_ls_left;right_gr_0_ls_CY_ls_TT_gr_L;CY_ls_TT_gr_L_gr_1_ls_CY_ls_TT_gr_L_gr__builtin_: .. das:function:: reduce(it: iterator const; blk: block<(left:TT const;right:TT const):TT const> const) reduce returns auto +--------+----------------------------------------------------+ +argument+argument type + +========+====================================================+ +it +iterator const + +--------+----------------------------------------------------+ +blk +block<(left:TT const;right:TT const):TT const> const+ +--------+----------------------------------------------------+ 所有元素的总和 .. _function-_at_functional_c__c_sum_C1_ls_Y_ls_TT_gr_._gr_G: .. das:function:: sum(it: iterator const) sum returns auto +--------+------------------------+ +argument+argument type + +========+========================+ +it +iterator const+ +--------+------------------------+ 迭代 `it` 并产生所有元素的总和 same as reduce(it, @(a,b) => a + b) any .. _function-_at_functional_c__c_any_C.: .. das:function:: any(it: auto const) any returns auto +--------+-------------+ +argument+argument type+ +========+=============+ +it +auto const + +--------+-------------+ 迭代 `it` 并生成 true,如果任何元素为 true。 all .. _function-_at_functional_c__c_all_C.: .. das:function:: all(it: auto const) all returns auto +--------+-------------+ +argument+argument type+ +========+=============+ +it +auto const + +--------+-------------+ 迭代 `it` 并生成 true,如果所有元素都为 true,则生成 true .. _function-_at_functional_c__c_cycle_1_ls_Y_ls_TT_gr_._gr_G: .. das:function:: cycle(src: iterator) cycle returns auto +--------+------------------+ +argument+argument type + +========+==================+ +src +iterator+ +--------+------------------+ endlessly iterates over `src` .. _function-_at_functional_c__c_islice_1_ls_Y_ls_TT_gr_._gr_G_Ci_Ci: .. das:function:: islice(src: iterator; start: int const; stop: int const) islice returns auto +--------+------------------+ +argument+argument type + +========+==================+ +src +iterator+ +--------+------------------+ +start +int const + +--------+------------------+ +stop +int const + +--------+------------------+ 迭代 `src` 并仅生成 Range[start,stop) 中的元素 [[ value; value; value; .... count times ]] .. _function-_at_functional_c__c_repeat_ref_CY_ls_TT_gr_._i: .. das:function:: repeat_ref(value: auto(TT) const; total: int) repeat_ref returns auto +--------+--------------+ +argument+argument type + +========+==============+ +value +auto(TT) const+ +--------+--------------+ +total +int + +--------+--------------+ 通过引用 `count` 次产生 `value` [[ value; value; value; .... count times ]] .. _function-_at_functional_c__c_repeat_CY_ls_TT_gr_._i: .. das:function:: repeat(value: auto(TT) const; count: int) repeat returns auto +--------+--------------+ +argument+argument type + +========+==============+ +value +auto(TT) const+ +--------+--------------+ +count +int + +--------+--------------+ 产生 `value` `count` 次 .. _function-_at_functional_c__c_not_C.: .. das:function:: not(x: auto const) not returns auto +--------+-------------+ +argument+argument type+ +========+=============+ +x +auto const + +--------+-------------+ yeilds !x .. _function-_at_functional_c__c_echo_._Cs: .. das:function:: echo(x: auto; extra: string const) echo returns auto +--------+-------------+ +argument+argument type+ +========+=============+ +x +auto + +--------+-------------+ +extra +string const + +--------+-------------+ 将字符串的内容打印到输出中,并附加 `extra` 字符串 .. _function-_at_functional_c__c_flatten_1_ls_Y_ls_TT_gr_._gr_G: .. das:function:: flatten(it: iterator) flatten returns auto +--------+------------------+ +argument+argument type + +========+==================+ +it +iterator+ +--------+------------------+ 迭代 `it`,然后迭代 `it` 的每个元素并生成它 +++++++ Queries +++++++ * :ref:`is_equal (a:auto const;b:auto const) : auto ` * :ref:`is_not_equal (a:auto const;b:auto const) : auto ` .. _function-_at_functional_c__c_is_equal_C._C.: .. das:function:: is_equal(a: auto const; b: auto const) is_equal returns auto +--------+-------------+ +argument+argument type+ +========+=============+ +a +auto const + +--------+-------------+ +b +auto const + +--------+-------------+ 如果 `a` 和 `b` 相等,则生成 true .. _function-_at_functional_c__c_is_not_equal_C._C.: .. das:function:: is_not_equal(a: auto const; b: auto const) is_not_equal returns auto +--------+-------------+ +argument+argument type+ +========+=============+ +a +auto const + +--------+-------------+ +b +auto const + +--------+-------------+ 如果 `a` 和 `b` 不相等,则生成 true +++++++++++++ Uncategorized +++++++++++++ .. _function-_at_functional_c__c_sorted_1_ls_._gr_A: .. das:function:: sorted(arr: array) sorted returns auto +--------+-------------+ +argument+argument type+ +========+=============+ +arr +array + +--------+-------------+ 迭代 input 并返回其排序版本 .. _function-_at_functional_c__c_sorted_1_ls_Y_ls_TT_gr_._gr_G: .. das:function:: sorted(it: iterator) sorted returns auto +--------+------------------+ +argument+argument type + +========+==================+ +it +iterator+ +--------+------------------+ 迭代 input 并返回其排序版本