[JAVA] About the explanation about functional type

at first

I myself am interested in functional languages and visit various sites Some sites carefully describe the merits of functional languages and make you think "functional languages are good", while others say "well, this doesn't make you feel the merits of functional languages". This time, I would like to write about the features of "a site that does not make you feel the merits of functional languages".

Specific site example

The sample is Code IQ site. The purpose of the above site seems to be that array processing is written in imperative language (C language) and functional language (Haskell), and functional language (Haskell) is easy to understand by comparing both.

What's wrong

I sincerely agree that the description of functional languages (Haskell) is very concise, but I personally think that there are some problems with the logic of C, which is given as an example of imperative languages. .. So I'm not sure if what is said to be the benefit of the last functional language (Haskell) on the site really benefits. For example, if reduce and map, which are said to be the merits of functional languages on this site, do not take a lot of time and effort when implemented in imperative languages, they cannot be said to have real merits.

Problems with the sample program

Below, I will list the problems of the sample program that I personally felt.

Problem 1 I don't know what the variable name means

What does i or N in the sample program really mean? I didn't know what the variable names meant, so I couldn't figure out what the logic was for just by reading the program. At the very least, i should be counter and N should be ARRAY_LIMITTED_NUMBER to clarify the role of the variable.

Problem 2 The array declaration method has changed.

If you use an initializer when you initialize an array, why not set the number of elements in the array?

Problem 3 Access to the array is strange

There may be some misunderstandings when it comes to strange things, but isn't it easier to write the process by using pointers instead of subscripts to access arrays? If you access it with a pointer, at least the variable i should be unnecessary

Problem 3 Array data structure

Isn't this better than a problem? It is a proposal. It seems better to introduce NULL to represent the end of the array That way, you don't have to compare the subscripts with the maximum number of elements in the array when determining the loop exit condition. Well, in this case, there is a problem of what to do if NULL is inserted in the middle of the array, but as long as you use C language, you need to make some rules for the end of the array. The sample program also uses define to secretly define the maximum number of elements in the array as 5.

Problem 4 Regarding the point that data processing and data control are mixed

In the original program, data control (loop processing) and data processing (snake and frog simulation) are performed in one place. But isn't this contrary to structured programming, which has been said for decades before functional languages? If you're doing modern development, no matter what language you use, data control and data processing should be cut into separate functions.

Specific C language source

Below, I will post the C language code that I improved

Improved source

sample.c


#include "stdafx.h"

//Calculate the number of surviving frogs
int calc(int snakes, int flogs) {
	if (flogs >= snakes) return flogs - snakes;
	else return 0;
}

//A function that manipulates an array of frogs and snakes
int execute(int snakes[], int flogs[]) {
	int survivors = 0; //Total number of surviving frogs
	while (true) {
		//Escape the loop when you reach the end of the array
		if (*snakes == NULL) break;
		survivors += calc(*snakes++, *flogs++);
	}
	return survivors;
}

int main()
{
	int snakes[] = { 5,4,3,2,1,NULL };//snake
	int flogs[] = { 7,6,1,2,2 ,NULL };//Frog
	printf("Number of frogs that survived%5d", execute(snakes, flogs));
	getchar();
	return 0;
}

Using prototype declarations, I made it possible to perform functional language reduce and map-like behavior.

Improved source (map, reduce version)

sample.c


#include "stdafx.h"
typedef int(*FUNCPTR)(int a, int b);

//A function that manipulates an array of frogs and snakes
//Pass a function pointer to FUNCPTR that simulates the survival competition between frogs and snakes.
int zip_and_reduce(int snakes[], int flogs[],FUNCPTR func) {
	int survivors = 0; //Total number of surviving frogs
	while (true) {
		//Escape the loop if NULL appears in the array
		if (*snakes == NULL) break;
		survivors += func(*snakes++, *flogs++);
	}
	return survivors;
}

//Calculate the number of surviving frogs
int calc(int snakes, int flogs) {
	if (flogs >= snakes) return flogs - snakes;
	else return 0;
}

