[PYTHON] [For beginners] Simple sample of list operation functions map, reduce, filter for each language

Here are some simple operations of `` `map filter reduce``` that are often used in list operations in each language. Java is redundant, isn't it? Ruby and Haskell are very concise. However, since I use Java in my daily work, I don't know much about other languages, so There may be a more concise way to write it.

Here is a simple example of.

Make a list of numbers

By the way, it seems that it is not often fixed to initialize the list in business. Normally, list data is generated by the factory method and referenced. However, I sometimes perform the following initialization as test data in tests.

Java


    List<Integer> xs = Arrays.asList(1, 2, 3, 4, 5);
    //Or@Java 10 or later, as taught by saka1029
    var xs = List.of(1, 2, 3, 4, 5);

    //There is also a way to use such an infinite list. Do you not do this in business?
    //Seed value from the outside, function,You can make it by entering the limit value.
    xs = Stream.iterate(1, i -> i + 1).limit(5).collect(toList())

Ruby


    xs = [1, 2, 3, 4, 5]
    #Actually, the above seems to be a bad example,
    # @The following ones taught by scivola are good for initialization.
    xs = [*1..5]

Python


	xs = [1, 2, 3, 4, 5]

Haskell


    xs = [1, 2, 3, 4, 5]
    --Or
    xs = [1..5]

Clojure


    (def xs '(1 2 3 4 5))
    ;Or@How lagenorhynque commented
    (def xs (range 1 (inc 5)))

It is assumed that this list has already been created below.

map function

Take a list and double each value.

The result for each language is `[2, 4, 6, 8, 10]`. The print function is omitted.

Java


	xs.stream().map(i -> i * 2).collect(toList());

Ruby


	xs.map {|i| i * 2}

Python


	list(map(lambda i: i * 2, xs))
	#Perhaps it's normal to use list comprehensions in Python to give this answer.
	[i * 2 for i in xs]

Haskell


	map (*2) xs

Clojure


    (map (fn [n](* n 2)) xs)
    ;Or@What lagenorhynque told me. This one is simple and nice!
    (map #(* % 2) xs)

filter function

Select and return only even numbers.

The result is `` `[2, 4] ```.

Java


	xs.stream().filter(n -> n % 2 == 0).collect(toList());

Ruby


	xs.select(&:even?)

Python


	list(filter(lambda n: n % 2 == 0, xs))
	#Probably, I think that it will be a list comprehension notation without writing as above.
	[n for n in xs if n % 2 == 0]

Haskell


	filter even xs

Clojure


	(filter even? xs)

reduce function

Add the numbers in the list, aggregate and return.

The result is `15`.

Java


    xs.stream().reduce(Integer::sum).orElse(0);

Ruby



    xs.reduce(:+) 
    #Or l.inject(:+)
    #When not using reduce
    xs.sum

Python was unfriendly because there was no import from @shiracamus, so I wrote it.

Python


    from functools import reduce
    from operator import add

    reduce(lambda a, b: a + b, xs)
    #When not using reduce
    sum(xs)
    #Or@I learned from shiracamus,
    reduce(add, xs)

Haskell


	foldl (+) 0 xs
	--When not using fold
	sum xs

Clojure


    (reduce + xs)
    ;@Taught by lagenorhynque
    (apply + xs)
    ;The same result can be obtained by writing. What is done internally(+ 1 2 3 4 5)
    ;reduce is(+ (+ (+ (+ 1 2) 3) 4) 5)

that's all.

I wrote for a language I've studied so far, but I personally wrote Ruby and Clojure, which I found comfortable </ b>. I feel that the feeling is similar. (Because both authors like lisp?) Haskell is too terrible and unpleasant! I think (in a good way) </ b>, but is it fun! </ B> in Ruby, Clojure? I don't know why. Also, Haskell has a completely different way of thinking from other languages, so it's hard to study.