• reduce, map, :+ and :upcase as blocks and symbols as arguments

    From Fernando Basso@21:1/5 to All on Thu Jun 28 06:25:26 2018
    Why does `reduce' work passing both `&:+' _and_ `:+', but `map' seems to
    accept only `&:upcase' (but not `:upcase`)?

    (1..5).reduce(:+)
    15
    (1..5).reduce(&:+)
    15
    ['x', 'y', 'z'].map(:upcase)
    ArgumentError: wrong number of arguments (given 1, expected 0)
    from (pry):3:in `map'
    ['x', 'y', 'z'].map(&:upcase)
    ["X", "Y", "Z"]

    Docs for `Enumerable#reduce' say:

    ""
    Combines all elements of enum by applying a binary
    operation, specified by a block or a symbol that names a
    method or operator.
    ""

    Docs `Enumerable#map' say:

    ""
    Returns a new array with the results of running block once
    for every element in enum.
    ""

    So, is that `reduce' takes both a block and a symbol, while map takes
    only a block? And if so, is that some Ruby inconsistency?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Fernando Basso on Thu Jul 5 08:23:33 2018
    On 28.06.2018 11:25, Fernando Basso wrote:
    Why does `reduce' work passing both `&:+' _and_ `:+', but `map' seems to accept only `&:upcase' (but not `:upcase`)?

    (1..5).reduce(:+)
    15
    (1..5).reduce(&:+)
    15
    ['x', 'y', 'z'].map(:upcase)
    ArgumentError: wrong number of arguments (given 1, expected 0)
    from (pry):3:in `map'
    ['x', 'y', 'z'].map(&:upcase)
    ["X", "Y", "Z"]

    Docs for `Enumerable#reduce' say:

    ""
    Combines all elements of enum by applying a binary
    operation, specified by a block or a symbol that names a
    method or operator.
    ""

    Docs `Enumerable#map' say:

    ""
    Returns a new array with the results of running block once
    for every element in enum.
    ""

    So, is that `reduce' takes both a block and a symbol, while map takes
    only a block?

    Yes.

    And if so, is that some Ruby inconsistency?

    Yes.

    Actually I am surprised that #reduce / #inject accepts a single Symbol.
    But these methods have some subtleties in argument handling anyway, so
    maybe I should not be surprised.

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Fernando Basso@21:1/5 to Robert Klemme on Thu Jul 5 07:01:17 2018
    On 05/07/2018 03:23, Robert Klemme wrote:
    On 28.06.2018 11:25, Fernando Basso wrote:
    Why does `reduce' work passing both `&:+' _and_ `:+', but `map' seems to
    accept only `&:upcase' (but not `:upcase`)?

    (1..5).reduce(:+)
    15
    (1..5).reduce(&:+)
    15
    ['x', 'y', 'z'].map(:upcase)
    ArgumentError: wrong number of arguments (given 1, expected 0)
    from (pry):3:in `map'
    ['x', 'y', 'z'].map(&:upcase)
    ["X", "Y", "Z"]

    Docs for `Enumerable#reduce' say:

    ""
    Combines all elements of enum by applying a binary
    operation, specified by a block or a symbol that names a
    method or operator.
    ""

    Docs `Enumerable#map' say:

    ""
    Returns a new array with the results of running block once
    for every element in enum.
    ""

    So, is that `reduce' takes both a block and a symbol, while map takes
    only a block?

    Yes.

    Okay. I'm glad I more or less got that from reading the docs.


    And if so, is that some Ruby inconsistency?

    Yes.

    Okay, I'm yet to see very consistent language, actually. It looks like
    all of them have some quirk spots.


    Actually I am surprised that #reduce / #inject accepts a single Symbol.
    But these methods have some subtleties in argument handling anyway, so
    maybe I should not be surprised.

    Indeed, makes sense. Thanks.

    With gratitude,
    Fernando

    Kind regards

        robert



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)