//Calculate the number when frogs and snakes coexist
int calc_kyouzon(int snakes, int flogs) {
	return flogs + snakes;
}

int main()
{
	int snakes[] = { 5,4,3,2,1,NULL };//snake
	int flogs[] = { 7,6,1,2,2 ,NULL };//Frog
	printf("Number of frogs that survived%5d", zip_and_reduce(snakes, flogs,&calc));
	printf("Number when snakes and frogs coexist%5d", zip_and_reduce(snakes, flogs, &calc_kyouzon));
	getchar();
	return 0;
}

I will also post the Ver implemented in Java.

sample.java


package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MyTest {
    public static void main(String args[]){
		List<Integer>snakes = new ArrayList<Integer>();
		Collections.addAll(snakes, 1,2,3,20);
		List<Integer>flogs = new ArrayList<Integer>();
		Collections.addAll(flogs, 10,2,3,4,5);
		System.out.println(execute(snakes,flogs));
	}
	//Functions exposed to the outside
	//Pass the snake and frog arguments as arguments
	public static int execute(List<Integer>snaeks,List<Integer>flogs){
		//The first total value is 0
		return execute(snaeks.iterator(),flogs.iterator() ,0);
	}
    //Functions used internally
	//Iterator<Integer>snakes :Enums of snakes
	//Iterator<Integer> :flogs enum
	//sum :Total value of flogs
	private static int execute(Iterator<Integer>snakes,Iterator<Integer>flogs ,int sum){
		//When there are no snakes or frogs, the process is complete
		if(!snakes.hasNext() && !flogs.hasNext())return sum;
		//Continue processing if either snake or frog is present
		else return execute(snakes,flogs,sum + calc(ifExists(snakes),ifExists(flogs) ) );
	}	
	//Find the number of frogs
	//Returns the number of frogs minus the number of snakes when there are more frogs than snakes
	//int snakes: Number of snakes
	//int flogs: Number of frogs
	private static int calc(int snakes,int flogs){
		if(snakes<= flogs) return flogs - snakes;
		else return 0;
	}
    //If the enumerator has an element, it returns that element, otherwise it returns 0.
	private static int ifExists(Iterator<Integer>ite){
		if(ite.hasNext())return ite.next();
		else return 0;
	}
}

At the end

Looking at the improved imperative language source and functional language source, I think that each person thinks differently. I'm not very familiar with functional languages (Haskell),

--Easy to handle arrays compared to C language --Compared to Java, it is easier to handle function pointers (like) in C, and there is flexibility in defining types.

I feel that there is a merit

On the contrary, how about the explanation that it is convenient because you can use reduce and map? I think

Recommended Posts

About the explanation about functional type
About the method
About the package
Output about the method # 2
About the StringBuilder class
Commentary: About the interface
About the asset pipeline
About the function double-java
About the ternary operator
null specifies the type
About the length method
About the Kernel module
About the authenticate method.
About the map method
[Java] About enum type
About the ancestors method
[Output] About the database
About the [ruby] operator
About the to_s method.
About the handling of Null
About specifying the JAXRS path
about the where method (rails)
Try functional type in Java! ①
[Swift] Summary about Bool type
About the description of Docker-compose.yml
About the File :: Stat class
Explanation of the FizzBuzz problem
About the same and equivalent
About the Android life cycle
About the programming language Crystal
Consideration about the times method
Explanation about Ruby String object
About the behavior of ruby Hash # ==
About the basics of Android development
[Rails] About the Punk List function
About the equals () and hashcode () methods
About the symbol <%%> in Rails erb
Explanation of the order of rails routes
About the information sharing app band
About the current development environment (Java 8)
Let's understand the Array (Element) type!
A murmur about the utility class
About the role of the initialize method
Think about the 7 rules of Optional
Explanation about Array object of Ruby
Summary about the introduction of Device
Let's understand the Optional (Wrapped) type!
About the log level of java.util.logging.Logger